1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/listctrl.cpp 
   4 // Author:      Julian Smart 
   5 // Modified by: Agron Selimaj 
   8 // Copyright:   (c) Julian Smart 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 // For compilers that support precompilation, includes "wx.h". 
  21 #include "wx/wxprec.h" 
  29 #include "wx/listctrl.h" 
  32     #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" 
  36     #include "wx/settings.h" 
  37     #include "wx/dcclient.h" 
  38     #include "wx/textctrl.h" 
  41 #include "wx/imaglist.h" 
  42 #include "wx/vector.h" 
  44 #include "wx/msw/private.h" 
  45 #include "wx/msw/private/keyboard.h" 
  47 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) 
  55 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines 
  56 // it by its old name NM_FINDTIEM. 
  58 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM) 
  59     #define HAVE_NMLVFINDITEM 1 
  60 #elif defined(__DMC__) || defined(NM_FINDITEM) 
  61     #define HAVE_NMLVFINDITEM 1 
  62     #define NMLVFINDITEM NM_FINDITEM 
  65 // ---------------------------------------------------------------------------- 
  67 // ---------------------------------------------------------------------------- 
  69 // convert our state and mask flags to LV_ITEM constants 
  70 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
); 
  72 // convert wxListItem to LV_ITEM 
  73 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
  74                                    const wxListItem
& info
, LV_ITEM
& lvItem
); 
  76 // convert LV_ITEM to wxListItem 
  77 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
  79                                      /* const */ LV_ITEM
& lvItem
); 
  81 // convert our wxListItem to LV_COLUMN 
  82 static void wxConvertToMSWListCol(HWND hwndList
, 
  84                                   const wxListItem
& item
, 
  90 // replacement for ListView_GetSubItemRect() which provokes warnings like 
  91 // "the address of 'rc' will always evaluate as 'true'" when used with mingw32 
  94 // this function does no error checking on item and subitem parameters, notice 
  95 // that subitem 0 means the whole item so there is no way to retrieve the 
  96 // rectangle of the first subitem using this function, in particular notice 
  97 // that the index is *not* 1-based, in spite of what MSDN says 
  99 wxGetListCtrlSubItemRect(HWND hwnd
, int item
, int subitem
, int flags
, RECT
& rect
) 
 103     return ::SendMessage(hwnd
, LVM_GETSUBITEMRECT
, item
, (LPARAM
)&rect
) != 0; 
 107 wxGetListCtrlItemRect(HWND hwnd
, int item
, int flags
, RECT
& rect
) 
 109     return wxGetListCtrlSubItemRect(hwnd
, item
, 0, flags
, rect
); 
 112 } // anonymous namespace 
 114 // ---------------------------------------------------------------------------- 
 115 // private helper classes 
 116 // ---------------------------------------------------------------------------- 
 118 // We have to handle both fooW and fooA notifications in several cases 
 119 // because of broken comctl32.dll and/or unicows.dll. This class is used to 
 120 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or 
 121 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed 
 122 // by wxConvertToMSWListItem(). 
 124     #define LV_ITEM_NATIVE  LV_ITEMW 
 125     #define LV_ITEM_OTHER   LV_ITEMA 
 127     #define LV_CONV_TO_WX   cMB2WX 
 128     #define LV_CONV_BUF     wxMB2WXbuf 
 130     #define LV_ITEM_NATIVE  LV_ITEMA 
 131     #define LV_ITEM_OTHER   LV_ITEMW 
 133     #define LV_CONV_TO_WX   cWC2WX 
 134     #define LV_CONV_BUF     wxWC2WXbuf 
 135 #endif // Unicode/ANSI 
 140     // default ctor, use Init() later 
 141     wxLV_ITEM() { m_buf 
= NULL
; m_pItem 
= NULL
; } 
 143     // init without conversion 
 144     void Init(LV_ITEM_NATIVE
& item
) 
 146         wxASSERT_MSG( !m_pItem
, wxT("Init() called twice?") ); 
 151     // init with conversion 
 152     void Init(const LV_ITEM_OTHER
& item
) 
 154         // avoid unnecessary dynamic memory allocation, jjust make m_pItem 
 155         // point to our own m_item 
 157         // memcpy() can't work if the struct sizes are different 
 158         wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
), 
 159                                CodeCantWorkIfDiffSizes
); 
 161         memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
)); 
 163         // convert text from ANSI to Unicod if necessary 
 164         if ( (item
.mask 
& LVIF_TEXT
) && item
.pszText 
) 
 166             m_buf 
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
)); 
 167             m_item
.pszText 
= (wxChar 
*)m_buf
->data(); 
 171     // ctor without conversion 
 172     wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { } 
 174     // ctor with conversion 
 175     wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
) 
 180     ~wxLV_ITEM() { delete m_buf
; } 
 182     // conversion to the real LV_ITEM 
 183     operator LV_ITEM_NATIVE
&() const { return *m_pItem
; } 
 188     LV_ITEM_NATIVE 
*m_pItem
; 
 189     LV_ITEM_NATIVE m_item
; 
 191     wxDECLARE_NO_COPY_CLASS(wxLV_ITEM
); 
 194 /////////////////////////////////////////////////////// 
 196 // The MSW version had problems with SetTextColour() et 
 197 // al as the wxListItemAttr's were stored keyed on the 
 198 // item index. If a item was inserted anywhere but the end 
 199 // of the list the text attributes (colour etc) for 
 200 // the following items were out of sync. 
 203 // Under MSW the only way to associate data with a List 
 204 // item independent of its position in the list is to 
 205 // store a pointer to it in its lParam attribute. However 
 206 // user programs are already using this (via the 
 207 // SetItemData() GetItemData() calls). 
 209 // However what we can do is store a pointer to a 
 210 // structure which contains the attributes we want *and* 
 211 // a lParam -- and this is what wxMSWListItemData does. 
 213 // To conserve memory, a wxMSWListItemData is 
 214 // only allocated for a LV_ITEM if text attributes or 
 215 // user data(lparam) are being set. 
 216 class wxMSWListItemData
 
 219    wxMSWListItemData() : attr(NULL
), lParam(0) {} 
 220    ~wxMSWListItemData() { delete attr
; } 
 222     wxListItemAttr 
*attr
; 
 223     LPARAM lParam
; // real user data 
 225     wxDECLARE_NO_COPY_CLASS(wxMSWListItemData
); 
 228 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
) 
 229     EVT_PAINT(wxListCtrl::OnPaint
) 
 232 // ============================================================================ 
 234 // ============================================================================ 
 236 // ---------------------------------------------------------------------------- 
 237 // wxListCtrl construction 
 238 // ---------------------------------------------------------------------------- 
 240 void wxListCtrl::Init() 
 244     m_imageListState 
= NULL
; 
 245     m_ownsImageListNormal 
= 
 246     m_ownsImageListSmall 
= 
 247     m_ownsImageListState 
= false; 
 253     m_hasAnyAttr 
= false; 
 256 bool wxListCtrl::Create(wxWindow 
*parent
, 
 261                         const wxValidator
& validator
, 
 262                         const wxString
& name
) 
 264     if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) ) 
 267     if ( !MSWCreateControl(WC_LISTVIEW
, wxEmptyString
, pos
, size
) ) 
 270     // explicitly say that we want to use Unicode because otherwise we get ANSI 
 271     // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build 
 272     wxSetCCUnicodeFormat(GetHwnd()); 
 274     // We must set the default text colour to the system/theme color, otherwise 
 275     // GetTextColour will always return black 
 276     SetTextColour(GetDefaultAttributes().colFg
); 
 278     if ( InReportView() ) 
 279         MSWSetExListStyles(); 
 284 void wxListCtrl::MSWSetExListStyles() 
 286     // for comctl32.dll v 4.70+ we want to have some non default extended 
 287     // styles because it's prettier (and also because wxGTK does it like this) 
 288     if ( wxApp::GetComCtl32Version() >= 470 ) 
 292             GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 0, 
 293             // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's 
 294             // not defined in the SDK headers 
 295 #ifdef LVS_EX_LABELTIP 
 298             LVS_EX_FULLROWSELECT 
| 
 299             LVS_EX_SUBITEMIMAGES 
| 
 300             // normally this should be governed by a style as it's probably not 
 301             // always appropriate, but we don't have any free styles left and 
 302             // it seems better to enable it by default than disable 
 303             LVS_EX_HEADERDRAGDROP
 
 308 WXDWORD 
wxListCtrl::MSWGetStyle(long style
, WXDWORD 
*exstyle
) const 
 310     WXDWORD wstyle 
= wxControl::MSWGetStyle(style
, exstyle
); 
 312     wstyle 
|= LVS_SHAREIMAGELISTS 
| LVS_SHOWSELALWAYS
; 
 317     #define MAP_MODE_STYLE(wx, ms)                                            \ 
 318         if ( style & (wx) ) { wstyle |= (ms); nModes++; } 
 319 #else // !wxDEBUG_LEVEL 
 320     #define MAP_MODE_STYLE(wx, ms)                                            \ 
 321         if ( style & (wx) ) wstyle |= (ms); 
 322 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL 
 324     MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
) 
 325     MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
) 
 326     MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
) 
 327     MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
) 
 329     wxASSERT_MSG( nModes 
== 1, 
 330                   wxT("wxListCtrl style should have exactly one mode bit set") ); 
 332 #undef MAP_MODE_STYLE 
 334     if ( style 
& wxLC_ALIGN_LEFT 
) 
 335         wstyle 
|= LVS_ALIGNLEFT
; 
 337     if ( style 
& wxLC_ALIGN_TOP 
) 
 338         wstyle 
|= LVS_ALIGNTOP
; 
 340     if ( style 
& wxLC_AUTOARRANGE 
) 
 341         wstyle 
|= LVS_AUTOARRANGE
; 
 343     if ( style 
& wxLC_NO_SORT_HEADER 
) 
 344         wstyle 
|= LVS_NOSORTHEADER
; 
 346     if ( style 
& wxLC_NO_HEADER 
) 
 347         wstyle 
|= LVS_NOCOLUMNHEADER
; 
 349     if ( style 
& wxLC_EDIT_LABELS 
) 
 350         wstyle 
|= LVS_EDITLABELS
; 
 352     if ( style 
& wxLC_SINGLE_SEL 
) 
 353         wstyle 
|= LVS_SINGLESEL
; 
 355     if ( style 
& wxLC_SORT_ASCENDING 
) 
 357         wstyle 
|= LVS_SORTASCENDING
; 
 359         wxASSERT_MSG( !(style 
& wxLC_SORT_DESCENDING
), 
 360                       wxT("can't sort in ascending and descending orders at once") ); 
 362     else if ( style 
& wxLC_SORT_DESCENDING 
) 
 363         wstyle 
|= LVS_SORTDESCENDING
; 
 365 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) 
 366     if ( style 
& wxLC_VIRTUAL 
) 
 368         int ver 
= wxApp::GetComCtl32Version(); 
 371             wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."), 
 372                         ver 
/ 100, ver 
% 100); 
 375         wstyle 
|= LVS_OWNERDATA
; 
 377 #endif // ancient cygwin 
 382 void wxListCtrl::UpdateStyle() 
 386         // The new window view style 
 387         DWORD dwStyleNew 
= MSWGetStyle(m_windowStyle
, NULL
); 
 389         // some styles are not returned by MSWGetStyle() 
 391             dwStyleNew 
|= WS_VISIBLE
; 
 393         // Get the current window style. 
 394         DWORD dwStyleOld 
= ::GetWindowLong(GetHwnd(), GWL_STYLE
); 
 396         // we don't have wxVSCROLL style, but the list control may have it, 
 397         // don't change it then 
 398         dwStyleNew 
|= dwStyleOld 
& (WS_HSCROLL 
| WS_VSCROLL
); 
 400         // Only set the window style if the view bits have changed. 
 401         if ( dwStyleOld 
!= dwStyleNew 
) 
 403             ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
); 
 405             // if we switched to the report view, set the extended styles for 
 407             if ( !(dwStyleOld 
& LVS_REPORT
) && (dwStyleNew 
& LVS_REPORT
) ) 
 408                 MSWSetExListStyles(); 
 413 void wxListCtrl::FreeAllInternalData() 
 415     const unsigned count 
= m_internalData
.size(); 
 416     for ( unsigned n 
= 0; n 
< count
; n
++ ) 
 417         delete m_internalData
[n
]; 
 419     m_internalData
