X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/35c2acd4a81a3b41ced9253f744a00e64de2a423..bb276f53fca627231b1e1f50550a314a5687650b:/src/msw/listctrl.cpp diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp index 578af99fef..615d64f8b5 100644 --- a/src/msw/listctrl.cpp +++ b/src/msw/listctrl.cpp @@ -2,7 +2,7 @@ // Name: src/msw/listctrl.cpp // Purpose: wxListCtrl // Author: Julian Smart -// Modified by: +// Modified by: Agron Selimaj // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart @@ -17,11 +17,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" @@ -29,19 +24,22 @@ #pragma hdrstop #endif -#if wxUSE_LISTCTRL && defined(__WIN95__) +#if wxUSE_LISTCTRL + +#include "wx/listctrl.h" #ifndef WX_PRECOMP + #include "wx/msw/wrapcctl.h" // include "properly" #include "wx/app.h" #include "wx/intl.h" #include "wx/log.h" #include "wx/settings.h" + #include "wx/dcclient.h" + #include "wx/textctrl.h" #endif -#include "wx/textctrl.h" #include "wx/imaglist.h" -#include "wx/listctrl.h" -#include "wx/dcclient.h" +#include "wx/vector.h" #include "wx/msw/private.h" @@ -53,16 +51,14 @@ #endif #endif -// include "properly" -#include "wx/msw/wrapcctl.h" - // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines // it by its old name NM_FINDTIEM. // #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM) #define HAVE_NMLVFINDITEM 1 #elif defined(__DMC__) || defined(NM_FINDITEM) - #define HAVE_NM_FINDITEM 1 + #define HAVE_NMLVFINDITEM 1 + #define NMLVFINDITEM NM_FINDITEM #endif // ---------------------------------------------------------------------------- @@ -82,9 +78,38 @@ static void wxConvertFromMSWListItem(HWND hwndListCtrl, /* const */ LV_ITEM& lvItem); // convert our wxListItem to LV_COLUMN -static void wxConvertToMSWListCol(int col, const wxListItem& item, +static void wxConvertToMSWListCol(HWND hwndList, + int col, + const wxListItem& item, LV_COLUMN& lvCol); +namespace +{ + +// replacement for ListView_GetSubItemRect() which provokes warnings like +// "the address of 'rc' will always evaluate as 'true'" when used with mingw32 +// 4.3+ +// +// this function does no error checking on item and subitem parameters, notice +// that subitem 0 means the whole item so there is no way to retrieve the +// rectangle of the first subitem using this function, in particular notice +// that the index is *not* 1-based, in spite of what MSDN says +inline bool +wxGetListCtrlSubItemRect(HWND hwnd, int item, int subitem, int flags, RECT& rect) +{ + rect.top = subitem; + rect.left = flags; + return ::SendMessage(hwnd, LVM_GETSUBITEMRECT, item, (LPARAM)&rect) != 0; +} + +inline bool +wxGetListCtrlItemRect(HWND hwnd, int item, int flags, RECT& rect) +{ + return wxGetListCtrlSubItemRect(hwnd, item, 0, flags, rect); +} + +} // anonymous namespace + // ---------------------------------------------------------------------------- // private helper classes // ---------------------------------------------------------------------------- @@ -123,7 +148,7 @@ public: } // init with conversion - void Init(LV_ITEM_OTHER& item) + void Init(const LV_ITEM_OTHER& item) { // avoid unnecessary dynamic memory allocation, jjust make m_pItem // point to our own m_item @@ -162,7 +187,7 @@ private: LV_ITEM_NATIVE *m_pItem; LV_ITEM_NATIVE m_item; - DECLARE_NO_COPY_CLASS(wxLV_ITEM) + wxDECLARE_NO_COPY_CLASS(wxLV_ITEM); }; /////////////////////////////////////////////////////// @@ -170,7 +195,7 @@ private: // The MSW version had problems with SetTextColour() et // al as the wxListItemAttr's were stored keyed on the // item index. If a item was inserted anywhere but the end -// of the list the the text attributes (colour etc) for +// of the list the text attributes (colour etc) for // the following items were out of sync. // // Solution: @@ -182,73 +207,28 @@ private: // // However what we can do is store a pointer to a // structure which contains the attributes we want *and* -// a lParam for the users data, e.g. -// -// class wxListItemInternalData -// { -// public: -// wxListItemAttr *attr; -// long lParam; // user data -// }; +// a lParam -- and this is what wxMSWListItemData does. // -// To conserve memory, a wxListItemInternalData is +// To conserve memory, a wxMSWListItemData is // only allocated for a LV_ITEM if text attributes or // user data(lparam) are being set. - - -// class wxListItemInternalData -class wxListItemInternalData +class wxMSWListItemData { public: - wxListItemAttr *attr; - LPARAM lParam; // user data + wxMSWListItemData() : attr(NULL), lParam(0) {} + ~wxMSWListItemData() { delete attr; } - wxListItemInternalData() : attr(NULL), lParam(0) {} - ~wxListItemInternalData() - { - if (attr) - delete attr; - }; + wxListItemAttr *attr; + LPARAM lParam; // real user data - DECLARE_NO_COPY_CLASS(wxListItemInternalData) + wxDECLARE_NO_COPY_CLASS(wxMSWListItemData); }; // Get the internal data structure -static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId); -static wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId); -static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId); -static void wxDeleteInternalData(wxListCtrl* ctl, long itemId); +static wxMSWListItemData *wxGetInternalData(HWND hwnd, long itemId); +static wxMSWListItemData *wxGetInternalData(const wxListCtrl *ctl, long itemId); -// ---------------------------------------------------------------------------- -// events -// ---------------------------------------------------------------------------- - -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS) -#if WXWIN_COMPATIBILITY_2_4 -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO) -#endif -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED) -DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT) - #if wxUSE_EXTENDED_RTTI WX_DEFINE_FLAGS( wxListCtrlStyle ) @@ -311,7 +291,7 @@ 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 (à la NotebookPageInfo) + TODO : Expose more information of a list's layout etc. via appropriate objects (a la NotebookPageInfo) */ #else IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl) @@ -336,15 +316,17 @@ END_EVENT_TABLE() void wxListCtrl::Init() { - m_imageListNormal = NULL; - m_imageListSmall = NULL; + m_imageListNormal = + m_imageListSmall = m_imageListState = NULL; - m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false; + m_ownsImageListNormal = + m_ownsImageListSmall = + m_ownsImageListState = false; + m_colCount = 0; m_count = 0; - m_ignoreChangeMessages = false; m_textCtrl = NULL; - m_AnyInternalData = false; + m_hasAnyAttr = false; } @@ -366,15 +348,38 @@ bool wxListCtrl::Create(wxWindow *parent, // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build wxSetCCUnicodeFormat(GetHwnd()); + // We must set the default text colour to the system/theme color, otherwise + // GetTextColour will always return black + SetTextColour(GetDefaultAttributes().colFg); + + if ( InReportView() ) + MSWSetExListStyles(); + + return true; +} + +void wxListCtrl::MSWSetExListStyles() +{ // for comctl32.dll v 4.70+ we want to have some non default extended // styles because it's prettier (and also because wxGTK does it like this) - if ( InReportView() && wxApp::GetComCtl32Version() >= 470 ) - { - ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, - 0, LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT); + if ( wxApp::GetComCtl32Version() >= 470 ) + { + ::SendMessage + ( + GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0, + // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's + // not defined in the SDK headers +#ifdef LVS_EX_LABELTIP + LVS_EX_LABELTIP | +#endif + LVS_EX_FULLROWSELECT | + LVS_EX_SUBITEMIMAGES | + // normally this should be governed by a style as it's probably not + // always appropriate, but we don't have any free styles left and + // it seems better to enable it by default than disable + LVS_EX_HEADERDRAGDROP + ); } - - return true; } WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const @@ -383,15 +388,15 @@ WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; -#ifdef __WXDEBUG__ +#if wxDEBUG_LEVEL size_t nModes = 0; #define MAP_MODE_STYLE(wx, ms) \ if ( style & (wx) ) { wstyle |= (ms); nModes++; } -#else // !__WXDEBUG__ +#else // !wxDEBUG_LEVEL #define MAP_MODE_STYLE(wx, ms) \ if ( style & (wx) ) wstyle |= (ms); -#endif // __WXDEBUG__ +#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL MAP_MODE_STYLE(wxLC_ICON, LVS_ICON) MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON) @@ -473,29 +478,26 @@ void wxListCtrl::UpdateStyle() if ( dwStyleOld != dwStyleNew ) { ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew); + + // if we switched to the report view, set the extended styles for + // it too + if ( !(dwStyleOld & LVS_REPORT) && (dwStyleNew & LVS_REPORT) ) + MSWSetExListStyles(); } } } void wxListCtrl::FreeAllInternalData() { - if (m_AnyInternalData) - { - int n = GetItemCount(); + const unsigned count = m_internalData.size(); + for ( unsigned n = 0; n < count; n++ ) + delete m_internalData[n]; - m_ignoreChangeMessages = true; - for (int i = 0; i < n; i++) - wxDeleteInternalData(this, i); - m_ignoreChangeMessages = false; - - m_AnyInternalData = false; - } + m_internalData.clear(); } -wxListCtrl::~wxListCtrl() +void wxListCtrl::DeleteEditControl() { - FreeAllInternalData(); - if ( m_textCtrl ) { m_textCtrl->UnsubclassWin(); @@ -503,6 +505,13 @@ wxListCtrl::~wxListCtrl() delete m_textCtrl; m_textCtrl = NULL; } +} + +wxListCtrl::~wxListCtrl() +{ + FreeAllInternalData(); + + DeleteEditControl(); if (m_ownsImageListNormal) delete m_imageListNormal; @@ -545,7 +554,7 @@ void wxListCtrl::SetWindowStyleFlag(long flag) { if ( flag != m_windowStyle ) { - m_windowStyle = flag; + wxControl::SetWindowStyleFlag(flag); UpdateStyle(); @@ -663,10 +672,10 @@ bool wxListCtrl::GetColumn(int col, wxListItem& item) const } // Sets information about this column -bool wxListCtrl::SetColumn(int col, wxListItem& item) +bool wxListCtrl::SetColumn(int col, const wxListItem& item) { LV_COLUMN lvCol; - wxConvertToMSWListCol(col, item, lvCol); + wxConvertToMSWListCol(GetHwnd(), col, item, lvCol); return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0; } @@ -691,6 +700,67 @@ bool wxListCtrl::SetColumnWidth(int col, int width) return ListView_SetColumnWidth(GetHwnd(), col, width) != 0; } +// ---------------------------------------------------------------------------- +// columns order +// ---------------------------------------------------------------------------- + +int wxListCtrl::GetColumnIndexFromOrder(int order) const +{ + const int numCols = GetColumnCount(); + wxCHECK_MSG( order >= 0 && order < numCols, -1, + _T("Column position out of bounds") ); + + wxArrayInt indexArray(numCols); + if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) + return -1; + + return indexArray[order]; +} + +int wxListCtrl::GetColumnOrder(int col) const +{ + const int numCols = GetColumnCount(); + wxASSERT_MSG( col >= 0 && col < numCols, _T("Column index out of bounds") ); + + wxArrayInt indexArray(numCols); + if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) + return -1; + + for ( int pos = 0; pos < numCols; pos++ ) + { + if ( indexArray[pos] == col ) + return pos; + } + + wxFAIL_MSG( _T("no column with with given order?") ); + + return -1; +} + +// Gets the column order for all columns +wxArrayInt wxListCtrl::GetColumnsOrder() const +{ + const int numCols = GetColumnCount(); + + wxArrayInt orders(numCols); + if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &orders[0]) ) + orders.clear(); + + return orders; +} + +// Sets the column order for all columns +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") ); + + return ListView_SetColumnOrderArray(GetHwnd(), numCols, &orders[0]) != 0; +} + + // Gets the number of items that can fit vertically in the // visible area of the list control (list or report view) // or the total number of items in the list control (icon @@ -703,6 +773,25 @@ int wxListCtrl::GetCountPerPage() const // Gets the edit control for editing labels. 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 + // case EditLabel() did create the control but it didn't have an HWND to + // initialize it with yet + if ( !m_textCtrl || !m_textCtrl->GetHWND() ) + { + HWND hwndEdit = ListView_GetEditControl(GetHwnd()); + if ( hwndEdit ) + { + wxListCtrl * const self = const_cast(this); + + if ( !m_textCtrl ) + self->m_textCtrl = new wxTextCtrl; + self->InitEditControl((WXHWND)hwndEdit); + } + } + return m_textCtrl; } @@ -759,50 +848,55 @@ bool wxListCtrl::GetItem(wxListItem& info) const // Sets information about the item bool wxListCtrl::SetItem(wxListItem& info) { + const long id = info.GetId(); + wxCHECK_MSG( id >= 0 && id < GetItemCount(), false, + _T("invalid item index in SetItem") ); + LV_ITEM item; wxConvertToMSWListItem(this, info, item); // we never update the lParam if it contains our pointer - // to the wxListItemInternalData structure + // to the wxMSWListItemData structure item.mask &= ~LVIF_PARAM; // check if setting attributes or lParam - if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA)) + if ( info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA) ) { // get internal item data - // perhaps a cache here ? - wxListItemInternalData *data = wxGetInternalData(this, info.m_itemId); + wxMSWListItemData *data = MSWGetItemData(id); - if (! data) + if ( !data ) { - // need to set it - m_AnyInternalData = true; - data = new wxListItemInternalData(); + // need to allocate the internal data object + data = new wxMSWListItemData; + m_internalData.push_back(data); item.lParam = (LPARAM) data; item.mask |= LVIF_PARAM; - }; + } // user data - if (info.m_mask & wxLIST_MASK_DATA) + if ( info.m_mask & wxLIST_MASK_DATA ) data->lParam = info.m_data; // attributes - if (info.HasAttributes()) + if ( info.HasAttributes() ) { - if (data->attr) - *data->attr = *info.GetAttributes(); + const wxListItemAttr& attrNew = *info.GetAttributes(); + + // don't overwrite the already set attributes if we have them + if ( data->attr ) + data->attr->AssignFrom(attrNew); else - data->attr = new wxListItemAttr(*info.GetAttributes()); - }; - }; + data->attr = new wxListItemAttr(attrNew); + } + } // we could be changing only the attribute in which case we don't need to // call ListView_SetItem() at all if ( item.mask ) { - item.cchTextMax = 0; if ( !ListView_SetItem(GetHwnd(), &item) ) { wxLogDebug(_T("ListView_SetItem() failed")); @@ -873,14 +967,15 @@ bool wxListCtrl::SetItemState(long item, long state, long stateMask) wxConvertToMSWFlags(state, stateMask, lvItem); + const bool changingFocus = (stateMask & wxLIST_STATE_FOCUSED) && + (state & wxLIST_STATE_FOCUSED); + // for the virtual list controls we need to refresh the previously focused // item manually when changing focus without changing selection // programmatically because otherwise it keeps its focus rectangle until // next repaint (yet another comctl32 bug) long focusOld; - if ( IsVirtual() && - (stateMask & wxLIST_STATE_FOCUSED) && - (state & wxLIST_STATE_FOCUSED) ) + if ( IsVirtual() && changingFocus ) { focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); } @@ -908,17 +1003,34 @@ bool wxListCtrl::SetItemState(long item, long state, long stateMask) } } + // we expect the selection anchor, i.e. the item from which multiple + // selection (such as performed with e.g. Shift-arrows) starts, to be the + // same as the currently focused item but the native control doesn't update + // it when we change focus and leaves at the last item it set itself focus + // to, so do it explicitly + if ( changingFocus && !HasFlag(wxLC_SINGLE_SEL) ) + { + ListView_SetSelectionMark(GetHwnd(), item); + } + return true; } // Sets the item image bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage)) +{ + return SetItemColumnImage(item, 0, image); +} + +// Sets the item image +bool wxListCtrl::SetItemColumnImage(long item, long column, int image) { wxListItem info; info.m_mask = wxLIST_MASK_IMAGE; info.m_image = image; info.m_itemId = item; + info.m_col = column; return SetItem(info); } @@ -948,6 +1060,19 @@ void wxListCtrl::SetItemText(long item, const wxString& str) SetItem(info); } +// Gets the internal item data +wxMSWListItemData *wxListCtrl::MSWGetItemData(long itemId) const +{ + LV_ITEM it; + it.mask = LVIF_PARAM; + it.iItem = itemId; + + if ( !ListView_GetItem(GetHwnd(), &it) ) + return NULL; + + return (wxMSWListItemData *) it.lParam; +} + // Gets the item data wxUIntPtr wxListCtrl::GetItemData(long item) const { @@ -962,7 +1087,7 @@ wxUIntPtr wxListCtrl::GetItemData(long item) const } // Sets the item data -bool wxListCtrl::SetItemData(long item, long data) +bool wxListCtrl::SetItemPtrData(long item, wxUIntPtr data) { wxListItem info; @@ -975,19 +1100,40 @@ bool wxListCtrl::SetItemData(long item, long data) wxRect wxListCtrl::GetViewRect() const { - wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST), - _T("wxListCtrl::GetViewRect() only works in icon mode") ); + wxRect rect; - RECT rc; - if ( !ListView_GetViewRect(GetHwnd(), &rc) ) + // ListView_GetViewRect() can only be used in icon and small icon views + // (this is documented in MSDN and, indeed, it returns bogus results in + // report view, at least with comctl32.dll v6 under Windows 2003) + if ( HasFlag(wxLC_ICON | wxLC_SMALL_ICON) ) { - wxLogDebug(_T("ListView_GetViewRect() failed.")); + RECT rc; + if ( !ListView_GetViewRect(GetHwnd(), &rc) ) + { + wxLogDebug(_T("ListView_GetViewRect() failed.")); - wxZeroMemory(rc); + wxZeroMemory(rc); + } + + wxCopyRECTToRect(rc, rect); } + else if ( HasFlag(wxLC_REPORT) ) + { + const long count = GetItemCount(); + if ( count ) + { + GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count - 1), rect); - wxRect rect; - wxCopyRECTToRect(rc, rect); + // extend the rectangle to start at the top (we include the column + // headers, if any, for compatibility with the generic version) + rect.height += rect.y; + rect.y = 0; + } + } + else + { + wxFAIL_MSG( _T("not implemented in this mode") ); + } return rect; } @@ -995,7 +1141,21 @@ wxRect wxListCtrl::GetViewRect() const // Gets the item rectangle bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const { - RECT rectWin; + return GetSubItemRect( item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code) ; +} + +bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) const +{ + // ListView_GetSubItemRect() doesn't do subItem error checking and returns + // true even for the out of range values of it (even if the results are + // 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") ); + + // use wxCHECK_MSG against "item" too, for coherency with the generic implementation: + wxCHECK_MSG( item >= 0 && item < GetItemCount(), false, + _T("invalid item in GetSubItemRect") ); int codeWin; if ( code == wxLIST_RECT_BOUNDS ) @@ -1006,21 +1166,37 @@ bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const codeWin = LVIR_LABEL; else { - wxFAIL_MSG( _T("incorrect code in GetItemRect()") ); - + wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") ); codeWin = LVIR_BOUNDS; } - bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0; + RECT rectWin; + if ( !wxGetListCtrlSubItemRect + ( + GetHwnd(), + item, + subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM ? 0 : subItem, + codeWin, + rectWin + ) ) + { + return false; + } + + wxCopyRECTToRect(rectWin, rect); - rect.x = rectWin.left; - rect.y = rectWin.top; - rect.width = rectWin.right - rectWin.left; - rect.height = rectWin.bottom - rectWin.top; + // 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 ) + rect.width = GetColumnWidth(0); - return success; + return true; } + + + // Gets the item position bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const { @@ -1051,11 +1227,15 @@ wxSize wxListCtrl::GetItemSpacing() const return wxSize(LOWORD(spacing), HIWORD(spacing)); } +#if WXWIN_COMPATIBILITY_2_6 + int wxListCtrl::GetItemSpacing(bool isSmall) const { return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall); } +#endif // WXWIN_COMPATIBILITY_2_6 + void wxListCtrl::SetItemTextColour( long item, const wxColour &col ) { wxListItem info; @@ -1067,7 +1247,7 @@ void wxListCtrl::SetItemTextColour( long item, const wxColour &col ) wxColour wxListCtrl::GetItemTextColour( long item ) const { wxColour col; - wxListItemInternalData *data = wxGetInternalData(this, item); + wxMSWListItemData *data = MSWGetItemData(item); if ( data && data->attr ) col = data->attr->GetTextColour(); @@ -1085,7 +1265,7 @@ void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col ) wxColour wxListCtrl::GetItemBackgroundColour( long item ) const { wxColour col; - wxListItemInternalData *data = wxGetInternalData(this, item); + wxMSWListItemData *data = MSWGetItemData(item); if ( data && data->attr ) col = data->attr->GetBackgroundColour(); @@ -1103,7 +1283,7 @@ void wxListCtrl::SetItemFont( long item, const wxFont &f ) wxFont wxListCtrl::GetItemFont( long item ) const { wxFont f; - wxListItemInternalData *data = wxGetInternalData(this, item); + wxMSWListItemData *data = MSWGetItemData(item); if ( data && data->attr ) f = data->attr->GetFont(); @@ -1214,7 +1394,7 @@ void wxListCtrl::SetImageList(wxImageList *imageList, int which) m_imageListState = imageList; m_ownsImageListState = false; } - ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); + (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); } void wxListCtrl::AssignImageList(wxImageList *imageList, int which) @@ -1330,48 +1510,71 @@ void wxListCtrl::ClearAll() DeleteAllColumns(); } +void wxListCtrl::InitEditControl(WXHWND hWnd) +{ + m_textCtrl->SetHWND(hWnd); + m_textCtrl->SubclassWin(hWnd); + m_textCtrl->SetParent(this); + + // we must disallow TABbing away from the control while the edit contol 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) + m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB); +} + wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass) { - wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) ); + wxCHECK_MSG( textControlClass->IsKindOf(CLASSINFO(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 + // 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 + DeleteEditControl(); + m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); + WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item); if ( !hWnd ) { // failed to start editing - return NULL; - } - - // [re]create the text control wrapping the HWND we got - if ( m_textCtrl ) - { - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND(0); delete m_textCtrl; - } + m_textCtrl = NULL; - m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); - m_textCtrl->SetHWND(hWnd); - m_textCtrl->SubclassWin(hWnd); - m_textCtrl->SetParent(this); + return NULL; + } - // we must disallow TABbing away from the control while the edit contol 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) - m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB); + // if GetEditControl() hasn't been called, we need to initialize the edit + // control ourselves + if ( !m_textCtrl->GetHWND() ) + InitEditControl(hWnd); return m_textCtrl; } // End label editing, optionally cancelling the edit -bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel)) +bool wxListCtrl::EndEditLabel(bool cancel) { - wxFAIL_MSG( _T("not implemented") ); + // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT + HWND hwnd = ListView_GetEditControl(GetHwnd()); + if ( !hwnd ) + return false; + + if ( cancel ) + ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing + + // 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); - return false; + return true; } // Ensures this item is visible @@ -1389,34 +1592,53 @@ long wxListCtrl::FindItem(long start, const wxString& str, bool partial) findInfo.flags = LVFI_STRING; if ( partial ) findInfo.flags |= LVFI_PARTIAL; - findInfo.psz = str; + findInfo.psz = str.wx_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 // inconsistent with the generic version - so we adjust the index if (start != -1) start --; - return ListView_FindItem(GetHwnd(), (int) start, &findInfo); + return ListView_FindItem(GetHwnd(), start, &findInfo); } -// Find an item whose data matches this data, starting from the item after 'start' -// or the beginning if 'start' is -1. -// NOTE : Lindsay Mathieson - 14-July-2002 -// No longer use ListView_FindItem as the data attribute is now stored -// in a wxListItemInternalData structure refernced by the actual lParam +// Find an item whose data matches this data, starting from the item after +// 'start' or the beginning if 'start' is -1. long wxListCtrl::FindItem(long start, wxUIntPtr data) { - long idx = start + 1; - long count = GetItemCount(); - - while (idx < count) + // we can't use ListView_FindItem() directly as we don't store the data + // pointer itself in the control but rather our own internal data, so first + // we need to find the right value to search for (and there can be several + // of them) + int idx = wxNOT_FOUND; + const unsigned count = m_internalData.size(); + for ( unsigned n = 0; n < count; n++ ) { - if (GetItemData(idx) == data) - return idx; - idx++; - }; + if ( m_internalData[n]->lParam == (LPARAM)data ) + { + LV_FINDINFO findInfo; + findInfo.flags = LVFI_PARAM; + findInfo.lParam = (LPARAM)wxPtrToUInt(m_internalData[n]); - return -1; + int rc = ListView_FindItem(GetHwnd(), start, &findInfo); + if ( rc != -1 ) + { + if ( idx == wxNOT_FOUND || rc < idx ) + { + idx = rc; + if ( idx == start + 1 ) + { + // we can stop here, we don't risk finding a closer + // match + break; + } + } + //else: this item is after the previously found one + } + } + } + + return idx; } // Find an item nearest this position in the specified direction, starting from @@ -1439,18 +1661,30 @@ long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction) else if ( direction == wxLIST_FIND_RIGHT ) findInfo.vkDirection = VK_RIGHT; - return ListView_FindItem(GetHwnd(), (int) start, & findInfo); + return ListView_FindItem(GetHwnd(), start, &findInfo); } // Determines which item (if any) is at the specified point, // giving details in 'flags' (see wxLIST_HITTEST_... flags above) -long wxListCtrl::HitTest(const wxPoint& point, int& flags) +long +wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const { LV_HITTESTINFO hitTestInfo; hitTestInfo.pt.x = (int) point.x; hitTestInfo.pt.y = (int) point.y; - ListView_HitTest(GetHwnd(), & hitTestInfo); + long item; +#ifdef LVM_SUBITEMHITTEST + if ( ptrSubItem && wxApp::GetComCtl32Version() >= 470 ) + { + item = ListView_SubItemHitTest(GetHwnd(), &hitTestInfo); + *ptrSubItem = hitTestInfo.iSubItem; + } + else +#endif // LVM_SUBITEMHITTEST + { + item = ListView_HitTest(GetHwnd(), &hitTestInfo); + } flags = 0; @@ -1485,12 +1719,13 @@ long wxListCtrl::HitTest(const wxPoint& point, int& flags) flags |= wxLIST_HITTEST_ONITEMSTATEICON; } - return (long) hitTestInfo.iItem; + return item; } + // Inserts an item, returning the index of the new item if successful, // -1 otherwise. -long wxListCtrl::InsertItem(wxListItem& info) +long wxListCtrl::InsertItem(const wxListItem& info) { wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); @@ -1498,18 +1733,18 @@ long wxListCtrl::InsertItem(wxListItem& info) wxConvertToMSWListItem(this, info, item); item.mask &= ~LVIF_PARAM; - // check wether we need to allocate our internal data - bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes()); - if (needInternalData) + // check whether we need to allocate our internal data + bool needInternalData = (info.m_mask & wxLIST_MASK_DATA) || + info.HasAttributes(); + if ( needInternalData ) { - m_AnyInternalData = true; item.mask |= LVIF_PARAM; - // internal stucture that manages data - wxListItemInternalData *data = new wxListItemInternalData(); - item.lParam = (LPARAM) data; + wxMSWListItemData * const data = new wxMSWListItemData; + m_internalData.push_back(data); + item.lParam = (LPARAM)data; - if (info.m_mask & wxLIST_MASK_DATA) + if ( info.m_mask & wxLIST_MASK_DATA ) data->lParam = info.m_data; // check whether it has any custom attributes @@ -1521,7 +1756,7 @@ long wxListCtrl::InsertItem(wxListItem& info) // and remember that we have some now... m_hasAnyAttr = true; } - }; + } long rv = ListView_InsertItem(GetHwnd(), & item); @@ -1563,10 +1798,10 @@ long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex) } // For list view mode (only), inserts a column. -long wxListCtrl::InsertColumn(long col, wxListItem& item) +long wxListCtrl::InsertColumn(long col, const wxListItem& item) { LV_COLUMN lvCol; - wxConvertToMSWListCol(col, item, lvCol); + wxConvertToMSWListCol(GetHwnd(), col, item, lvCol); if ( !(lvCol.mask & LVCF_WIDTH) ) { @@ -1641,26 +1876,26 @@ bool wxListCtrl::ScrollList(int dx, int dy) struct wxInternalDataSort { wxListCtrlCompare user_fn; - long data; + wxIntPtr data; }; int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { - struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort; + wxInternalDataSort * const internalData = (wxInternalDataSort *) lParamSort; - wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1; - wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2; + wxMSWListItemData *data1 = (wxMSWListItemData *) lParam1; + wxMSWListItemData *data2 = (wxMSWListItemData *) lParam2; long d1 = (data1 == NULL ? 0 : data1->lParam); long d2 = (data2 == NULL ? 0 : data2->lParam); return internalData->user_fn(d1, d2, internalData->data); -}; +} -bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data) +bool wxListCtrl::SortItems(wxListCtrlCompare fn, wxIntPtr data) { - struct wxInternalDataSort internalData; + wxInternalDataSort internalData; internalData.user_fn = fn; internalData.data = data; @@ -1683,8 +1918,24 @@ bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data) // message processing // ---------------------------------------------------------------------------- -bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id) +bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg) { + if ( msg->message == WM_KEYDOWN ) + { + // Only eat VK_RETURN if not being used by the application in + // conjunction with modifiers + if ( msg->wParam == VK_RETURN && !wxIsAnyModifierDown() ) + { + // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED + return false; + } + } + return wxControl::MSWShouldPreProcessMessage(msg); +} + +bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_) +{ + const int id = (signed short)id_; if (cmd == EN_UPDATE) { wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); @@ -1703,6 +1954,55 @@ bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id) return false; } +// utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify +int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick) +{ + // find the column clicked: we have to search for it ourselves as the + // notification message doesn't provide this info + + // where did the click occur? +#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 + if ( nmhdr->code == GN_CONTEXTMENU ) + { + *ptClick = ((NMRGINFO*)nmhdr)->ptAction; + } + else +#endif //__WXWINCE__ + if ( !::GetCursorPos(ptClick) ) + { + wxLogLastError(_T("GetCursorPos")); + } + + // we need to use listctrl coordinates for the event point so this is what + // we return in ptClick, but for comparison with Header_GetItemRect() + // result below we need to use header window coordinates + POINT ptClickHeader = *ptClick; + if ( !::ScreenToClient(nmhdr->hwndFrom, &ptClickHeader) ) + { + wxLogLastError(_T("ScreenToClient(listctrl header)")); + } + + if ( !::ScreenToClient(::GetParent(nmhdr->hwndFrom), ptClick) ) + { + wxLogLastError(_T("ScreenToClient(listctrl)")); + } + + const int colCount = Header_GetItemCount(nmhdr->hwndFrom); + for ( int col = 0; col < colCount; col++ ) + { + RECT rect; + if ( Header_GetItemRect(nmhdr->hwndFrom, col, &rect) ) + { + if ( ::PtInRect(&rect, ptClickHeader) ) + { + return col; + } + } + } + + return wxNOT_FOUND; +} + bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) { @@ -1770,47 +2070,12 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) #endif //__WXWINCE__ case NM_RCLICK: { - eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK; - event.m_col = -1; - - // find the column clicked: we have to search for it - // ourselves as the notification message doesn't provide - // this info - - // where did the click occur? POINT ptClick; -#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 - if(nmhdr->code == GN_CONTEXTMENU) { - ptClick = ((NMRGINFO*)nmhdr)->ptAction; - } else -#endif //__WXWINCE__ - if ( !::GetCursorPos(&ptClick) ) - { - wxLogLastError(_T("GetCursorPos")); - } - - if ( !::ScreenToClient(GetHwnd(), &ptClick) ) - { - wxLogLastError(_T("ScreenToClient(listctrl header)")); - } + eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK; + event.m_col = wxMSWGetColumnClicked(nmhdr, &ptClick); event.m_pointDrag.x = ptClick.x; event.m_pointDrag.y = ptClick.y; - - int colCount = Header_GetItemCount(hwndHdr); - - RECT rect; - for ( int col = 0; col < colCount; col++ ) - { - if ( Header_GetItemRect(hwndHdr, col, &rect) ) - { - if ( ::PtInRect(&rect, ptClick) ) - { - event.m_col = col; - break; - } - } - } } break; @@ -1820,7 +2085,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // parameters // // I have no idea what is the real cause of the bug (which is, - // just to make things interesting, is impossible to reproduce + // just to make things interesting, impossible to reproduce // reliably) but ignoring all these messages does fix it and // doesn't seem to have any negative consequences return true; @@ -1831,7 +2096,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) } else #endif // defined(HDN_BEGINTRACKA) - if ( nmhdr->hwndFrom == GetHwnd() ) + if ( nmhdr->hwndFrom == GetHwnd() ) { // almost all messages use NM_LISTVIEW NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr; @@ -1839,23 +2104,12 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) const int iItem = nmLV->iItem; - // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be - // ignored for efficiency. It is done here because the internal data is in the - // process of being deleted so we don't want to try and access it below. - if ( m_ignoreChangeMessages && - ( (nmLV->hdr.code == LVN_ITEMCHANGED) || - (nmLV->hdr.code == LVN_ITEMCHANGING)) ) - { - return true; - } - - // If we have a valid item then check if there is a data value // associated with it and put it in the event. if ( iItem >= 0 && iItem < GetItemCount() ) { - wxListItemInternalData *internaldata = - wxGetInternalData(GetHwnd(), iItem); + wxMSWListItemData *internaldata = + MSWGetItemData(iItem); if ( internaldata ) event.m_item.m_data = internaldata->lParam; @@ -1918,16 +2172,9 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) const LV_ITEM& lvi = (LV_ITEM)item; if ( !lvi.pszText || lvi.iItem == -1 ) { - // don't keep a stale wxTextCtrl around - if ( m_textCtrl ) - { - // EDIT control will be deleted by the list control itself so - // prevent us from deleting it as well - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND(0); - delete m_textCtrl; - m_textCtrl = NULL; - } + // EDIT control will be deleted by the list control + // itself so prevent us from deleting it as well + DeleteEditControl(); event.SetEditCanceled(true); } @@ -1959,19 +2206,25 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) eventType = wxEVT_COMMAND_LIST_DELETE_ITEM; event.m_itemIndex = iItem; - // delete the assoicated internal data - wxDeleteInternalData(this, iItem); - break; -#if WXWIN_COMPATIBILITY_2_4 - case LVN_SETDISPINFO: + // delete the associated internal data + if ( wxMSWListItemData *data = MSWGetItemData(iItem) ) { - eventType = wxEVT_COMMAND_LIST_SET_INFO; - LV_DISPINFO *info = (LV_DISPINFO *)lParam; - wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item); + 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; -#endif case LVN_INSERTITEM: eventType = wxEVT_COMMAND_LIST_INSERT_ITEM; @@ -2012,7 +2265,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // focus event from here and the selection one // below event.SetEventType(eventType); - (void)GetEventHandler()->ProcessEvent(event); + (void)HandleWindowEvent(event); } else // no focus event to send { @@ -2046,11 +2299,11 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) wxLIST_STATE_SELECTED); // or activate the selected item if any (but - // not with Shift and/or Ctrl as then they have a predefined - // meaning for the list view) + // not with any modifiers as they have a predefined meaning + // then) if ( lItem != -1 && (wVKey == VK_RETURN || wVKey == VK_SPACE) && - !(wxIsShiftDown() || wxIsCtrlDown()) ) + !wxIsAnyModifierDown() ) { eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; } @@ -2114,13 +2367,18 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) 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 ) + { + ::GetCursorPos(&(lvhti.pt)); + } + + ::ScreenToClient(GetHwnd(), &lvhti.pt); + if ( ListView_HitTest(GetHwnd(), &lvhti) != -1 ) { if ( lvhti.flags & LVHT_ONITEM ) { @@ -2136,7 +2394,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) case NM_CUSTOMDRAW: *result = OnCustomDraw(lParam); - return true; + return *result != CDRF_DODEFAULT; #endif // _WIN32_IE >= 0x300 case LVN_ODCACHEHINT: @@ -2160,7 +2418,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) } break; -#if HAVE_NMLVFINDITEM || HAVE_NM_FINDITEM +#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 @@ -2169,11 +2427,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // application waiting while it performs linear search if ( IsVirtual() && GetItemCount() <= 1000 ) { -#if HAVE_NMLVFINDITEM NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam; -#else - NM_FINDITEM* pFindInfo = (NM_FINDITEM*)lParam; -#endif // no match by default *result = -1; @@ -2234,7 +2488,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) processed = false; } break; -#endif // HAVE_NMLVFINDITEM || HAVE_NM_FINDITEM +#endif // HAVE_NMLVFINDITEM case LVN_GETDISPINFO: if ( IsVirtual() ) @@ -2247,21 +2501,24 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) if ( lvi.mask & LVIF_TEXT ) { wxString text = OnGetItemText(item, lvi.iSubItem); - wxStrncpy(lvi.pszText, text, lvi.cchTextMax); + wxStrlcpy(lvi.pszText, text.c_str(), lvi.cchTextMax); } // see comment at the end of wxListCtrl::GetColumn() #ifdef NM_CUSTOMDRAW if ( lvi.mask & LVIF_IMAGE ) { - lvi.iImage = OnGetItemImage(item); + lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem); } #endif // NM_CUSTOMDRAW - // a little dose of healthy paranoia: as we never use - // LVM_SETCALLBACKMASK we're not supposed to get these ones - wxASSERT_MSG( !(lvi.mask & LVIF_STATE), - _T("we don't support state callbacks yet!") ); + // even though we never use LVM_SETCALLBACKMASK, we still + // can get messages with LVIF_STATE in lvi.mask under Vista + if ( lvi.mask & LVIF_STATE ) + { + // we don't have anything to return from here... + lvi.stateMask = 0; + } return true; } @@ -2285,7 +2542,18 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) event.SetEventType(eventType); - bool processed = GetEventHandler()->ProcessEvent(event); + // 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) + if ( event.m_itemIndex != -1 && !event.m_item.GetMask() ) + { + 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 // --------------- @@ -2311,16 +2579,9 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) // logic here is inverted compared to all the other messages *result = event.IsAllowed(); - // don't keep a stale wxTextCtrl around - if ( m_textCtrl ) - { - // EDIT control will be deleted by the list control itself so - // prevent us from deleting it as well - m_textCtrl->UnsubclassWin(); - m_textCtrl->SetHWND(0); - delete m_textCtrl; - m_textCtrl = NULL; - } + // EDIT control will be deleted by the list control itself so + // prevent us from deleting it as well + DeleteEditControl(); return true; } @@ -2331,13 +2592,257 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) return processed; } +// ---------------------------------------------------------------------------- +// custom draw stuff +// ---------------------------------------------------------------------------- + // see comment at the end of wxListCtrl::GetColumn() #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 +static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd) +{ + RECT rc; + wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_BOUNDS, rc); + + RECT rcIcon; + wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_ICON, rcIcon); + + // exclude the icon part, neither the selection background nor focus rect + // should cover it + rc.left = rcIcon.right; + + return rc; +} + +static +bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont, int colCount) +{ + NMCUSTOMDRAW& nmcd = pLVCD->nmcd; + + HDC hdc = nmcd.hdc; + HWND hwndList = nmcd.hdr.hwndFrom; + const int col = pLVCD->iSubItem; + const DWORD item = nmcd.dwItemSpec; + + // the font must be valid, otherwise we wouldn't be painting the item at all + 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; + + // get the image and text to draw + wxChar text[512]; + LV_ITEM it; + wxZeroMemory(it); + it.mask = LVIF_TEXT | LVIF_IMAGE; + it.iItem = item; + it.iSubItem = col; + it.pszText = text; + it.cchTextMax = WXSIZEOF(text); + ListView_GetItem(hwndList, &it); + + HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL); + if ( himl && ImageList_GetImageCount(himl) ) + { + if ( it.iImage != -1 ) + { + ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top, + nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED + : ILD_TRANSPARENT); + } + + // notice that even if this item doesn't have any image, the list + // control still leaves space for the image in the first column if the + // image list is not empty (presumably so that items with and without + // images align?) + if ( it.iImage != -1 || it.iSubItem == 0 ) + { + int wImage, hImage; + ImageList_GetIconSize(himl, &wImage, &hImage); + + rc.left += wImage + 2; + } + } + + ::SetBkMode(hdc, TRANSPARENT); + + UINT fmt = DT_SINGLELINE | +#ifndef __WXWINCE__ + DT_WORD_ELLIPSIS | +#endif // __WXWINCE__ + DT_NOPREFIX | + DT_VCENTER; + + LV_COLUMN lvCol; + wxZeroMemory(lvCol); + lvCol.mask = LVCF_FMT; + if ( ListView_GetColumn(hwndList, col, &lvCol) ) + { + switch ( lvCol.fmt & LVCFMT_JUSTIFYMASK ) + { + case LVCFMT_LEFT: + fmt |= DT_LEFT; + break; + + case LVCFMT_CENTER: + fmt |= DT_CENTER; + break; + + case LVCFMT_RIGHT: + fmt |= DT_RIGHT; + break; + } + } + //else: failed to get alignment, assume it's DT_LEFT (default) + + DrawText(hdc, text, -1, &rc, fmt); + + return true; +} + +static void HandleItemPostpaint(NMCUSTOMDRAW nmcd) +{ + if ( nmcd.uItemState & CDIS_FOCUS ) + { + RECT rc = GetCustomDrawnItemRect(nmcd); + + // don't use the provided HDC, it's in some strange state by now + ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc); + } +} + +// pLVCD->clrText and clrTextBk should contain the colours to use +static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont) +{ + NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut + + const HWND hwndList = nmcd.hdr.hwndFrom; + const int item = nmcd.dwItemSpec; + + // unfortunately we can't trust CDIS_SELECTED, it is often set even when + // the item is not at all selected for some reason (comctl32 6), but we + // also can't always trust ListView_GetItem() as it could return the old + // item status if we're called just after the (de)selection, so remember + // the last item to gain selection and also check for it here + for ( int i = -1;; ) + { + i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED); + if ( i == -1 ) + { + nmcd.uItemState &= ~CDIS_SELECTED; + break; + } + + if ( i == item ) + { + nmcd.uItemState |= CDIS_SELECTED; + break; + } + } + + // same thing for CDIS_FOCUS (except simpler as there is only one of them) + // + // NB: cast is needed to work around the bug in mingw32 headers which don't + // have it inside ListView_GetNextItem() itself (unlike SDK ones) + if ( ::GetFocus() == hwndList && + ListView_GetNextItem( + hwndList, static_cast(-1), LVNI_FOCUSED) == item ) + { + nmcd.uItemState |= CDIS_FOCUS; + } + else + { + nmcd.uItemState &= ~CDIS_FOCUS; + } + + if ( nmcd.uItemState & CDIS_SELECTED ) + { + int syscolFg, syscolBg; + if ( ::GetFocus() == hwndList ) + { + syscolFg = COLOR_HIGHLIGHTTEXT; + syscolBg = COLOR_HIGHLIGHT; + } + else // selected but unfocused + { + syscolFg = COLOR_WINDOWTEXT; + syscolBg = COLOR_BTNFACE; + + // don't grey out the icon in this case neither + nmcd.uItemState &= ~CDIS_SELECTED; + } + + pLVCD->clrText = ::GetSysColor(syscolFg); + pLVCD->clrTextBk = ::GetSysColor(syscolBg); + } + //else: not selected, use normal colours from pLVCD + + HDC hdc = nmcd.hdc; + RECT rc = GetCustomDrawnItemRect(nmcd); + + ::SetTextColor(hdc, pLVCD->clrText); + ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk)); + + // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint + // problems so just draw everything except the focus rect from here instead + const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList)); + for ( int col = 0; col < colCount; col++ ) + { + pLVCD->iSubItem = col; + HandleSubItemPrepaint(pLVCD, hfont, colCount); + } + + HandleItemPostpaint(nmcd); +} + +static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl, + LPNMLVCUSTOMDRAW pLVCD, + wxListItemAttr *attr) +{ + if ( !attr ) + { + // nothing to do for this item + return CDRF_DODEFAULT; + } + + + // set the colours to use for text drawing + pLVCD->clrText = attr->HasTextColour() + ? wxColourToRGB(attr->GetTextColour()) + : wxColourToRGB(listctrl->GetTextColour()); + pLVCD->clrTextBk = attr->HasBackgroundColour() + ? wxColourToRGB(attr->GetBackgroundColour()) + : wxColourToRGB(listctrl->GetBackgroundColour()); + + // select the font if non default one is specified + if ( attr->HasFont() ) + { + wxFont font = attr->GetFont(); + if ( font.GetEncoding() != wxFONTENCODING_SYSTEM ) + { + // the standard control ignores the font encoding/charset, at least + // with recent comctl32.dll versions (5 and 6, it uses to work with + // 4.something) so we have to draw the item entirely ourselves in + // this case + HandleItemPaint(pLVCD, GetHfontOf(font)); + return CDRF_SKIPDEFAULT; + } + + ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font)); + + return CDRF_NEWFONT; + } + + return CDRF_DODEFAULT; +} + WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam) { - LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam; - NMCUSTOMDRAW& nmcd = lplvcd->nmcd; + LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam; + NMCUSTOMDRAW& nmcd = pLVCD->nmcd; switch ( nmcd.dwDrawStage ) { case CDDS_PREPAINT: @@ -2346,78 +2851,30 @@ WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam) // // for virtual controls, always suppose that we have attributes as // there is no way to check for this - return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW - : CDRF_DODEFAULT; + if ( IsVirtual() || m_hasAnyAttr ) + return CDRF_NOTIFYITEMDRAW; + break; case CDDS_ITEMPREPAINT: - { - size_t item = (size_t)nmcd.dwItemSpec; - if ( item >= (size_t)GetItemCount() ) - { - // we get this message with item == 0 for an empty control, - // we must ignore it as calling OnGetItemAttr() would be - // wrong - return CDRF_DODEFAULT; - } + // get a message for each subitem + return CDRF_NOTIFYITEMDRAW; - wxListItemAttr *attr = - IsVirtual() ? OnGetItemAttr(item) - : wxGetInternalDataAttr(this, item); + case CDDS_SUBITEM | CDDS_ITEMPREPAINT: + const int item = nmcd.dwItemSpec; + const int column = pLVCD->iSubItem; - if ( !attr ) - { - // nothing to do for this item - return CDRF_DODEFAULT; - } - - HFONT hFont; - wxColour colText, colBack; - if ( attr->HasFont() ) - { - wxFont font = attr->GetFont(); - hFont = (HFONT)font.GetResourceHandle(); - } - else - { - hFont = 0; - } - - if ( attr->HasTextColour() ) - { - colText = attr->GetTextColour(); - } - else - { - colText = GetTextColour(); - } - - if ( attr->HasBackgroundColour() ) - { - colBack = attr->GetBackgroundColour(); - } - else - { - colBack = GetBackgroundColour(); - } - - lplvcd->clrText = wxColourToRGB(colText); - lplvcd->clrTextBk = wxColourToRGB(colBack); - - // note that if we wanted to set colours for - // individual columns (subitems), we would have - // returned CDRF_NOTIFYSUBITEMREDRAW from here - if ( hFont ) - { - ::SelectObject(nmcd.hdc, hFont); - - return CDRF_NEWFONT; - } - } - // fall through to return CDRF_DODEFAULT + // we get this message with item == 0 for an empty control, we + // must ignore it as calling OnGetItemAttr() would be wrong + if ( item < 0 || item >= GetItemCount() ) + break; + // same for columns + if ( column < 0 || column >= GetColumnCount() ) + break; - default: - return CDRF_DODEFAULT; + return HandleItemPrepaint(this, pLVCD, DoGetItemColumnAttr(item, column)); } + + return CDRF_DODEFAULT; } #endif // NM_CUSTOMDRAW supported @@ -2425,6 +2882,16 @@ WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam) // Necessary for drawing hrules and vrules, if specified void wxListCtrl::OnPaint(wxPaintEvent& event) { + const int itemCount = GetItemCount(); + const bool drawHRules = HasFlag(wxLC_HRULES); + const bool drawVRules = HasFlag(wxLC_VRULES); + + if (!InReportView() || !(drawHRules || drawVRules) || !itemCount) + { + event.Skip(); + return; + } + wxPaintDC dc(this); wxControl::OnPaint(event); @@ -2432,25 +2899,17 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) // Reset the device origin since it may have been set dc.SetDeviceOrigin(0, 0); - bool drawHRules = HasFlag(wxLC_HRULES); - bool drawVRules = HasFlag(wxLC_VRULES); - - if (!InReportView() || !drawHRules && !drawVRules) - return; - - wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID); + wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT)); dc.SetPen(pen); dc.SetBrush(* wxTRANSPARENT_BRUSH); wxSize clientSize = GetClientSize(); wxRect itemRect; - int itemCount = GetItemCount(); - int i; if (drawHRules) { - long top = GetTopItem(); - for (i = top; i < top + GetCountPerPage() + 1; i++) + const long top = GetTopItem(); + for ( int i = top; i < top + GetCountPerPage() + 1; i++ ) { if (GetItemRect(i, itemRect)) { @@ -2464,25 +2923,47 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) { cy = itemRect.GetBottom(); dc.DrawLine(0, cy, clientSize.x, cy); + break; } } } } - i = itemCount - 1; - if (drawVRules && (i > -1)) + + if (drawVRules) { wxRect firstItemRect; GetItemRect(0, firstItemRect); - if (GetItemRect(i, itemRect)) + if (GetItemRect(itemCount - 1, itemRect)) { - int col; + // this is a fix for bug 673394: erase the pixels which we would + // otherwise leave on the screen + static const int gap = 2; + dc.SetPen(*wxTRANSPARENT_PEN); + dc.SetBrush(wxBrush(GetBackgroundColour())); + dc.DrawRectangle(0, firstItemRect.GetY() - gap, + clientSize.GetWidth(), gap); + + dc.SetPen(pen); + dc.SetBrush(*wxTRANSPARENT_BRUSH); + + const int numCols = GetColumnCount(); + wxVector indexArray(numCols); + if ( !ListView_GetColumnOrderArray(GetHwnd(), + numCols, + &indexArray[0]) ) + { + wxFAIL_MSG( _T("invalid column index array in OnPaint()") ); + return; + } + int x = itemRect.GetX(); - for (col = 0; col < GetColumnCount(); col++) + for (int col = 0; col < numCols; col++) { - int colWidth = GetColumnWidth(col); + int colWidth = GetColumnWidth(indexArray[col]); x += colWidth ; - dc.DrawLine(x-1, firstItemRect.GetY() - 2, x-1, itemRect.GetBottom()); + dc.DrawLine(x-1, firstItemRect.GetY() - gap, + x-1, itemRect.GetBottom()); } } } @@ -2491,15 +2972,26 @@ void wxListCtrl::OnPaint(wxPaintEvent& event) WXLRESULT wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) { -#ifdef WM_PRINT - if ( nMsg == WM_PRINT ) + switch ( nMsg ) { - // we should bypass our own WM_PRINT handling as we don't handle - // PRF_CHILDREN flag, so leave it to the native control itself - return MSWDefWindowProc(nMsg, wParam, lParam); - } +#ifdef WM_PRINT + case WM_PRINT: + // we should bypass our own WM_PRINT handling as we don't handle + // PRF_CHILDREN flag, so leave it to the native control itself + return MSWDefWindowProc(nMsg, wParam, lParam); #endif // WM_PRINT + case WM_CONTEXTMENU: + // 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 + // so just ignore them + if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) ) + return 0; + //else: break + } + return wxControl::MSWWindowProc(nMsg, wParam, lParam); } @@ -2520,7 +3012,15 @@ int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const { wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL), -1, - wxT("List control has an image list, OnGetItemImage should be overridden.")); + wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden.")); + return -1; +} + +int wxListCtrl::OnGetItemColumnImage(long item, long column) const +{ + if (!column) + return OnGetItemImage(item); + return -1; } @@ -2533,6 +3033,15 @@ wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) cons return NULL; } +wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const +{ + if ( IsVirtual() ) + return OnGetItemColumnAttr(item, column); + + wxMSWListItemData * const data = MSWGetItemData(item); + return data ? data->attr : NULL; +} + void wxListCtrl::SetItemCount(long count) { wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); @@ -2549,74 +3058,12 @@ void wxListCtrl::SetItemCount(long count) void wxListCtrl::RefreshItem(long item) { - // strangely enough, ListView_Update() results in much more flicker here - // than a dumb Refresh() -- why? -#if 0 - if ( !ListView_Update(GetHwnd(), item) ) - { - wxLogLastError(_T("ListView_Update")); - } -#else // 1 - wxRect rect; - GetItemRect(item, rect); - RefreshRect(rect); -#endif // 0/1 + RefreshItems(item, item); } void wxListCtrl::RefreshItems(long itemFrom, long itemTo) { - wxRect rect1, rect2; - GetItemRect(itemFrom, rect1); - GetItemRect(itemTo, rect2); - - wxRect rect = rect1; - rect.height = rect2.GetBottom() - rect1.GetTop(); - - RefreshRect(rect); -} - -// ---------------------------------------------------------------------------- -// internal data stuff -// ---------------------------------------------------------------------------- - -static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId) -{ - LV_ITEM it; - it.mask = LVIF_PARAM; - it.iItem = itemId; - - if ( !ListView_GetItem(hwnd, &it) ) - return NULL; - - return (wxListItemInternalData *) it.lParam; -}; - -static -wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId) -{ - return wxGetInternalData(GetHwndOf(ctl), itemId); -}; - -static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId) -{ - wxListItemInternalData *data = wxGetInternalData(ctl, itemId); - - return data ? data->attr : NULL; -}; - -static void wxDeleteInternalData(wxListCtrl* ctl, long itemId) -{ - wxListItemInternalData *data = wxGetInternalData(ctl, itemId); - if (data) - { - LV_ITEM item; - memset(&item, 0, sizeof(item)); - item.iItem = itemId; - item.mask = LVIF_PARAM; - item.lParam = (LPARAM) 0; - ListView_SetItem((HWND)ctl->GetHWND(), &item); - delete data; - } + ListView_RedrawItems(GetHwnd(), itemFrom, itemTo); } // ---------------------------------------------------------------------------- @@ -2627,8 +3074,8 @@ static void wxConvertFromMSWListItem(HWND hwndListCtrl, wxListItem& info, LV_ITEM& lvItem) { - wxListItemInternalData *internaldata = - (wxListItemInternalData *) lvItem.lParam; + wxMSWListItemData *internaldata = + (wxMSWListItemData *) lvItem.lParam; if (internaldata) info.m_data = internaldata->lParam; @@ -2743,6 +3190,16 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl, const wxListItem& info, LV_ITEM& lvItem) { + if ( ctrl->InReportView() ) + { + wxASSERT_MSG( 0 <= info.m_col && info.m_col < ctrl->GetColumnCount(), + "wxListCtrl column index out of bounds" ); + } + else // not in report view + { + wxASSERT_MSG( info.m_col == 0, "columns only exist in report view" ); + } + lvItem.iItem = (int) info.m_itemId; lvItem.iImage = info.m_image; @@ -2768,9 +3225,9 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl, else { // pszText is not const, hence the cast - lvItem.pszText = (wxChar *)info.m_text.c_str(); + lvItem.pszText = (wxChar *)info.m_text.wx_str(); if ( lvItem.pszText ) - lvItem.cchTextMax = info.m_text.Length(); + lvItem.cchTextMax = info.m_text.length(); else lvItem.cchTextMax = 0; } @@ -2779,7 +3236,9 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl, lvItem.mask |= LVIF_IMAGE; } -static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item, +static void wxConvertToMSWListCol(HWND hwndList, + int col, + const wxListItem& item, LV_COLUMN& lvCol) { wxZeroMemory(lvCol); @@ -2787,7 +3246,7 @@ static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item, if ( item.m_mask & wxLIST_MASK_TEXT ) { lvCol.mask |= LVCF_TEXT; - lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe + lvCol.pszText = (wxChar *)item.m_text.wx_str(); // cast is safe } if ( item.m_mask & wxLIST_MASK_FORMAT ) @@ -2817,18 +3276,36 @@ static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item, #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 if ( item.m_mask & wxLIST_MASK_IMAGE ) { - if ( wxTheApp->GetComCtl32Version() >= 470 ) + if ( wxApp::GetComCtl32Version() >= 470 ) { - lvCol.mask |= LVCF_IMAGE | LVCF_FMT; + lvCol.mask |= LVCF_IMAGE; - // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right + // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right // seem to be generally nicer than on the left and the generic // version only draws them on the right (we don't have a flag to // specify the image location anyhow) // // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to // make any difference in my tests -- but maybe we should? - lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE; + if ( item.m_image != -1 ) + { + // as we're going to overwrite the format field, get its + // current value first -- unless we want to overwrite it anyhow + if ( !(lvCol.mask & LVCF_FMT) ) + { + LV_COLUMN lvColOld; + wxZeroMemory(lvColOld); + lvColOld.mask = LVCF_FMT; + if ( ListView_GetColumn(hwndList, col, &lvColOld) ) + { + lvCol.fmt = lvColOld.fmt; + } + + lvCol.mask |= LVCF_FMT; + } + + lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE; + } lvCol.iImage = item.m_image; } @@ -2838,4 +3315,3 @@ static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item, } #endif // wxUSE_LISTCTRL -