.clear(); 
 422 void wxListCtrl::DeleteEditControl() 
 426         m_textCtrl
->UnsubclassWin(); 
 427         m_textCtrl
->SetHWND(0); 
 428         wxDELETE(m_textCtrl
); 
 432 wxListCtrl::~wxListCtrl() 
 434     FreeAllInternalData(); 
 438     if (m_ownsImageListNormal
) 
 439         delete m_imageListNormal
; 
 440     if (m_ownsImageListSmall
) 
 441         delete m_imageListSmall
; 
 442     if (m_ownsImageListState
) 
 443         delete m_imageListState
; 
 446 // ---------------------------------------------------------------------------- 
 447 // set/get/change style 
 448 // ---------------------------------------------------------------------------- 
 450 // Add or remove a single window style 
 451 void wxListCtrl::SetSingleStyle(long style
, bool add
) 
 453     long flag 
= GetWindowStyleFlag(); 
 455     // Get rid of conflicting styles 
 458         if ( style 
& wxLC_MASK_TYPE
) 
 459             flag 
= flag 
& ~wxLC_MASK_TYPE
; 
 460         if ( style 
& wxLC_MASK_ALIGN 
) 
 461             flag 
= flag 
& ~wxLC_MASK_ALIGN
; 
 462         if ( style 
& wxLC_MASK_SORT 
) 
 463             flag 
= flag 
& ~wxLC_MASK_SORT
; 
 471     SetWindowStyleFlag(flag
); 
 474 // Set the whole window style 
 475 void wxListCtrl::SetWindowStyleFlag(long flag
) 
 477     if ( flag 
!= m_windowStyle 
) 
 479         wxControl::SetWindowStyleFlag(flag
); 
 487 // ---------------------------------------------------------------------------- 
 489 // ---------------------------------------------------------------------------- 
 491 /* static */ wxVisualAttributes
 
 492 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
) 
 494     wxVisualAttributes attrs 
= GetCompositeControlsDefaultAttributes(variant
); 
 496     // common controls have their own default font 
 497     attrs
.font 
= wxGetCCDefaultFont(); 
 502 // Sets the foreground, i.e. text, colour 
 503 bool wxListCtrl::SetForegroundColour(const wxColour
& col
) 
 505     if ( !wxWindow::SetForegroundColour(col
) ) 
 508     ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
)); 
 513 // Sets the background colour 
 514 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
) 
 516     if ( !wxWindow::SetBackgroundColour(col
) ) 
 519     // we set the same colour for both the "empty" background and the items 
 521     COLORREF color 
= wxColourToRGB(col
); 
 522     ListView_SetBkColor(GetHwnd(), color
); 
 523     ListView_SetTextBkColor(GetHwnd(), color
); 
 528 // Gets information about this column 
 529 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const 
 534     lvCol
.mask 
= LVCF_WIDTH
; 
 536     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 538         lvCol
.mask 
|= LVCF_TEXT
; 
 539         lvCol
.pszText 
= new wxChar
[513]; 
 540         lvCol
.cchTextMax 
= 512; 
 543     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 545         lvCol
.mask 
|= LVCF_FMT
; 
 548     if ( item
.m_mask 
& wxLIST_MASK_IMAGE 
) 
 550         lvCol
.mask 
|= LVCF_IMAGE
; 
 553     bool success 
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0; 
 555     //  item.m_subItem = lvCol.iSubItem; 
 556     item
.m_width 
= lvCol
.cx
; 
 558     if ( (item
.m_mask 
& wxLIST_MASK_TEXT
) && lvCol
.pszText 
) 
 560         item
.m_text 
= lvCol
.pszText
; 
 561         delete[] lvCol
.pszText
; 
 564     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 566         switch (lvCol
.fmt 
& LVCFMT_JUSTIFYMASK
) { 
 568                 item
.m_format 
= wxLIST_FORMAT_LEFT
; 
 571                 item
.m_format 
= wxLIST_FORMAT_RIGHT
; 
 574                 item
.m_format 
= wxLIST_FORMAT_CENTRE
; 
 577                 item
.m_format 
= -1;  // Unknown? 
 582     // the column images were not supported in older versions but how to check 
 583     // for this? we can't use _WIN32_IE because we always define it to a very 
 584     // high value, so see if another symbol which is only defined starting from 
 585     // comctl32.dll 4.70 is available 
 586 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 
 587     if ( item
.m_mask 
& wxLIST_MASK_IMAGE 
) 
 589         item
.m_image 
= lvCol
.iImage
; 
 591 #endif // LVCOLUMN::iImage exists 
 596 // Sets information about this column 
 597 bool wxListCtrl::SetColumn(int col
, const wxListItem
& item
) 
 600     wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
); 
 602     return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0; 
 605 // Gets the column width 
 606 int wxListCtrl::GetColumnWidth(int col
) const 
 608     return ListView_GetColumnWidth(GetHwnd(), col
); 
 611 // Sets the column width 
 612 bool wxListCtrl::SetColumnWidth(int col
, int width
) 
 614     if ( m_windowStyle 
& wxLC_LIST 
) 
 617     if ( width 
== wxLIST_AUTOSIZE
) 
 618         width 
= LVSCW_AUTOSIZE
; 
 619     else if ( width 
== wxLIST_AUTOSIZE_USEHEADER
) 
 620         width 
= LVSCW_AUTOSIZE_USEHEADER
; 
 622     return ListView_SetColumnWidth(GetHwnd(), col
, width
) != 0; 
 625 // ---------------------------------------------------------------------------- 
 627 // ---------------------------------------------------------------------------- 
 629 int wxListCtrl::GetColumnIndexFromOrder(int order
) const 
 631     const int numCols 
= GetColumnCount(); 
 632     wxCHECK_MSG( order 
>= 0 && order 
< numCols
, -1, 
 633                 wxT("Column position out of bounds") ); 
 635     wxArrayInt 
indexArray(numCols
); 
 636     if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) ) 
 639     return indexArray
[order
]; 
 642 int wxListCtrl::GetColumnOrder(int col
) const 
 644     const int numCols 
= GetColumnCount(); 
 645     wxASSERT_MSG( col 
>= 0 && col 
< numCols
, wxT("Column index out of bounds") ); 
 647     wxArrayInt 
indexArray(numCols
); 
 648     if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) ) 
 651     for ( int pos 
= 0; pos 
< numCols
; pos
++ ) 
 653         if ( indexArray
[pos
] == col 
) 
 657     wxFAIL_MSG( wxT("no column with with given order?") ); 
 662 // Gets the column order for all columns 
 663 wxArrayInt 
wxListCtrl::GetColumnsOrder() const 
 665     const int numCols 
= GetColumnCount(); 
 667     wxArrayInt 
orders(numCols
); 
 668     if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) ) 
 674 // Sets the column order for all columns 
 675 bool wxListCtrl::SetColumnsOrder(const wxArrayInt
& orders
) 
 677     const int numCols 
= GetColumnCount(); 
 679     wxCHECK_MSG( orders
.size() == (size_t)numCols
, false, 
 680                     wxT("wrong number of elements in column orders array") ); 
 682     return ListView_SetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) != 0; 
 686 // Gets the number of items that can fit vertically in the 
 687 // visible area of the list control (list or report view) 
 688 // or the total number of items in the list control (icon 
 689 // or small icon view) 
 690 int wxListCtrl::GetCountPerPage() const 
 692     return ListView_GetCountPerPage(GetHwnd()); 
 695 // Gets the edit control for editing labels. 
 696 wxTextCtrl
* wxListCtrl::GetEditControl() const 
 698     // first check corresponds to the case when the label editing was started 
 699     // by user and hence m_textCtrl wasn't created by EditLabel() at all, while 
 700     // the second case corresponds to us being called from inside EditLabel() 
 701     // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this 
 702     // case EditLabel() did create the control but it didn't have an HWND to 
 703     // initialize it with yet 
 704     if ( !m_textCtrl 
|| !m_textCtrl
->GetHWND() ) 
 706         HWND hwndEdit 
= ListView_GetEditControl(GetHwnd()); 
 709             wxListCtrl 
* const self 
= const_cast<wxListCtrl 
*>(this); 
 712                 self
->m_textCtrl 
= new wxTextCtrl
; 
 713             self
->InitEditControl((WXHWND
)hwndEdit
); 
 720 // Gets information about the item 
 721 bool wxListCtrl::GetItem(wxListItem
& info
) const 
 724     wxZeroMemory(lvItem
); 
 726     lvItem
.iItem 
= info
.m_itemId
; 
 727     lvItem
.iSubItem 
= info
.m_col
; 
 729     if ( info
.m_mask 
& wxLIST_MASK_TEXT 
) 
 731         lvItem
.mask 
|= LVIF_TEXT
; 
 732         lvItem
.pszText 
= new wxChar
[513]; 
 733         lvItem
.cchTextMax 
= 512; 
 737         lvItem
.pszText 
= NULL
; 
 740     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
 741         lvItem
.mask 
|= LVIF_PARAM
; 
 743     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
 744         lvItem
.mask 
|= LVIF_IMAGE
; 
 746     if ( info
.m_mask 
& wxLIST_MASK_STATE 
) 
 748         lvItem
.mask 
|= LVIF_STATE
; 
 749         wxConvertToMSWFlags(0, info
.m_stateMask
, lvItem
); 
 752     bool success 
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0; 
 755         wxLogError(_("Couldn't retrieve information about list control item %d."), 
 760         // give NULL as hwnd as we already have everything we need 
 761         wxConvertFromMSWListItem(NULL
, info
, lvItem
); 
 765         delete[] lvItem
.pszText
; 
 770 // Sets information about the item 
 771 bool wxListCtrl::SetItem(wxListItem
& info
) 
 773     const long id 
= info
.GetId(); 
 774     wxCHECK_MSG( id 
>= 0 && id 
< GetItemCount(), false, 
 775                  wxT("invalid item index in SetItem") ); 
 778     wxConvertToMSWListItem(this, info
, item
); 
 780     // we never update the lParam if it contains our pointer 
 781     // to the wxMSWListItemData structure 
 782     item
.mask 
&= ~LVIF_PARAM
; 
 784     // check if setting attributes or lParam 
 785     if ( info
.HasAttributes() || (info
.m_mask 
& wxLIST_MASK_DATA
) ) 
 787         // get internal item data 
 788         wxMSWListItemData 
*data 
= MSWGetItemData(id
); 
 792             // need to allocate the internal data object 
 793             data 
= new wxMSWListItemData
; 
 794             m_internalData
.push_back(data
); 
 795             item
.lParam 
= (LPARAM
) data
; 
 796             item
.mask 
|= LVIF_PARAM
; 
 801         if ( info
.m_mask 
& wxLIST_MASK_DATA 
) 
 802             data
->lParam 
= info
.m_data
; 
 805         if ( info
.HasAttributes() ) 
 807             const wxListItemAttr
& attrNew 
= *info
.GetAttributes(); 
 809             // don't overwrite the already set attributes if we have them 
 811                 data
->attr
->AssignFrom(attrNew
); 
 813                 data
->attr 
= new wxListItemAttr(attrNew
); 
 818     // we could be changing only the attribute in which case we don't need to 
 819     // call ListView_SetItem() at all 
 822         if ( !ListView_SetItem(GetHwnd(), &item
) ) 
 824             wxLogDebug(wxT("ListView_SetItem() failed")); 
 830     // we need to update the item immediately to show the new image 
 831     bool updateNow 
= (info
.m_mask 
& wxLIST_MASK_IMAGE
) != 0; 
 833     // check whether it has any custom attributes 
 834     if ( info
.HasAttributes() ) 
 838         // if the colour has changed, we must redraw the item 
 844         // we need this to make the change visible right now 
 845         RefreshItem(item
.iItem
); 
 851 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
) 
 855     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 856     info
.m_itemId 
= index
; 
 860         info
.m_image 
= imageId
; 
 861         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
 863     return SetItem(info
); 
 867 // Gets the item state 
 868 int wxListCtrl::GetItemState(long item
, long stateMask
) const 
 872     info
.m_mask 
= wxLIST_MASK_STATE
; 
 873     info
.m_stateMask 
= stateMask
; 
 874     info
.m_itemId 
= item
; 
 882 // Sets the item state 
 883 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
) 
 885     // NB: don't use SetItem() here as it doesn't work with the virtual list 
 888     wxZeroMemory(lvItem
); 
 890     wxConvertToMSWFlags(state
, stateMask
, lvItem
); 
 892     const bool changingFocus 
= (stateMask 
& wxLIST_STATE_FOCUSED
) && 
 893                                     (state 
& wxLIST_STATE_FOCUSED
); 
 895     // for the virtual list controls we need to refresh the previously focused 
 896     // item manually when changing focus without changing selection 
 897     // programmatically because otherwise it keeps its focus rectangle until 
 898     // next repaint (yet another comctl32 bug) 
 900     if ( IsVirtual() && changingFocus 
) 
 902         focusOld 
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
); 
 909     if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
, 
 910                         (WPARAM
)item
, (LPARAM
)&lvItem
) ) 
 912         wxLogLastError(wxT("ListView_SetItemState")); 
 917     if ( focusOld 
!= -1 ) 
 919         // no need to refresh the item if it was previously selected, it would 
 920         // only result in annoying flicker 
 921         if ( !(GetItemState(focusOld
, 
 922                             wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) ) 
 924             RefreshItem(focusOld
); 
 928     // we expect the selection anchor, i.e. the item from which multiple 
 929     // selection (such as performed with e.g. Shift-arrows) starts, to be the 
 930     // same as the currently focused item but the native control doesn't update 
 931     // it when we change focus and leaves at the last item it set itself focus 
 932     // to, so do it explicitly 
 933     if ( changingFocus 
&& !HasFlag(wxLC_SINGLE_SEL
) ) 
 935         ListView_SetSelectionMark(GetHwnd(), item
); 
 941 // Sets the item image 
 942 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
)) 
 944     return SetItemColumnImage(item
, 0, image
); 
 947 // Sets the item image 
 948 bool wxListCtrl::SetItemColumnImage(long item
, long column
, int image
) 
 952     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
 953     info
.m_image 
= image
; 
 954     info
.m_itemId 
= item
; 
 957     return SetItem(info
); 
 960 // Gets the item text 
 961 wxString 
wxListCtrl::GetItemText(long item
, int col
) const 
 965     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 966     info
.m_itemId 
= item
; 
 970         return wxEmptyString
; 
 974 // Sets the item text 
 975 void wxListCtrl::SetItemText(long item
, const wxString
& str
) 
 979     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 980     info
.m_itemId 
= item
; 
 986 // Gets the internal item data 
 987 wxMSWListItemData 
*wxListCtrl::MSWGetItemData(long itemId
) const 
 990     it
.mask 
= LVIF_PARAM
; 
 993     if ( !ListView_GetItem(GetHwnd(), &it
) ) 
 996     return (wxMSWListItemData 
*) it
.lParam
; 
 999 // Gets the item data 
1000 wxUIntPtr 
wxListCtrl::GetItemData(long item
) const 
1004     info
.m_mask 
= wxLIST_MASK_DATA
; 
1005     info
.m_itemId 
= item
; 
1012 // Sets the item data 
1013 bool wxListCtrl::SetItemPtrData(long item
, wxUIntPtr data
) 
1017     info
.m_mask 
= wxLIST_MASK_DATA
; 
1018     info
.m_itemId 
= item
; 
1021     return SetItem(info
); 
1024 wxRect 
wxListCtrl::GetViewRect() const 
1028     // ListView_GetViewRect() can only be used in icon and small icon views 
1029     // (this is documented in MSDN and, indeed, it returns bogus results in 
1030     // report view, at least with comctl32.dll v6 under Windows 2003) 
1031     if ( HasFlag(wxLC_ICON 
| wxLC_SMALL_ICON
) ) 
1034         if ( !ListView_GetViewRect(GetHwnd(), &rc
) ) 
1036             wxLogDebug(wxT("ListView_GetViewRect() failed.")); 
1041         wxCopyRECTToRect(rc
, rect
); 
1043     else if ( HasFlag(wxLC_REPORT
) ) 
1045         const long count 
= GetItemCount(); 
1048             GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count 
- 1), rect
); 
1050             // extend the rectangle to start at the top (we include the column 
1051             // headers, if any, for compatibility with the generic version) 
1052             rect
.height 
+= rect
.y
; 
1058         wxFAIL_MSG( wxT("not implemented in this mode") ); 
1064 // Gets the item rectangle 
1065 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const 
1067     return GetSubItemRect( item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
) ; 
1070 bool wxListCtrl::GetSubItemRect(long item
, long subItem
, wxRect
& rect
, int code
) const 
1072     // ListView_GetSubItemRect() doesn't do subItem error checking and returns 
1073     // true even for the out of range values of it (even if the results are 
1074     // completely bogus in this case), so we check item validity ourselves 
1075     wxCHECK_MSG( subItem 
== wxLIST_GETSUBITEMRECT_WHOLEITEM 
|| 
1076                     (subItem 
>= 0 && subItem 
< GetColumnCount()), 
1077                  false, wxT("invalid sub item index") ); 
1079     // use wxCHECK_MSG against "item" too, for coherency with the generic implementation: 
1080     wxCHECK_MSG( item 
>= 0 && item 
< GetItemCount(), false, 
1081                  wxT("invalid item in GetSubItemRect") ); 
1084     if ( code 
== wxLIST_RECT_BOUNDS 
) 
1085         codeWin 
= LVIR_BOUNDS
; 
1086     else if ( code 
== wxLIST_RECT_ICON 
) 
1087         codeWin 
= LVIR_ICON
; 
1088     else if ( code 
== wxLIST_RECT_LABEL 
) 
1089         codeWin 
= LVIR_LABEL
; 
1092         wxFAIL_MSG( wxT("incorrect code in GetItemRect() / GetSubItemRect()") ); 
1093         codeWin 
= LVIR_BOUNDS
; 
1097     if ( !wxGetListCtrlSubItemRect
 
1101             subItem 
== wxLIST_GETSUBITEMRECT_WHOLEITEM 
? 0 : subItem
, 
1109     wxCopyRECTToRect(rectWin
, rect
); 
1111     // there is no way to retrieve the first sub item bounding rectangle using 
1112     // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to 
1113     // truncate it at first column ourselves 
1114     if ( subItem 
== 0 && code 
== wxLIST_RECT_BOUNDS 
) 
1115         rect
.width 
= GetColumnWidth(0); 
1123 // Gets the item position 
1124 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const 
1128     bool success 
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0); 
1130     pos
.x 
= pt
.x
; pos
.y 
= pt
.y
; 
1134 // Sets the item position. 
1135 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
) 
1137     return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0); 
1140 // Gets the number of items in the list control 
1141 int wxListCtrl::GetItemCount() const 
1146 wxSize 
wxListCtrl::GetItemSpacing() const 
1148     const int spacing 
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
)); 
1150     return wxSize(LOWORD(spacing
), HIWORD(spacing
)); 
1153 #if WXWIN_COMPATIBILITY_2_6 
1155 int wxListCtrl::GetItemSpacing(bool isSmall
) const 
1157     return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
); 
1160 #endif // WXWIN_COMPATIBILITY_2_6 
1162 void wxListCtrl::SetItemTextColour( long item
, const wxColour 
&col 
) 
1165     info
.m_itemId 
= item
; 
1166     info
.SetTextColour( col 
); 
1170 wxColour 
wxListCtrl::GetItemTextColour( long item 
) const 
1173     wxMSWListItemData 
*data 
= MSWGetItemData(item
); 
1174     if ( data 
&& data
->attr 
) 
1175         col 
= data
->attr
->GetTextColour(); 
1180 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour 
&col 
) 
1183     info
.m_itemId 
= item
; 
1184     info
.SetBackgroundColour( col 
); 
1188 wxColour 
wxListCtrl::GetItemBackgroundColour( long item 
) const 
1191     wxMSWListItemData 
*data 
= MSWGetItemData(item
); 
1192     if ( data 
&& data
->attr 
) 
1193         col 
= data
->attr
->GetBackgroundColour(); 
1198 void wxListCtrl::SetItemFont( long item
, const wxFont 
&f 
) 
1201     info
.m_itemId 
= item
; 
1206 wxFont 
wxListCtrl::GetItemFont( long item 
) const 
1209     wxMSWListItemData 
*data 
= MSWGetItemData(item
); 
1210     if ( data 
&& data
->attr 
) 
1211         f 
= data
->attr
->GetFont(); 
1216 // Gets the number of selected items in the list control 
1217 int wxListCtrl::GetSelectedItemCount() const 
1219     return ListView_GetSelectedCount(GetHwnd()); 
1222 // Gets the text colour of the listview 
1223 wxColour 
wxListCtrl::GetTextColour() const 
1225     COLORREF ref 
= ListView_GetTextColor(GetHwnd()); 
1226     wxColour 
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
)); 
1230 // Sets the text colour of the listview 
1231 void wxListCtrl::SetTextColour(const wxColour
& col
) 
1233     ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue())); 
1236 // Gets the index of the topmost visible item when in 
1237 // list or report view 
1238 long wxListCtrl::GetTopItem() const 
1240     return (long) ListView_GetTopIndex(GetHwnd()); 
1243 // Searches for an item, starting from 'item'. 
1244 // 'geometry' is one of 
1245 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. 
1246 // 'state' is a state bit flag, one or more of 
1247 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. 
1248 // item can be -1 to find the first item that matches the 
1250 // Returns the item or -1 if unsuccessful. 
1251 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const 
1255     if ( geom 
== wxLIST_NEXT_ABOVE 
) 
1256         flags 
|= LVNI_ABOVE
; 
1257     if ( geom 
== wxLIST_NEXT_ALL 
) 
1259     if ( geom 
== wxLIST_NEXT_BELOW 
) 
1260         flags 
|= LVNI_BELOW
; 
1261     if ( geom 
== wxLIST_NEXT_LEFT 
) 
1262         flags 
|= LVNI_TOLEFT
; 
1263     if ( geom 
== wxLIST_NEXT_RIGHT 
) 
1264         flags 
|= LVNI_TORIGHT
; 
1266     if ( state 
& wxLIST_STATE_CUT 
) 
1268     if ( state 
& wxLIST_STATE_DROPHILITED 
) 
1269         flags 
|= LVNI_DROPHILITED
; 
1270     if ( state 
& wxLIST_STATE_FOCUSED 
) 
1271         flags 
|= LVNI_FOCUSED
; 
1272     if ( state 
& wxLIST_STATE_SELECTED 
) 
1273         flags 
|= LVNI_SELECTED
; 
1275     return (long) ListView_GetNextItem(GetHwnd(), item
, flags
); 
1279 wxImageList 
*wxListCtrl::GetImageList(int which
) const 
1281     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1283         return m_imageListNormal
; 
1285     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1287         return m_imageListSmall
; 
1289     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1291         return m_imageListState
; 
1296 void wxListCtrl::SetImageList(wxImageList 
*imageList
, int which
) 
1299     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1301         flags 
= LVSIL_NORMAL
; 
1302         if (m_ownsImageListNormal
) delete m_imageListNormal
; 
1303         m_imageListNormal 
= imageList
; 
1304         m_ownsImageListNormal 
= false; 
1306     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1308         flags 
= LVSIL_SMALL
; 
1309         if (m_ownsImageListSmall
) delete m_imageListSmall
; 
1310         m_imageListSmall 
= imageList
; 
1311         m_ownsImageListSmall 
= false; 
1313     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1315         flags 
= LVSIL_STATE
; 
1316         if (m_ownsImageListState
) delete m_imageListState
; 
1317         m_imageListState 
= imageList
; 
1318         m_ownsImageListState 
= false; 
1320     (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList 
? imageList
->GetHIMAGELIST() : 0, flags
); 
1323 void wxListCtrl::AssignImageList(wxImageList 
*imageList
, int which
) 
1325     SetImageList(imageList
, which
); 
1326     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1327         m_ownsImageListNormal 
= true; 
1328     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1329         m_ownsImageListSmall 
= true; 
1330     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1331         m_ownsImageListState 
= true; 
1334 // ---------------------------------------------------------------------------- 
1336 // ---------------------------------------------------------------------------- 
1338 // Arranges the items 
1339 bool wxListCtrl::Arrange(int flag
) 
1342     if ( flag 
== wxLIST_ALIGN_LEFT 
) 
1343         code 
= LVA_ALIGNLEFT
; 
1344     else if ( flag 
== wxLIST_ALIGN_TOP 
) 
1345         code 
= LVA_ALIGNTOP
; 
1346     else if ( flag 
== wxLIST_ALIGN_DEFAULT 
) 
1348     else if ( flag 
== wxLIST_ALIGN_SNAP_TO_GRID 
) 
1349         code 
= LVA_SNAPTOGRID
; 
1351     return (ListView_Arrange(GetHwnd(), code
) != 0); 
1355 bool wxListCtrl::DeleteItem(long item
) 
1357     if ( !ListView_DeleteItem(GetHwnd(), (int) item
) ) 
1359         wxLogLastError(wxT("ListView_DeleteItem")); 
1364     wxASSERT_MSG( m_count 
== ListView_GetItemCount(GetHwnd()), 
1365                   wxT("m_count should match ListView_GetItemCount")); 
1367     // the virtual list control doesn't refresh itself correctly, help it 
1370         // we need to refresh all the lines below the one which was deleted 
1372         if ( item 
> 0 && GetItemCount() ) 
1374             GetItemRect(item 
- 1, rectItem
); 
1379             rectItem
.height 
= 0; 
1382         wxRect rectWin 
= GetRect(); 
1383         rectWin
.height 
= rectWin
.GetBottom() - rectItem
.GetBottom(); 
1384         rectWin
.y 
= rectItem
.GetBottom(); 
1386         RefreshRect(rectWin
); 
1392 // Deletes all items 
1393 bool wxListCtrl::DeleteAllItems() 
1395     // Calling ListView_DeleteAllItems() will always generate an event but we 
1396     // shouldn't do it if the control is empty 
1397     return !GetItemCount() || ListView_DeleteAllItems(GetHwnd()) != 0; 
1400 // Deletes all items 
1401 bool wxListCtrl::DeleteAllColumns() 
1403     while ( m_colCount 
> 0 ) 
1405         if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) 
1407             wxLogLastError(wxT("ListView_DeleteColumn")); 
1415     wxASSERT_MSG( m_colCount 
== 0, wxT("no columns should be left") ); 
1421 bool wxListCtrl::DeleteColumn(int col
) 
1423     bool success 
= (ListView_DeleteColumn(GetHwnd(), col
) != 0); 
1425     if ( success 
&& (m_colCount 
> 0) ) 
1430 // Clears items, and columns if there are any. 
1431 void wxListCtrl::ClearAll() 
1434     if ( m_colCount 
> 0 ) 
1438 void wxListCtrl::InitEditControl(WXHWND hWnd
) 
1440     m_textCtrl
->SetHWND(hWnd
); 
1441     m_textCtrl
->SubclassWin(hWnd
); 
1442     m_textCtrl
->SetParent(this); 
1444     // we must disallow TABbing away from the control while the edit contol is 
1445     // shown because this leaves it in some strange state (just try removing 
1446     // this line and then pressing TAB while editing an item in  listctrl 
1448     m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
); 
1451 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
) 
1453     wxCHECK_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)), NULL
, 
1454                   "control used for label editing must be a wxTextCtrl" ); 
1456     // ListView_EditLabel requires that the list has focus. 
1459     // create m_textCtrl here before calling ListView_EditLabel() because it 
1460     // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and 
1461     // the user handler for it can call GetEditControl() resulting in an on 
1462     // demand creation of a stock wxTextCtrl instead of the control of a 
1463     // (possibly) custom wxClassInfo 
1464     DeleteEditControl(); 
1465     m_textCtrl 
= (wxTextCtrl 
*)textControlClass
->CreateObject(); 
1467     WXHWND hWnd 
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
); 
1470         // failed to start editing 
1471         wxDELETE(m_textCtrl
); 
1476     // if GetEditControl() hasn't been called, we need to initialize the edit 
1477     // control ourselves 
1478     if ( !m_textCtrl
->GetHWND() ) 
1479         InitEditControl(hWnd
); 
1484 // End label editing, optionally cancelling the edit 
1485 bool wxListCtrl::EndEditLabel(bool cancel
) 
1487     // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT 
1488     HWND hwnd 
= ListView_GetEditControl(GetHwnd()); 
1492     // Newer versions of Windows have a special message for cancelling editing, 
1493     // use it if available. 
1494 #ifdef ListView_CancelEditLabel 
1495     if ( cancel 
&& (wxApp::GetComCtl32Version() >= 600) ) 
1497         ListView_CancelEditLabel(GetHwnd()); 
1500 #endif // ListView_CancelEditLabel 
1502         // We shouldn't destroy the control ourselves according to MSDN, which 
1503         // proposes WM_CANCELMODE to do this, but it doesn't seem to work so 
1504         // emulate the corresponding user action instead. 
1505         ::SendMessage(hwnd
, WM_KEYDOWN
, cancel 
? VK_ESCAPE 
: VK_RETURN
, 0); 
1511 // Ensures this item is visible 
1512 bool wxListCtrl::EnsureVisible(long item
) 
1514     return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
; 
1517 // Find an item whose label matches this string, starting from the item after 'start' 
1518 // or the beginning if 'start' is -1. 
1519 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
) 
1521     LV_FINDINFO findInfo
; 
1523     findInfo
.flags 
= LVFI_STRING
; 
1525         findInfo
.flags 
|= LVFI_PARTIAL
; 
1526     findInfo
.psz 
= str
.wx_str(); 
1528     // ListView_FindItem() excludes the first item from search and to look 
1529     // through all the items you need to start from -1 which is unnatural and 
1530     // inconsistent with the generic version - so we adjust the index 
1533     return ListView_FindItem(GetHwnd(), start
, &findInfo
); 
1536 // Find an item whose data matches this data, starting from the item after 
1537 // 'start' or the beginning if 'start' is -1. 
1538 long wxListCtrl::FindItem(long start
, wxUIntPtr data
) 
1540     // we can't use ListView_FindItem() directly as we don't store the data 
1541     // pointer itself in the control but rather our own internal data, so first 
1542     // we need to find the right value to search for (and there can be several 
1544     int idx 
= wxNOT_FOUND
; 
1545     const unsigned count 
= m_internalData
.size(); 
1546     for ( unsigned n 
= 0; n 
< count
; n
++ ) 
1548         if ( m_internalData
[n
]->lParam 
== (LPARAM
)data 
) 
1550             LV_FINDINFO findInfo
; 
1551             findInfo
.flags 
= LVFI_PARAM
; 
1552             findInfo
.lParam 
= (LPARAM
)wxPtrToUInt(m_internalData
[n
]); 
1554             int rc 
= ListView_FindItem(GetHwnd(), start
, &findInfo
); 
1557                 if ( idx 
== wxNOT_FOUND 
|| rc 
< idx 
) 
1560                     if ( idx 
== start 
+ 1 ) 
1562                         // we can stop here, we don't risk finding a closer 
1567                 //else: this item is after the previously found one 
1575 // Find an item nearest this position in the specified direction, starting from 
1576 // the item after 'start' or the beginning if 'start' is -1. 
1577 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
) 
1579     LV_FINDINFO findInfo
; 
1581     findInfo
.flags 
= LVFI_NEARESTXY
; 
1582     findInfo
.pt
.x 
= pt
.x
; 
1583     findInfo
.pt
.y 
= pt
.y
; 
1584     findInfo
.vkDirection 
= VK_RIGHT
; 
1586     if ( direction 
== wxLIST_FIND_UP 
) 
1587         findInfo
.vkDirection 
= VK_UP
; 
1588     else if ( direction 
== wxLIST_FIND_DOWN 
) 
1589         findInfo
.vkDirection 
= VK_DOWN
; 
1590     else if ( direction 
== wxLIST_FIND_LEFT 
) 
1591         findInfo
.vkDirection 
= VK_LEFT
; 
1592     else if ( direction 
== wxLIST_FIND_RIGHT 
) 
1593         findInfo
.vkDirection 
= VK_RIGHT
; 
1595     return ListView_FindItem(GetHwnd(), start
, &findInfo
); 
1598 // Determines which item (if any) is at the specified point, 
1599 // giving details in 'flags' (see wxLIST_HITTEST_... flags above) 
1601 wxListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *ptrSubItem
) const 
1603     LV_HITTESTINFO hitTestInfo
; 
1604     hitTestInfo
.pt
.x 
= (int) point
.x
; 
1605     hitTestInfo
.pt
.y 
= (int) point
.y
; 
1608 #ifdef LVM_SUBITEMHITTEST 
1609     if ( ptrSubItem 
&& wxApp::GetComCtl32Version() >= 470 ) 
1611         item 
= ListView_SubItemHitTest(GetHwnd(), &hitTestInfo
); 
1612         *ptrSubItem 
= hitTestInfo
.iSubItem
; 
1615 #endif // LVM_SUBITEMHITTEST 
1617         item 
= ListView_HitTest(GetHwnd(), &hitTestInfo
); 
1622     if ( hitTestInfo
.flags 
& LVHT_ABOVE 
) 
1623         flags 
|= wxLIST_HITTEST_ABOVE
; 
1624     if ( hitTestInfo
.flags 
& LVHT_BELOW 
) 
1625         flags 
|= wxLIST_HITTEST_BELOW
; 
1626     if ( hitTestInfo
.flags 
& LVHT_TOLEFT 
) 
1627         flags 
|= wxLIST_HITTEST_TOLEFT
; 
1628     if ( hitTestInfo
.flags 
& LVHT_TORIGHT 
) 
1629         flags 
|= wxLIST_HITTEST_TORIGHT
; 
1631     if ( hitTestInfo
.flags 
& LVHT_NOWHERE 
) 
1632         flags 
|= wxLIST_HITTEST_NOWHERE
; 
1634     // note a bug or at least a very strange feature of comtl32.dll (tested 
1635     // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to 
1636     // the right of the item label, ListView_HitTest() returns a combination of 
1637     // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out 
1638     // the bits which don't make sense 
1639     if ( hitTestInfo
.flags 
& LVHT_ONITEMLABEL 
) 
1641         flags 
|= wxLIST_HITTEST_ONITEMLABEL
; 
1643         // do not translate LVHT_ONITEMICON here, as per above 
1647         if ( hitTestInfo
.flags 
& LVHT_ONITEMICON 
) 
1648             flags 
|= wxLIST_HITTEST_ONITEMICON
; 
1649         if ( hitTestInfo
.flags 
& LVHT_ONITEMSTATEICON 
) 
1650             flags 
|= wxLIST_HITTEST_ONITEMSTATEICON
; 
1657 // Inserts an item, returning the index of the new item if successful, 
1659 long wxListCtrl::InsertItem(const wxListItem
& info
) 
1661     wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual controls") ); 
1664     wxConvertToMSWListItem(this, info
, item
); 
1665     item
.mask 
&= ~LVIF_PARAM
; 
1667     // check whether we need to allocate our internal data 
1668     bool needInternalData 
= (info
.m_mask 
& wxLIST_MASK_DATA
) || 
1669                                 info
.HasAttributes(); 
1670     if ( needInternalData 
) 
1672         item
.mask 
|= LVIF_PARAM
; 
1674         wxMSWListItemData 
* const data 
= new wxMSWListItemData
; 
1675         m_internalData
.push_back(data
); 
1676         item
.lParam 
= (LPARAM
)data
; 
1678         if ( info
.m_mask 
& wxLIST_MASK_DATA 
) 
1679             data
->lParam 
= info
.m_data
; 
1681         // check whether it has any custom attributes 
1682         if ( info
.HasAttributes() ) 
1684             // take copy of attributes 
1685             data
->attr 
= new wxListItemAttr(*info
.GetAttributes()); 
1687             // and remember that we have some now... 
1688             m_hasAnyAttr 
= true; 
1692     const long rv 
= ListView_InsertItem(GetHwnd(), & item
); 
1694     // failing to insert the item is really unexpected 
1695     wxCHECK_MSG( rv 
!= -1, rv
, "failed to insert an item in wxListCtrl" ); 
1698     wxASSERT_MSG( m_count 
== ListView_GetItemCount(GetHwnd()), 
1699                   wxT("m_count should match ListView_GetItemCount")); 
1704 long wxListCtrl::InsertItem(long index
, const wxString
& label
) 
1707     info
.m_text 
= label
; 
1708     info
.m_mask 
= wxLIST_MASK_TEXT
; 
1709     info
.m_itemId 
= index
; 
1710     return InsertItem(info
); 
1713 // Inserts an image item 
1714 long wxListCtrl::InsertItem(long index
, int imageIndex
) 
1717     info
.m_image 
= imageIndex
; 
1718     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
1719     info
.m_itemId 
= index
; 
1720     return InsertItem(info
); 
1723 // Inserts an image/string item 
1724 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
) 
1727     info
.m_image 
= imageIndex
; 
1728     info
.m_text 
= label
; 
1729     info
.m_mask 
= wxLIST_MASK_IMAGE 
| wxLIST_MASK_TEXT
; 
1730     info
.m_itemId 
= index
; 
1731     return InsertItem(info
); 
1734 // For list view mode (only), inserts a column. 
1735 long wxListCtrl::InsertColumn(long col
, const wxListItem
& item
) 
1738     wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
); 
1740     if ( !(lvCol
.mask 
& LVCF_WIDTH
) ) 
1742         // always give some width to the new column: this one is compatible 
1743         // with the generic version 
1744         lvCol
.mask 
|= LVCF_WIDTH
; 
1748     long n 
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
); 
1753     else // failed to insert? 
1755         wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), 
1762 long wxListCtrl::InsertColumn(long col
, 
1763                               const wxString
& heading
, 
1768     item
.m_mask 
= wxLIST_MASK_TEXT 
| wxLIST_MASK_FORMAT
; 
1769     item
.m_text 
= heading
; 
1772         item
.m_mask 
|= wxLIST_MASK_WIDTH
; 
1773         item
.m_width 
= width
; 
1775     item
.m_format 
= format
; 
1777     return InsertColumn(col
, item
); 
1780 // scroll the control by the given number of pixels (exception: in list view, 
1781 // dx is interpreted as number of columns) 
1782 bool wxListCtrl::ScrollList(int dx
, int dy
) 
1784     if ( !ListView_Scroll(GetHwnd(), dx
, dy
) ) 
1786         wxLogDebug(wxT("ListView_Scroll(%d, %d) failed"), dx
, dy
); 
1796 // fn is a function which takes 3 long arguments: item1, item2, data. 
1797 // item1 is the long data associated with a first item (NOT the index). 
1798 // item2 is the long data associated with a second item (NOT the index). 
1799 // data is the same value as passed to SortItems. 
1800 // The return value is a negative number if the first item should precede the second 
1801 // item, a positive number of the second item should precede the first, 
1802 // or zero if the two items are equivalent. 
1804 // data is arbitrary data to be passed to the sort function. 
1806 // Internal structures for proxying the user compare function 
1807 // so that we can pass it the *real* user data 
1809 // translate lParam data and call user func 
1810 struct wxInternalDataSort
 
1812     wxListCtrlCompare user_fn
; 
1816 int CALLBACK 
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
,  LPARAM lParamSort
) 
1818     wxInternalDataSort 
* const internalData 
= (wxInternalDataSort 
*) lParamSort
; 
1820     wxMSWListItemData 
*data1 
= (wxMSWListItemData 
*) lParam1
; 
1821     wxMSWListItemData 
*data2 
= (wxMSWListItemData 
*) lParam2
; 
1823     long d1 
= (data1 
== NULL 
? 0 : data1
->lParam
); 
1824     long d2 
= (data2 
== NULL 
? 0 : data2
->lParam
); 
1826     return internalData
->user_fn(d1
, d2
, internalData
->data
); 
1830 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, wxIntPtr data
) 
1832     wxInternalDataSort internalData
; 
1833     internalData
.user_fn 
= fn
; 
1834     internalData
.data 
= data
; 
1836     // WPARAM cast is needed for mingw/cygwin 
1837     if ( !ListView_SortItems(GetHwnd(), 
1838                              wxInternalDataCompareFunc
, 
1839                              (WPARAM
) &internalData
) ) 
1841         wxLogDebug(wxT("ListView_SortItems() failed")); 
1851 // ---------------------------------------------------------------------------- 
1852 // message processing 
1853 // ---------------------------------------------------------------------------- 
1855 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
) 
1857     if ( msg
->message 
== WM_KEYDOWN 
) 
1859         // Only eat VK_RETURN if not being used by the application in 
1860         // conjunction with modifiers 
1861         if ( msg
->wParam 
== VK_RETURN 
&& !wxIsAnyModifierDown() ) 
1863             // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED 
1867     return wxControl::MSWShouldPreProcessMessage(msg
); 
1870 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
) 
1872     const int id 
= (signed short)id_
; 
1873     if (cmd 
== EN_UPDATE
) 
1875         wxCommandEvent 
event(wxEVT_COMMAND_TEXT_UPDATED
, id
); 
1876         event
.SetEventObject( this ); 
1877         ProcessCommand(event
); 
1880     else if (cmd 
== EN_KILLFOCUS
) 
1882         wxCommandEvent 
event(wxEVT_KILL_FOCUS
, id
); 
1883         event
.SetEventObject( this ); 
1884         ProcessCommand(event
); 
1891 // utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify 
1892 int WXDLLIMPEXP_CORE 
wxMSWGetColumnClicked(NMHDR 
*nmhdr
, POINT 
*ptClick
) 
1894     // find the column clicked: we have to search for it ourselves as the 
1895     // notification message doesn't provide this info 
1897     // where did the click occur? 
1898 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 
1899     if ( nmhdr
->code 
== GN_CONTEXTMENU 
) 
1901         *ptClick 
= ((NMRGINFO
*)nmhdr
)->ptAction
; 
1904 #endif //__WXWINCE__ 
1905     if ( !::GetCursorPos(ptClick
) ) 
1907         wxLogLastError(wxT("GetCursorPos")); 
1910     // we need to use listctrl coordinates for the event point so this is what 
1911     // we return in ptClick, but for comparison with Header_GetItemRect() 
1912     // result below we need to use header window coordinates 
1913     POINT ptClickHeader 
= *ptClick
; 
1914     if ( !::ScreenToClient(nmhdr
->hwndFrom
, &ptClickHeader
) ) 
1916         wxLogLastError(wxT("ScreenToClient(listctrl header)")); 
1919     if ( !::ScreenToClient(::GetParent(nmhdr
->hwndFrom
), ptClick
) ) 
1921         wxLogLastError(wxT("ScreenToClient(listctrl)")); 
1924     const int colCount 
= Header_GetItemCount(nmhdr
->hwndFrom
); 
1925     for ( int col 
= 0; col 
< colCount
; col
++ ) 
1928         if ( Header_GetItemRect(nmhdr
->hwndFrom
, col
, &rect
) ) 
1930             if ( ::PtInRect(&rect
, ptClickHeader
) ) 
1940 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM 
*result
) 
1943     // prepare the event 
1944     // ----------------- 
1946     wxListEvent 
event(wxEVT_NULL
, m_windowId
); 
1947     event
.SetEventObject(this); 
1949     wxEventType eventType 
= wxEVT_NULL
; 
1951     NMHDR 
*nmhdr 
= (NMHDR 
*)lParam
; 
1953     // if your compiler is as broken as this, you should really change it: this 
1954     // code is needed for normal operation! #ifdef below is only useful for 
1955     // automatic rebuilds which are done with a very old compiler version 
1956 #ifdef HDN_BEGINTRACKA 
1958     // check for messages from the header (in report view) 
1959     HWND hwndHdr 
= ListView_GetHeader(GetHwnd()); 
1961     // is it a message from the header? 
1962     if ( nmhdr
->hwndFrom 
== hwndHdr 
) 
1964         HD_NOTIFY 
*nmHDR 
= (HD_NOTIFY 
*)nmhdr
; 
1966         event
.m_itemIndex 
= -1; 
1968         bool ignore 
= false; 
1969         switch ( nmhdr
->code 
) 
1971             // yet another comctl32.dll bug: under NT/W2K it sends Unicode 
1972             // TRACK messages even to ANSI programs: on my system I get 
1973             // HDN_BEGINTRACKW and HDN_ENDTRACKA! 
1975             // work around is to simply catch both versions and hope that it 
1976             // works (why should this message exist in ANSI and Unicode is 
1977             // beyond me as it doesn't deal with strings at all...) 
1979             // another problem is that HDN_TRACK is not sent at all by header 
1980             // with HDS_FULLDRAG style which is used by default by wxListCtrl 
1981             // under recent Windows versions (starting from at least XP) so we 
1982             // need to use HDN_ITEMCHANGING instead of it 
1983             case HDN_BEGINTRACKA
: 
1984             case HDN_BEGINTRACKW
: 
1985                 eventType 
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
; 
1988             case HDN_ITEMCHANGING
: 
1989                 if ( eventType 
== wxEVT_NULL 
) 
1991                     if ( !nmHDR
->pitem 
|| !(nmHDR
->pitem
->mask 
& HDI_WIDTH
) ) 
1993                         // something other than the width is being changed, 
1999                     // also ignore the events sent when the width didn't really 
2000                     // change: this is not just an optimization but also gets 
2001                     // rid of a useless and unexpected DRAGGING event which 
2002                     // would otherwise be sent after the END_DRAG one as we get 
2003                     // an HDN_ITEMCHANGING after HDN_ENDTRACK for some reason 
2004                     if ( nmHDR
->pitem
->cxy 
== GetColumnWidth(nmHDR
->iItem
) ) 
2010                     eventType 
= wxEVT_COMMAND_LIST_COL_DRAGGING
; 
2016                 if ( eventType 
== wxEVT_NULL 
) 
2017                     eventType 
= wxEVT_COMMAND_LIST_COL_END_DRAG
; 
2019                 event
.m_item
.m_width 
= nmHDR
->pitem
->cxy
; 
2020                 event
.m_col 
= nmHDR
->iItem
; 
2023 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 
2024             case GN_CONTEXTMENU
: 
2025 #endif //__WXWINCE__ 
2030                     eventType 
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
; 
2031                     event
.m_col 
= wxMSWGetColumnClicked(nmhdr
, &ptClick
); 
2032                     event
.m_pointDrag
.x 
= ptClick
.x
; 
2033                     event
.m_pointDrag
.y 
= ptClick
.y
; 
2037             case HDN_GETDISPINFOW
: 
2038                 // letting Windows XP handle this message results in mysterious 
2039                 // crashes in comctl32.dll seemingly because of bad message 
2042                 // I have no idea what is the real cause of the bug (which is, 
2043                 // just to make things interesting, impossible to reproduce 
2044                 // reliably) but ignoring all these messages does fix it and 
2045                 // doesn't seem to have any negative consequences 
2053             return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
2056 #endif // defined(HDN_BEGINTRACKA) 
2057     if ( nmhdr
->hwndFrom 
== GetHwnd() ) 
2059         // almost all messages use NM_LISTVIEW 
2060         NM_LISTVIEW 
*nmLV 
= (NM_LISTVIEW 
*)nmhdr
; 
2062         const int iItem 
= nmLV
->iItem
; 
2065         // If we have a valid item then check if there is a data value 
2066         // associated with it and put it in the event. 
2067         if ( iItem 
>= 0 && iItem 
< GetItemCount() ) 
2069             wxMSWListItemData 
*internaldata 
= 
2070                 MSWGetItemData(iItem
); 
2073                 event
.m_item
.m_data 
= internaldata
->lParam
; 
2076         bool processed 
= true; 
2077         switch ( nmhdr
->code 
) 
2079             case LVN_BEGINRDRAG
: 
2080                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
; 
2084                 if ( eventType 
== wxEVT_NULL 
) 
2086                     eventType 
= wxEVT_COMMAND_LIST_BEGIN_DRAG
; 
2089                 event
.m_itemIndex 
= iItem
; 
2090                 event
.m_pointDrag
.x 
= nmLV
->ptAction
.x
; 
2091                 event
.m_pointDrag
.y 
= nmLV
->ptAction
.y
; 
2094             // NB: we have to handle both *A and *W versions here because some 
2095             //     versions of comctl32.dll send ANSI messages even to the 
2097             case LVN_BEGINLABELEDITA
: 
2098             case LVN_BEGINLABELEDITW
: 
2101                     if ( nmhdr
->code 
== LVN_BEGINLABELEDITA 
) 
2103                         item
.Init(((LV_DISPINFOA 
*)lParam
)->item
); 
2105                     else // LVN_BEGINLABELEDITW 
2107                         item
.Init(((LV_DISPINFOW 
*)lParam
)->item
); 
2110                     eventType 
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
; 
2111                     wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
); 
2112                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
2116             case LVN_ENDLABELEDITA
: 
2117             case LVN_ENDLABELEDITW
: 
2120                     if ( nmhdr
->code 
== LVN_ENDLABELEDITA 
) 
2122                         item
.Init(((LV_DISPINFOA 
*)lParam
)->item
); 
2124                     else // LVN_ENDLABELEDITW 
2126                         item
.Init(((LV_DISPINFOW 
*)lParam
)->item
); 
2129                     // was editing cancelled? 
2130                     const LV_ITEM
& lvi 
= (LV_ITEM
)item
; 
2131                     if ( !lvi
.pszText 
|| lvi
.iItem 
== -1 ) 
2133                         // EDIT control will be deleted by the list control 
2134                         // itself so prevent us from deleting it as well 
2135                         DeleteEditControl(); 
2137                         event
.SetEditCanceled(true); 
2140                     eventType 
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
; 
2141                     wxConvertFromMSWListItem(NULL
, event
.m_item
, item
); 
2142                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
2146             case LVN_COLUMNCLICK
: 
2147                 eventType 
= wxEVT_COMMAND_LIST_COL_CLICK
; 
2148                 event
.m_itemIndex 
= -1; 
2149                 event
.m_col 
= nmLV
->iSubItem
; 
2152             case LVN_DELETEALLITEMS
: 
2153                 eventType 
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
; 
2154                 event
.m_itemIndex 
= -1; 
2157             case LVN_DELETEITEM
: 
2160                     // this should be prevented by the post-processing code 
2161                     // below, but "just in case" 
2165                 eventType 
= wxEVT_COMMAND_LIST_DELETE_ITEM
; 
2166                 event
.m_itemIndex 
= iItem
; 
2170             case LVN_INSERTITEM
: 
2171                 eventType 
= wxEVT_COMMAND_LIST_INSERT_ITEM
; 
2172                 event
.m_itemIndex 
= iItem
; 
2175             case LVN_ITEMCHANGED
: 
2176                 // we translate this catch all message into more interesting 
2177                 // (and more easy to process) wxWidgets events 
2179                 // first of all, we deal with the state change events only and 
2180                 // only for valid items (item == -1 for the virtual list 
2182                 if ( nmLV
->uChanged 
& LVIF_STATE 
&& iItem 
!= -1 ) 
2184                     // temp vars for readability 
2185                     const UINT stOld 
= nmLV
->uOldState
; 
2186                     const UINT stNew 
= nmLV
->uNewState
; 
2188                     event
.m_item
.SetId(iItem
); 
2189                     event
.m_item
.SetMask(wxLIST_MASK_TEXT 
| 
2192                     GetItem(event
.m_item
); 
2194                     // has the focus changed? 
2195                     if ( !(stOld 
& LVIS_FOCUSED
) && (stNew 
& LVIS_FOCUSED
) ) 
2197                         eventType 
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
; 
2198                         event
.m_itemIndex 
= iItem
; 
2201                     if ( (stNew 
& LVIS_SELECTED
) != (stOld 
& LVIS_SELECTED
) ) 
2203                         if ( eventType 
!= wxEVT_NULL 
) 
2205                             // focus and selection have both changed: send the 
2206                             // focus event from here and the selection one 
2208                             event
.SetEventType(eventType
); 
2209                             (void)HandleWindowEvent(event
); 
2211                         else // no focus event to send 
2213                             // then need to set m_itemIndex as it wasn't done 
2215                             event
.m_itemIndex 
= iItem
; 
2218                         eventType 
= stNew 
& LVIS_SELECTED
 
2219                                         ? wxEVT_COMMAND_LIST_ITEM_SELECTED
 
2220                                         : wxEVT_COMMAND_LIST_ITEM_DESELECTED
; 
2224                 if ( eventType 
== wxEVT_NULL 
) 
2226                     // not an interesting event for us 
2234                     LV_KEYDOWN 
*info 
= (LV_KEYDOWN 
*)lParam
; 
2235                     WORD wVKey 
= info
->wVKey
; 
2237                     // get the current selection 
2238                     long lItem 
= GetNextItem(-1, 
2240                                              wxLIST_STATE_SELECTED
); 
2242                     // <Enter> or <Space> activate the selected item if any (but 
2243                     // not with any modifiers as they have a predefined meaning 
2246                          (wVKey 
== VK_RETURN 
|| wVKey 
== VK_SPACE
) && 
2247                          !wxIsAnyModifierDown() ) 
2249                         eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
2253                         eventType 
= wxEVT_COMMAND_LIST_KEY_DOWN
; 
2255                         event
.m_code 
= wxMSWKeyboard::VKToWX(wVKey
); 
2257                         if ( event
.m_code 
== WXK_NONE 
) 
2259                             // We can't translate this to a standard key code, 
2260                             // until support for Unicode key codes is added to 
2261                             // wxListEvent we just ignore them. 
2267                     event
.m_item
.m_itemId 
= lItem
; 
2271                         // fill the other fields too 
2272                         event
.m_item
.m_text 
= GetItemText(lItem
); 
2273                         event
.m_item
.m_data 
= GetItemData(lItem
); 
2279                 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do 
2281                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
2286                 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event 
2287                 // if it happened on an item (and not on empty place) 
2294                 eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
2295                 event
.m_itemIndex 
= iItem
; 
2296                 event
.m_item
.m_text 
= GetItemText(iItem
); 
2297                 event
.m_item
.m_data 
= GetItemData(iItem
); 
2300 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 
2301             case GN_CONTEXTMENU
: 
2302 #endif //__WXWINCE__ 
2304                 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), 
2305                 // don't do anything else 
2306                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
2311                 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event 
2312                 LV_HITTESTINFO lvhti
; 
2313                 wxZeroMemory(lvhti
); 
2315 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 
2316                 if ( nmhdr
->code 
== GN_CONTEXTMENU 
) 
2318                     lvhti
.pt 
= ((NMRGINFO
*)nmhdr
)->ptAction
; 
2321 #endif //__WXWINCE__ 
2323                     ::GetCursorPos(&(lvhti
.pt
)); 
2326                 ::ScreenToClient(GetHwnd(), &lvhti
.pt
); 
2327                 if ( ListView_HitTest(GetHwnd(), &lvhti
) != -1 ) 
2329                     if ( lvhti
.flags 
& LVHT_ONITEM 
) 
2331                         eventType 
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
; 
2332                         event
.m_itemIndex 
= lvhti
.iItem
; 
2333                         event
.m_pointDrag
.x 
= lvhti
.pt
.x
; 
2334                         event
.m_pointDrag
.y 
= lvhti
.pt
.y
; 
2339 #ifdef NM_CUSTOMDRAW 
2341                 *result 
= OnCustomDraw(lParam
); 
2343                 return *result 
!= CDRF_DODEFAULT
; 
2344 #endif // _WIN32_IE >= 0x300 
2346             case LVN_ODCACHEHINT
: 
2348                     const NM_CACHEHINT 
*cacheHint 
= (NM_CACHEHINT 
*)lParam
; 
2350                     eventType 
= wxEVT_COMMAND_LIST_CACHE_HINT
; 
2352                     // we get some really stupid cache hints like ones for 
2353                     // items in range 0..0 for an empty control or, after 
2354                     // deleting an item, for items in invalid range -- filter 
2356                     if ( cacheHint
->iFrom 
> cacheHint
->iTo 
) 
2359                     event
.m_oldItemIndex 
= cacheHint
->iFrom
; 
2361                     const long iMax 
= GetItemCount(); 
2362                     event
.m_itemIndex 
= cacheHint
->iTo 
< iMax 
? cacheHint
->iTo
 
2367 #ifdef HAVE_NMLVFINDITEM 
2368             case LVN_ODFINDITEM
: 
2369                 // this message is only used with the virtual list control but 
2370                 // even there we don't want to always use it: in a control with 
2371                 // sufficiently big number of items (defined as > 1000 here), 
2372                 // accidentally pressing a key could result in hanging an 
2373                 // application waiting while it performs linear search 
2374                 if ( IsVirtual() && GetItemCount() <= 1000 ) 
2376                     NMLVFINDITEM
* pFindInfo 
= (NMLVFINDITEM
*)lParam
; 
2378                     // no match by default 
2381                     // we only handle string-based searches here 
2383                     // TODO: what about LVFI_PARTIAL, should we handle this? 
2384                     if ( !(pFindInfo
->lvfi
.flags 
& LVFI_STRING
) ) 
2389                     const wxChar 
* const searchstr 
= pFindInfo
->lvfi
.psz
; 
2390                     const size_t len 
= wxStrlen(searchstr
); 
2392                     // this is the first item we should examine, search from it 
2393                     // wrapping if necessary 
2394                     int startPos 
= pFindInfo
->iStart
; 
2395                     const int maxPos 
= GetItemCount(); 
2397                     // Check that the index is valid to ensure that our loop 
2398                     // below always terminates. 
2399                     if ( startPos 
< 0 || startPos 
>= maxPos 
) 
2401                         // When the last item in the control is selected, 
2402                         // iStart is really set to (invalid) maxPos index so 
2403                         // accept this silently. 
2404                         if ( startPos 
!= maxPos 
) 
2406                             wxLogDebug(wxT("Ignoring invalid search start ") 
2407                                        wxT("position %d in list control with ") 
2408                                        wxT("%d items."), startPos
, maxPos
); 
2414                     for ( int currentPos 
= startPos
; ; ) 
2416                         // does this item begin with searchstr? 
2417                         if ( wxStrnicmp(searchstr
, 
2418                                             GetItemText(currentPos
), len
) == 0 ) 
2420                             *result 
= currentPos
; 
2424                         // Go to next item with wrapping if necessary. 
2425                         if ( ++currentPos 
== maxPos 
) 
2427                             // Surprisingly, LVFI_WRAP seems to be never set in 
2428                             // the flags so wrap regardless of it. 
2432                         if ( currentPos 
== startPos 
) 
2434                             // We examined all items without finding anything. 
2436                             // Notice that we still return true as we did 
2437                             // perform the search, if we didn't do this the 
2438                             // message would have been considered unhandled and 
2439                             // the control seems to always select the first 
2440                             // item by default in this case. 
2445                     SetItemState(*result
, 
2446                                  wxLIST_STATE_SELECTED 
| wxLIST_STATE_FOCUSED
, 
2447                                  wxLIST_STATE_SELECTED 
| wxLIST_STATE_FOCUSED
); 
2448                     EnsureVisible(*result
); 
2456 #endif // HAVE_NMLVFINDITEM 
2458             case LVN_GETDISPINFO
: 
2461                     LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
2463                     LV_ITEM
& lvi 
= info
->item
; 
2464                     long item 
= lvi
.iItem
; 
2466                     if ( lvi
.mask 
& LVIF_TEXT 
) 
2468                         wxString text 
= OnGetItemText(item
, lvi
.iSubItem
); 
2469                         wxStrlcpy(lvi
.pszText
, text
.c_str(), lvi
.cchTextMax
); 
2472                     // see comment at the end of wxListCtrl::GetColumn() 
2473 #ifdef NM_CUSTOMDRAW 
2474                     if ( lvi
.mask 
& LVIF_IMAGE 
) 
2476                         lvi
.iImage 
= OnGetItemColumnImage(item
, lvi
.iSubItem
); 
2478 #endif // NM_CUSTOMDRAW 
2480                     // even though we never use LVM_SETCALLBACKMASK, we still 
2481                     // can get messages with LVIF_STATE in lvi.mask under Vista 
2482                     if ( lvi
.mask 
& LVIF_STATE 
) 
2484                         // we don't have anything to return from here... 
2497             return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
2501         // where did this one come from? 
2505     // process the event 
2506     // ----------------- 
2508     event
.SetEventType(eventType
); 
2510     // fill in the item before passing it to the event handler if we do have a 
2511     // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED) 
2512     if ( event
.m_itemIndex 
!= -1 && !event
.m_item
.GetMask() ) 
2514         wxListItem
& item 
= event
.m_item
; 
2516         item
.SetId(event
.m_itemIndex
); 
2517         item
.SetMask(wxLIST_MASK_TEXT 
| wxLIST_MASK_IMAGE 
| wxLIST_MASK_DATA
); 
2521     bool processed 
= HandleWindowEvent(event
); 
2525     switch ( nmhdr
->code 
) 
2527         case LVN_DELETEALLITEMS
: 
2528             // always return true to suppress all additional LVN_DELETEITEM 
2529             // notifications - this makes deleting all items from a list ctrl 
2533             // also, we may free all user data now (couldn't do it before as 
2534             // the user should have access to it in OnDeleteAllItems() handler) 
2535             FreeAllInternalData(); 
2537             // the control is empty now, synchronize the cached number of items 
2538             // with the real one 
2542         case LVN_DELETEITEM
: 
2543             // Delete the associated internal data. Notice that this can be 
2544             // done only after the event has been handled as the data could be 
2545             // accessed during the handling of the event. 
2546             if ( wxMSWListItemData 
*data 
= MSWGetItemData(event
.m_itemIndex
) ) 
2548                 const unsigned count 
= m_internalData
.size(); 
2549                 for ( unsigned n 
= 0; n 
< count
; n
++ ) 
2551                     if ( m_internalData
[n
] == data 
) 
2553                         m_internalData
.erase(m_internalData
.begin() + n
); 
2559                 wxASSERT_MSG( !data
, "invalid internal data pointer?" ); 
2563         case LVN_ENDLABELEDITA
: 
2564         case LVN_ENDLABELEDITW
: 
2565             // logic here is inverted compared to all the other messages 
2566             *result 
= event
.IsAllowed(); 
2568             // EDIT control will be deleted by the list control itself so 
2569             // prevent us from deleting it as well 
2570             DeleteEditControl(); 
2576         *result 
= !event
.IsAllowed(); 
2581 // ---------------------------------------------------------------------------- 
2582 // custom draw stuff 
2583 // ---------------------------------------------------------------------------- 
2585 // see comment at the end of wxListCtrl::GetColumn() 
2586 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 
2588 static RECT 
GetCustomDrawnItemRect(const NMCUSTOMDRAW
& nmcd
) 
2591     wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_BOUNDS
, rc
); 
2594     wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_ICON
, rcIcon
); 
2596     // exclude the icon part, neither the selection background nor focus rect 
2598     rc
.left 
= rcIcon
.right
; 
2604 bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
, int colCount
) 
2606     NMCUSTOMDRAW
& nmcd 
= pLVCD
->nmcd
; 
2609     HWND hwndList 
= nmcd
.hdr
.hwndFrom
; 
2610     const int col 
= pLVCD
->iSubItem
; 
2611     const DWORD item 
= nmcd
.dwItemSpec
; 
2613     // the font must be valid, otherwise we wouldn't be painting the item at all 
2614     SelectInHDC 
selFont(hdc
, hfont
); 
2616     // get the rectangle to paint 
2617     int subitem 
= colCount 
? col 
+ 1 : col
; 
2619     wxGetListCtrlSubItemRect(hwndList
, item
, subitem
, LVIR_BOUNDS
, rc
); 
2622     // get the image and text to draw 
2626     it
.mask 
= LVIF_TEXT 
| LVIF_IMAGE
; 
2630     it
.cchTextMax 
= WXSIZEOF(text
); 
2631     ListView_GetItem(hwndList
, &it
); 
2633     HIMAGELIST himl 
= ListView_GetImageList(hwndList
, LVSIL_SMALL
); 
2634     if ( himl 
&& ImageList_GetImageCount(himl
) ) 
2636         if ( it
.iImage 
!= -1 ) 
2638             ImageList_Draw(himl
, it
.iImage
, hdc
, rc
.left
, rc
.top
, 
2639                            nmcd
.uItemState 
& CDIS_SELECTED 
? ILD_SELECTED
 
2643         // notice that even if this item doesn't have any image, the list 
2644         // control still leaves space for the image in the first column if the 
2645         // image list is not empty (presumably so that items with and without 
2647         if ( it
.iImage 
!= -1 || it
.iSubItem 
== 0 ) 
2650             ImageList_GetIconSize(himl
, &wImage
, &hImage
); 
2652             rc
.left 
+= wImage 
+ 2; 
2656     ::SetBkMode(hdc
, TRANSPARENT
); 
2658     UINT fmt 
= DT_SINGLELINE 
| 
2661 #endif // __WXWINCE__ 
2666     wxZeroMemory(lvCol
); 
2667     lvCol
.mask 
= LVCF_FMT
; 
2668     if ( ListView_GetColumn(hwndList
, col
, &lvCol
) ) 
2670         switch ( lvCol
.fmt 
& LVCFMT_JUSTIFYMASK 
) 
2685     //else: failed to get alignment, assume it's DT_LEFT (default) 
2687     DrawText(hdc
, text
, -1, &rc
, fmt
); 
2692 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd
) 
2694     if ( nmcd
.uItemState 
& CDIS_FOCUS 
) 
2696         RECT rc 
= GetCustomDrawnItemRect(nmcd
); 
2698         // don't use the provided HDC, it's in some strange state by now 
2699         ::DrawFocusRect(WindowHDC(nmcd
.hdr
.hwndFrom
), &rc
); 
2703 // pLVCD->clrText and clrTextBk should contain the colours to use 
2704 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
) 
2706     NMCUSTOMDRAW
& nmcd 
= pLVCD
->nmcd
; // just a shortcut 
2708     const HWND hwndList 
= nmcd
.hdr
.hwndFrom
; 
2709     const int item 
= nmcd
.dwItemSpec
; 
2711     // unfortunately we can't trust CDIS_SELECTED, it is often set even when 
2712     // the item is not at all selected for some reason (comctl32 6), but we 
2713     // also can't always trust ListView_GetItem() as it could return the old 
2714     // item status if we're called just after the (de)selection, so remember 
2715     // the last item to gain selection and also check for it here 
2716     for ( int i 
= -1;; ) 
2718         i 
= ListView_GetNextItem(hwndList
, i
, LVNI_SELECTED
); 
2721             nmcd
.uItemState 
&= ~CDIS_SELECTED
; 
2727             nmcd
.uItemState 
|= CDIS_SELECTED
; 
2732     // same thing for CDIS_FOCUS (except simpler as there is only one of them) 
2734     // NB: cast is needed to work around the bug in mingw32 headers which don't 
2735     //     have it inside ListView_GetNextItem() itself (unlike SDK ones) 
2736     if ( ::GetFocus() == hwndList 
&& 
2737             ListView_GetNextItem( 
2738                 hwndList
, static_cast<WPARAM
>(-1), LVNI_FOCUSED
) == item 
) 
2740         nmcd
.uItemState 
|= CDIS_FOCUS
; 
2744         nmcd
.uItemState 
&= ~CDIS_FOCUS
; 
2747     if ( nmcd
.uItemState 
& CDIS_SELECTED 
) 
2749         int syscolFg
, syscolBg
; 
2750         if ( ::GetFocus() == hwndList 
) 
2752             syscolFg 
= COLOR_HIGHLIGHTTEXT
; 
2753             syscolBg 
= COLOR_HIGHLIGHT
; 
2755         else // selected but unfocused 
2757             syscolFg 
= COLOR_WINDOWTEXT
; 
2758             syscolBg 
= COLOR_BTNFACE
; 
2760             // don't grey out the icon in this case neither 
2761             nmcd
.uItemState 
&= ~CDIS_SELECTED
; 
2764         pLVCD
->clrText 
= ::GetSysColor(syscolFg
); 
2765         pLVCD
->clrTextBk 
= ::GetSysColor(syscolBg
); 
2767     //else: not selected, use normal colours from pLVCD 
2770     RECT rc 
= GetCustomDrawnItemRect(nmcd
); 
2772     ::SetTextColor(hdc
, pLVCD
->clrText
); 
2773     ::FillRect(hdc
, &rc
, AutoHBRUSH(pLVCD
->clrTextBk
)); 
2775     // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint 
2776     // problems so just draw everything except the focus rect from here instead 
2777     const int colCount 
= Header_GetItemCount(ListView_GetHeader(hwndList
)); 
2778     for ( int col 
= 0; col 
< colCount
; col
++ ) 
2780         pLVCD
->iSubItem 
= col
; 
2781         HandleSubItemPrepaint(pLVCD
, hfont
, colCount
); 
2784     HandleItemPostpaint(nmcd
); 
2787 static WXLPARAM 
HandleItemPrepaint(wxListCtrl 
*listctrl
, 
2788                                    LPNMLVCUSTOMDRAW pLVCD
, 
2789                                    wxListItemAttr 
*attr
) 
2793         // nothing to do for this item 
2794         return CDRF_DODEFAULT
; 
2798     // set the colours to use for text drawing 
2799     pLVCD
->clrText 
= attr
->HasTextColour() 
2800                      ? wxColourToRGB(attr
->GetTextColour()) 
2801                      : wxColourToRGB(listctrl
->GetTextColour()); 
2802     pLVCD
->clrTextBk 
= attr
->HasBackgroundColour() 
2803                        ? wxColourToRGB(attr
->GetBackgroundColour()) 
2804                        : wxColourToRGB(listctrl
->GetBackgroundColour()); 
2806     // select the font if non default one is specified 
2807     if ( attr
->HasFont() ) 
2809         wxFont font 
= attr
->GetFont(); 
2810         if ( font
.GetEncoding() != wxFONTENCODING_SYSTEM 
) 
2812             // the standard control ignores the font encoding/charset, at least 
2813             // with recent comctl32.dll versions (5 and 6, it uses to work with 
2814             // 4.something) so we have to draw the item entirely ourselves in 
2816             HandleItemPaint(pLVCD
, GetHfontOf(font
)); 
2817             return CDRF_SKIPDEFAULT
; 
2820         ::SelectObject(pLVCD
->nmcd
.hdc
, GetHfontOf(font
)); 
2822         return CDRF_NEWFONT
; 
2825     return CDRF_DODEFAULT
; 
2828 WXLPARAM 
wxListCtrl::OnCustomDraw(WXLPARAM lParam
) 
2830     LPNMLVCUSTOMDRAW pLVCD 
= (LPNMLVCUSTOMDRAW
)lParam
; 
2831     NMCUSTOMDRAW
& nmcd 
= pLVCD
->nmcd
; 
2832     switch ( nmcd
.dwDrawStage 
) 
2835             // if we've got any items with non standard attributes, 
2836             // notify us before painting each item 
2838             // for virtual controls, always suppose that we have attributes as 
2839             // there is no way to check for this 
2840             if ( IsVirtual() || m_hasAnyAttr 
) 
2841                 return CDRF_NOTIFYITEMDRAW
; 
2844         case CDDS_ITEMPREPAINT
: 
2845             // get a message for each subitem 
2846             return CDRF_NOTIFYITEMDRAW
; 
2848         case CDDS_SUBITEM 
| CDDS_ITEMPREPAINT
: 
2849             const int item 
= nmcd
.dwItemSpec
; 
2850             const int column 
= pLVCD
->iSubItem
; 
2852             // we get this message with item == 0 for an empty control, we 
2853             // must ignore it as calling OnGetItemAttr() would be wrong 
2854             if ( item 
< 0 || item 
>= GetItemCount() ) 
2857             if ( column 
< 0 || column 
>= GetColumnCount() ) 
2860             return HandleItemPrepaint(this, pLVCD
, DoGetItemColumnAttr(item
, column
)); 
2863     return CDRF_DODEFAULT
; 
2866 #endif // NM_CUSTOMDRAW supported 
2868 // Necessary for drawing hrules and vrules, if specified 
2869 void wxListCtrl::OnPaint(wxPaintEvent
& event
) 
2871     const int itemCount 
= GetItemCount(); 
2872     const bool drawHRules 
= HasFlag(wxLC_HRULES
); 
2873     const bool drawVRules 
= HasFlag(wxLC_VRULES
); 
2875     if (!InReportView() || !(drawHRules 
|| drawVRules
) || !itemCount
) 
2883     wxControl::OnPaint(event
); 
2885     // Reset the device origin since it may have been set 
2886     dc
.SetDeviceOrigin(0, 0); 
2888     wxPen 
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
)); 
2890     dc
.SetBrush(* wxTRANSPARENT_BRUSH
); 
2892     wxSize clientSize 
= GetClientSize(); 
2897         const long top 
= GetTopItem(); 
2898         for ( int i 
= top
; i 
< top 
+ GetCountPerPage() + 1; i
++ ) 
2900             if (GetItemRect(i
, itemRect
)) 
2902                 int cy 
= itemRect
.GetTop(); 
2903                 if (i 
!= 0) // Don't draw the first one 
2905                     dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
2908                 if (i 
== itemCount 
- 1) 
2910                     cy 
= itemRect
.GetBottom(); 
2911                     dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
2920         wxRect firstItemRect
; 
2921         GetItemRect(0, firstItemRect
); 
2923         if (GetItemRect(itemCount 
- 1, itemRect
)) 
2925             // this is a fix for bug 673394: erase the pixels which we would 
2926             // otherwise leave on the screen 
2927             static const int gap 
= 2; 
2928             dc
.SetPen(*wxTRANSPARENT_PEN
); 
2929             dc
.SetBrush(wxBrush(GetBackgroundColour())); 
2930             dc
.DrawRectangle(0, firstItemRect
.GetY() - gap
, 
2931                              clientSize
.GetWidth(), gap
); 
2934             dc
.SetBrush(*wxTRANSPARENT_BRUSH
); 
2936             const int numCols 
= GetColumnCount(); 
2937             wxVector
<int> indexArray(numCols
); 
2938             if ( !ListView_GetColumnOrderArray(GetHwnd(), 
2942                 wxFAIL_MSG( wxT("invalid column index array in OnPaint()") ); 
2946             int x 
= itemRect
.GetX(); 
2947             for (int col 
= 0; col 
< numCols
; col
++) 
2949                 int colWidth 
= GetColumnWidth(indexArray
[col
]); 
2951                 dc
.DrawLine(x
-1, firstItemRect
.GetY() - gap
, 
2952                             x
-1, itemRect
.GetBottom()); 
2959 wxListCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
) 
2965             // we should bypass our own WM_PRINT handling as we don't handle 
2966             // PRF_CHILDREN flag, so leave it to the native control itself 
2967             return MSWDefWindowProc(nMsg
, wParam
, lParam
); 
2970         case WM_CONTEXTMENU
: 
2971             // because this message is propagated upwards the child-parent 
2972             // chain, we get it for the right clicks on the header window but 
2973             // this is confusing in wx as right clicking there already 
2974             // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event 
2975             // so just ignore them 
2976             if ( (HWND
)wParam 
== ListView_GetHeader(GetHwnd()) ) 
2981     return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
); 
2984 // ---------------------------------------------------------------------------- 
2985 // virtual list controls 
2986 // ---------------------------------------------------------------------------- 
2988 wxString 
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const 
2990     // this is a pure virtual function, in fact - which is not really pure 
2991     // because the controls which are not virtual don't need to implement it 
2992     wxFAIL_MSG( wxT("wxListCtrl::OnGetItemText not supposed to be called") ); 
2994     return wxEmptyString
; 
2997 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const 
2999     wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
), 
3001                 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden.")); 
3005 int wxListCtrl::OnGetItemColumnImage(long item
, long column
) const 
3008         return OnGetItemImage(item
); 
3013 wxListItemAttr 
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const 
3015     wxASSERT_MSG( item 
>= 0 && item 
< GetItemCount(), 
3016                   wxT("invalid item index in OnGetItemAttr()") ); 
3018     // no attributes by default 
3022 wxListItemAttr 
*wxListCtrl::DoGetItemColumnAttr(long item
, long column
) const 
3025         return OnGetItemColumnAttr(item
, column
); 
3027     wxMSWListItemData 
* const data 
= MSWGetItemData(item
); 
3028     return data 
? data
->attr 
: NULL
; 
3031 void wxListCtrl::SetItemCount(long count
) 
3033     wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") ); 
3035     if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 
3036                         LVSICF_NOSCROLL 
| LVSICF_NOINVALIDATEALL
) ) 
3038         wxLogLastError(wxT("ListView_SetItemCount")); 
3041     wxASSERT_MSG( m_count 
== ListView_GetItemCount(GetHwnd()), 
3042                   wxT("m_count should match ListView_GetItemCount")); 
3045 void wxListCtrl::RefreshItem(long item
) 
3047     RefreshItems(item
, item
); 
3050 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
) 
3052     ListView_RedrawItems(GetHwnd(), itemFrom
, itemTo
); 
3055 // ---------------------------------------------------------------------------- 
3056 // wxWin <-> MSW items conversions 
3057 // ---------------------------------------------------------------------------- 
3059 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
3063     wxMSWListItemData 
*internaldata 
= 
3064         (wxMSWListItemData 
*) lvItem
.lParam
; 
3067         info
.m_data 
= internaldata
->lParam
; 
3071     info
.m_stateMask 
= 0; 
3072     info
.m_itemId 
= lvItem
.iItem
; 
3074     long oldMask 
= lvItem
.mask
; 
3076     bool needText 
= false; 
3077     if (hwndListCtrl 
!= 0) 
3079         if ( lvItem
.mask 
& LVIF_TEXT 
) 
3086             lvItem
.pszText 
= new wxChar
[513]; 
3087             lvItem
.cchTextMax 
= 512; 
3089         lvItem
.mask 
|= LVIF_TEXT 
| LVIF_IMAGE 
| LVIF_PARAM
; 
3090         ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
); 
3093     if ( lvItem
.mask 
& LVIF_STATE 
) 
3095         info
.m_mask 
|= wxLIST_MASK_STATE
; 
3097         if ( lvItem
.stateMask 
& LVIS_CUT
) 
3099             info
.m_stateMask 
|= wxLIST_STATE_CUT
; 
3100             if ( lvItem
.state 
& LVIS_CUT 
) 
3101                 info
.m_state 
|= wxLIST_STATE_CUT
; 
3103         if ( lvItem
.stateMask 
& LVIS_DROPHILITED
) 
3105             info
.m_stateMask 
|= wxLIST_STATE_DROPHILITED
; 
3106             if ( lvItem
.state 
& LVIS_DROPHILITED 
) 
3107                 info
.m_state 
|= wxLIST_STATE_DROPHILITED
; 
3109         if ( lvItem
.stateMask 
& LVIS_FOCUSED
) 
3111             info
.m_stateMask 
|= wxLIST_STATE_FOCUSED
; 
3112             if ( lvItem
.state 
& LVIS_FOCUSED 
) 
3113                 info
.m_state 
|= wxLIST_STATE_FOCUSED
; 
3115         if ( lvItem
.stateMask 
& LVIS_SELECTED
) 
3117             info
.m_stateMask 
|= wxLIST_STATE_SELECTED
; 
3118             if ( lvItem
.state 
& LVIS_SELECTED 
) 
3119                 info
.m_state 
|= wxLIST_STATE_SELECTED
; 
3123     if ( lvItem
.mask 
& LVIF_TEXT 
) 
3125         info
.m_mask 
|= wxLIST_MASK_TEXT
; 
3126         info
.m_text 
= lvItem
.pszText
; 
3128     if ( lvItem
.mask 
& LVIF_IMAGE 
) 
3130         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
3131         info
.m_image 
= lvItem
.iImage
; 
3133     if ( lvItem
.mask 
& LVIF_PARAM 
) 
3134         info
.m_mask 
|= wxLIST_MASK_DATA
; 
3135     if ( lvItem
.mask 
& LVIF_DI_SETITEM 
) 
3136         info
.m_mask 
|= wxLIST_SET_ITEM
; 
3137     info
.m_col 
= lvItem
.iSubItem
; 
3142             delete[] lvItem
.pszText
; 
3144     lvItem
.mask 
= oldMask
; 
3147 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
) 
3149     if (stateMask 
& wxLIST_STATE_CUT
) 
3151         lvItem
.stateMask 
|= LVIS_CUT
; 
3152         if (state 
& wxLIST_STATE_CUT
) 
3153             lvItem
.state 
|= LVIS_CUT
; 
3155     if (stateMask 
& wxLIST_STATE_DROPHILITED
) 
3157         lvItem
.stateMask 
|= LVIS_DROPHILITED
; 
3158         if (state 
& wxLIST_STATE_DROPHILITED
) 
3159             lvItem
.state 
|= LVIS_DROPHILITED
; 
3161     if (stateMask 
& wxLIST_STATE_FOCUSED
) 
3163         lvItem
.stateMask 
|= LVIS_FOCUSED
; 
3164         if (state 
& wxLIST_STATE_FOCUSED
) 
3165             lvItem
.state 
|= LVIS_FOCUSED
; 
3167     if (stateMask 
& wxLIST_STATE_SELECTED
) 
3169         lvItem
.stateMask 
|= LVIS_SELECTED
; 
3170         if (state 
& wxLIST_STATE_SELECTED
) 
3171             lvItem
.state 
|= LVIS_SELECTED
; 
3175 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
3176                                    const wxListItem
& info
, 
3179     if ( ctrl
->InReportView() ) 
3181         wxASSERT_MSG( 0 <= info
.m_col 
&& info
.m_col 
< ctrl
->GetColumnCount(), 
3182                       "wxListCtrl column index out of bounds" ); 
3184     else // not in report view 
3186         wxASSERT_MSG( info
.m_col 
== 0, "columns only exist in report view" ); 
3189     lvItem
.iItem 
= (int) info
.m_itemId
; 
3191     lvItem
.iImage 
= info
.m_image
; 
3192     lvItem
.stateMask 
= 0; 
3195     lvItem
.iSubItem 
= info
.m_col
; 
3197     if (info
.m_mask 
& wxLIST_MASK_STATE
) 
3199         lvItem
.mask 
|= LVIF_STATE
; 
3201         wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
); 
3204     if (info
.m_mask 
& wxLIST_MASK_TEXT
) 
3206         lvItem
.mask 
|= LVIF_TEXT
; 
3207         if ( ctrl
->HasFlag(wxLC_USER_TEXT
) ) 
3209             lvItem
.pszText 
= LPSTR_TEXTCALLBACK
; 
3213             // pszText is not const, hence the cast 
3214             lvItem
.pszText 
= (wxChar 
*)info
.m_text
.wx_str(); 
3215             if ( lvItem
.pszText 
) 
3216                 lvItem
.cchTextMax 
= info
.m_text
.length(); 
3218                 lvItem
.cchTextMax 
= 0; 
3221     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
3222         lvItem
.mask 
|= LVIF_IMAGE
; 
3225 static void wxConvertToMSWListCol(HWND hwndList
, 
3227                                   const wxListItem
& item
, 
3230     wxZeroMemory(lvCol
); 
3232     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
3234         lvCol
.mask 
|= LVCF_TEXT
; 
3235         lvCol
.pszText 
= (wxChar 
*)item
.m_text
.wx_str(); // cast is safe 
3238     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
3240         lvCol
.mask 
|= LVCF_FMT
; 
3242         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
3243             lvCol
.fmt 
= LVCFMT_LEFT
; 
3244         else if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
3245             lvCol
.fmt 
= LVCFMT_RIGHT
; 
3246         else if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
3247             lvCol
.fmt 
= LVCFMT_CENTER
; 
3250     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
3252         lvCol
.mask 
|= LVCF_WIDTH
; 
3253         if ( item
.m_width 
== wxLIST_AUTOSIZE
) 
3254             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
3255         else if ( item
.m_width 
== wxLIST_AUTOSIZE_USEHEADER
) 
3256             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
3258             lvCol
.cx 
= item
.m_width
; 
3261     // see comment at the end of wxListCtrl::GetColumn() 
3262 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 
3263     if ( item
.m_mask 
& wxLIST_MASK_IMAGE 
) 
3265         if ( wxApp::GetComCtl32Version() >= 470 ) 
3267             lvCol
.mask 
|= LVCF_IMAGE
; 
3269             // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right 
3270             // seem to be generally nicer than on the left and the generic 
3271             // version only draws them on the right (we don't have a flag to 
3272             // specify the image location anyhow) 
3274             // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to 
3275             // make any difference in my tests -- but maybe we should? 
3276             if ( item
.m_image 
!= -1 ) 
3278                 // as we're going to overwrite the format field, get its 
3279                 // current value first -- unless we want to overwrite it anyhow 
3280                 if ( !(lvCol
.mask 
& LVCF_FMT
) ) 
3283                     wxZeroMemory(lvColOld
); 
3284                     lvColOld
.mask 
= LVCF_FMT
; 
3285                     if ( ListView_GetColumn(hwndList
, col
, &lvColOld
) ) 
3287                         lvCol
.fmt 
= lvColOld
.fmt
; 
3290                     lvCol
.mask 
|= LVCF_FMT
; 
3293                 lvCol
.fmt 
|= LVCFMT_BITMAP_ON_RIGHT 
| LVCFMT_IMAGE
; 
3296             lvCol
.iImage 
= item
.m_image
; 
3298         //else: it doesn't support item images anyhow 
3300 #endif // _WIN32_IE >= 0x0300 
3303 #endif // wxUSE_LISTCTRL