1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/listctrl.cpp 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21     #pragma implementation "listctrl.h" 
  22     #pragma implementation "listctrlbase.h" 
  25 // For compilers that support precompilation, includes "wx.h". 
  26 #include "wx/wxprec.h" 
  32 #if wxUSE_LISTCTRL && defined(__WIN95__) 
  38     #include "wx/settings.h" 
  41 #include "wx/textctrl.h" 
  42 #include "wx/imaglist.h" 
  43 #include "wx/listctrl.h" 
  44 #include "wx/dcclient.h" 
  46 #include "wx/msw/private.h" 
  48 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__)) 
  49     #include "wx/msw/gnuwin32/extra.h" 
  56                 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON) 
  59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE 
  60     #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54) 
  63 #ifndef LVS_EX_FULLROWSELECT 
  64     #define LVS_EX_FULLROWSELECT 0x00000020 
  68     #define LVS_OWNERDATA 0x1000 
  72     #define LVM_FIRST 0x1000 
  76     #define HDM_FIRST 0x1200 
  79 // mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff 
  81     typedef struct tagNMLVCACHEHINT
 
  88     #define NM_CACHEHINT NMLVCACHEHINT 
  91 #ifndef LVN_ODCACHEHINT 
  92     #define LVN_ODCACHEHINT (-113) 
  95 #ifndef ListView_GetHeader 
  96     #define ListView_GetHeader(w) (HWND)SendMessage((w),LVM_GETHEADER,0,0) 
 100     #define LVM_GETHEADER (LVM_FIRST+31) 
 103 #ifndef Header_GetItemRect 
 104     #define Header_GetItemRect(w,i,r) \ 
 105             (BOOL)SendMessage((w),HDM_GETITEMRECT,(WPARAM)(i),(LPARAM)(r)) 
 108 #ifndef HDM_GETITEMRECT 
 109     #define HDM_GETITEMRECT (HDM_FIRST+7) 
 113     #define LVCF_IMAGE             0x0010 
 116 #ifndef LVCFMT_BITMAP_ON_RIGHT 
 117     #define LVCFMT_BITMAP_ON_RIGHT 0x1000 
 120 #if defined(__GNUWIN32__) && !defined(LV_ITEM) \ 
 121     && !wxCHECK_W32API_VERSION( 0, 5 ) 
 122 typedef struct _LVITEMW 
{ 
 132 #if (_WIN32_IE >= 0x0300) 
 138 // ---------------------------------------------------------------------------- 
 140 // ---------------------------------------------------------------------------- 
 142 // convert our state and mask flags to LV_ITEM constants 
 143 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
); 
 145 // convert wxListItem to LV_ITEM 
 146 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
 147                                    const wxListItem
& info
, LV_ITEM
& lvItem
); 
 149 // convert LV_ITEM to wxListItem 
 150 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
 152                                      /* const */ LV_ITEM
& lvItem
); 
 154 // convert our wxListItem to LV_COLUMN 
 155 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
, 
 158 // ---------------------------------------------------------------------------- 
 159 // private helper classes 
 160 // ---------------------------------------------------------------------------- 
 162 // We have to handle both fooW and fooA notifications in several cases 
 163 // because of broken commctl.dll and/or unicows.dll. This class is used to 
 164 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or  
 165 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed 
 166 // by wxConvertToMSWListItem(). 
 170     ~wxLV_ITEM() { delete m_buf
; } 
 171     operator LV_ITEM
&() const { return *m_item
; } 
 174     wxLV_ITEM(LV_ITEMW 
&item
) : m_buf(NULL
), m_item(&item
) {} 
 175     wxLV_ITEM(LV_ITEMA 
&item
) 
 177         m_item 
= new LV_ITEM((LV_ITEM
&)item
); 
 178         if ( (item
.mask 
& LVIF_TEXT
) && item
.pszText 
) 
 180             m_buf 
= new wxMB2WXbuf(wxConvLocal
.cMB2WX(item
.pszText
)); 
 181             m_item
->pszText 
= (wxChar
*)m_buf
->data(); 
 190     wxLV_ITEM(LV_ITEMW 
&item
) 
 192         m_item 
= new LV_ITEM((LV_ITEM
&)item
); 
 193         if ( (item
.mask 
& LVIF_TEXT
) && item
.pszText 
) 
 195             m_buf 
= new wxWC2WXbuf(wxConvLocal
.cWC2WX(item
.pszText
)); 
 196             m_item
->pszText 
= (wxChar
*)m_buf
->data(); 
 201     wxLV_ITEM(LV_ITEMA 
&item
) : m_buf(NULL
), m_item(&item
) {} 
 209 // ---------------------------------------------------------------------------- 
 211 // ---------------------------------------------------------------------------- 
 213 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
) 
 214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
) 
 215 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
) 
 216 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
) 
 217 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
) 
 218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
) 
 219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
) 
 220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
) 
 221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
) 
 222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
) 
 223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
) 
 224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
) 
 225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
) 
 226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
) 
 227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
) 
 228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
) 
 229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
) 
 230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
) 
 231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
) 
 232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
) 
 233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
) 
 234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
) 
 236 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
) 
 237 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
) 
 238 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
) 
 240 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
) 
 242 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
) 
 243     EVT_PAINT(wxListCtrl::OnPaint
) 
 246 // ============================================================================ 
 248 // ============================================================================ 
 250 // ---------------------------------------------------------------------------- 
 251 // wxListCtrl construction 
 252 // ---------------------------------------------------------------------------- 
 254 void wxListCtrl::Init() 
 256     m_imageListNormal 
= NULL
; 
 257     m_imageListSmall 
= NULL
; 
 258     m_imageListState 
= NULL
; 
 259     m_ownsImageListNormal 
= m_ownsImageListSmall 
= m_ownsImageListState 
= FALSE
; 
 263     m_hasAnyAttr 
= FALSE
; 
 266 bool wxListCtrl::Create(wxWindow 
*parent
, 
 271                         const wxValidator
& validator
, 
 272                         const wxString
& name
) 
 275     SetValidator(validator
); 
 276 #endif // wxUSE_VALIDATORS 
 285     m_windowStyle 
= style
; 
 298     m_windowId 
= (id 
== -1) ? NewControlId() : id
; 
 300     DWORD wstyle 
= WS_VISIBLE 
| WS_CHILD 
| WS_TABSTOP 
| 
 301                    LVS_SHAREIMAGELISTS 
| LVS_SHOWSELALWAYS
; 
 303     if ( m_windowStyle 
& wxCLIP_SIBLINGS 
) 
 304         wstyle 
|= WS_CLIPSIBLINGS
; 
 306     if ( wxStyleHasBorder(m_windowStyle
) ) 
 308     m_baseStyle 
= wstyle
; 
 310     if ( !DoCreateControl(x
, y
, width
, height
) ) 
 314         parent
->AddChild(this); 
 319 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
) 
 321     DWORD wstyle 
= m_baseStyle
; 
 324     WXDWORD exStyle 
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
); 
 326     // Even with extended styles, need to combine with WS_BORDER 
 327     // for them to look right. 
 331     long oldStyle 
= 0; // Dummy 
 332     wstyle 
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
); 
 334     // Create the ListView control. 
 335     m_hWnd 
= (WXHWND
)CreateWindowEx(exStyle
, 
 340                                     GetWinHwnd(GetParent()), 
 347         wxLogError(_("Can't create list control window, check that comctl32.dll is installed.")); 
 352     // for comctl32.dll v 4.70+ we want to have this attribute because it's 
 353     // prettier (and also because wxGTK does it like this) 
 354     if ( (wstyle 
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 ) 
 356         ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 
 357                       0, LVS_EX_FULLROWSELECT
); 
 360     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)); 
 361     SetForegroundColour(GetParent()->GetForegroundColour()); 
 368 void wxListCtrl::UpdateStyle() 
 372         // The new window view style 
 374         DWORD dwStyleNew 
= ConvertToMSWStyle(dummy
, m_windowStyle
); 
 375         dwStyleNew 
|= m_baseStyle
; 
 377         // Get the current window style. 
 378         DWORD dwStyleOld 
= ::GetWindowLong(GetHwnd(), GWL_STYLE
); 
 380         // Only set the window style if the view bits have changed. 
 381         if ( dwStyleOld 
!= dwStyleNew 
) 
 383             ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
); 
 388 void wxListCtrl::FreeAllAttrs(bool dontRecreate
) 
 392         for ( wxNode 
*node 
= m_attrs
.Next(); node
; node 
= m_attrs
.Next() ) 
 394             delete (wxListItemAttr 
*)node
->Data(); 
 400             m_attrs
.Create(wxKEY_INTEGER
, 1000);        // just as def ctor 
 403         m_hasAnyAttr 
= FALSE
; 
 407 wxListCtrl::~wxListCtrl() 
 409     FreeAllAttrs(TRUE 
/* no need to recreate hash any more */); 
 413         m_textCtrl
->SetHWND(0); 
 414         m_textCtrl
->UnsubclassWin(); 
 419     if (m_ownsImageListNormal
) delete m_imageListNormal
; 
 420     if (m_ownsImageListSmall
) delete m_imageListSmall
; 
 421     if (m_ownsImageListState
) delete m_imageListState
; 
 424 // ---------------------------------------------------------------------------- 
 425 // set/get/change style 
 426 // ---------------------------------------------------------------------------- 
 428 // Add or remove a single window style 
 429 void wxListCtrl::SetSingleStyle(long style
, bool add
) 
 431     long flag 
= GetWindowStyleFlag(); 
 433     // Get rid of conflicting styles 
 436         if ( style 
& wxLC_MASK_TYPE
) 
 437             flag 
= flag 
& ~wxLC_MASK_TYPE
; 
 438         if ( style 
& wxLC_MASK_ALIGN 
) 
 439             flag 
= flag 
& ~wxLC_MASK_ALIGN
; 
 440         if ( style 
& wxLC_MASK_SORT 
) 
 441             flag 
= flag 
& ~wxLC_MASK_SORT
; 
 457     m_windowStyle 
= flag
; 
 462 // Set the whole window style 
 463 void wxListCtrl::SetWindowStyleFlag(long flag
) 
 465     m_windowStyle 
= flag
; 
 470 // Can be just a single style, or a bitlist 
 471 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const 
 474     if ( style 
& wxLC_ICON 
) 
 476         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 477             oldStyle 
-= LVS_SMALLICON
; 
 478         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 479             oldStyle 
-= LVS_REPORT
; 
 480         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 481             oldStyle 
-= LVS_LIST
; 
 485     if ( style 
& wxLC_SMALL_ICON 
) 
 487         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 488             oldStyle 
-= LVS_ICON
; 
 489         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 490             oldStyle 
-= LVS_REPORT
; 
 491         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 492             oldStyle 
-= LVS_LIST
; 
 493         wstyle 
|= LVS_SMALLICON
; 
 496     if ( style 
& wxLC_LIST 
) 
 498         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 499             oldStyle 
-= LVS_ICON
; 
 500         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 501             oldStyle 
-= LVS_REPORT
; 
 502         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 503             oldStyle 
-= LVS_SMALLICON
; 
 507     if ( style 
& wxLC_REPORT 
) 
 509         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 510             oldStyle 
-= LVS_ICON
; 
 511         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 512             oldStyle 
-= LVS_LIST
; 
 513         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 514             oldStyle 
-= LVS_SMALLICON
; 
 516         wstyle 
|= LVS_REPORT
; 
 519     if ( style 
& wxLC_ALIGN_LEFT 
) 
 521         if ( oldStyle 
& LVS_ALIGNTOP 
) 
 522             oldStyle 
-= LVS_ALIGNTOP
; 
 523         wstyle 
|= LVS_ALIGNLEFT
; 
 526     if ( style 
& wxLC_ALIGN_TOP 
) 
 528         if ( oldStyle 
& LVS_ALIGNLEFT 
) 
 529             oldStyle 
-= LVS_ALIGNLEFT
; 
 530         wstyle 
|= LVS_ALIGNTOP
; 
 533     if ( style 
& wxLC_AUTOARRANGE 
) 
 534         wstyle 
|= LVS_AUTOARRANGE
; 
 536     if ( style 
& wxLC_NO_SORT_HEADER 
) 
 537         wstyle 
|= LVS_NOSORTHEADER
; 
 539     if ( style 
& wxLC_NO_HEADER 
) 
 540         wstyle 
|= LVS_NOCOLUMNHEADER
; 
 542     if ( style 
& wxLC_EDIT_LABELS 
) 
 543         wstyle 
|= LVS_EDITLABELS
; 
 545     if ( style 
& wxLC_SINGLE_SEL 
) 
 546         wstyle 
|= LVS_SINGLESEL
; 
 548     if ( style 
& wxLC_SORT_ASCENDING 
) 
 550         if ( oldStyle 
& LVS_SORTDESCENDING 
) 
 551             oldStyle 
-= LVS_SORTDESCENDING
; 
 552         wstyle 
|= LVS_SORTASCENDING
; 
 555     if ( style 
& wxLC_SORT_DESCENDING 
) 
 557         if ( oldStyle 
& LVS_SORTASCENDING 
) 
 558             oldStyle 
-= LVS_SORTASCENDING
; 
 559         wstyle 
|= LVS_SORTDESCENDING
; 
 562 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) 
 563     if ( style 
& wxLC_VIRTUAL 
) 
 565         int ver 
= wxTheApp
->GetComCtl32Version(); 
 568             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."), 
 569                         ver 
/ 100, ver 
% 100); 
 572         wstyle 
|= LVS_OWNERDATA
; 
 579 // ---------------------------------------------------------------------------- 
 581 // ---------------------------------------------------------------------------- 
 583 // Sets the foreground, i.e. text, colour 
 584 bool wxListCtrl::SetForegroundColour(const wxColour
& col
) 
 586     if ( !wxWindow::SetForegroundColour(col
) ) 
 589     ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
)); 
 594 // Sets the background colour 
 595 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
) 
 597     if ( !wxWindow::SetBackgroundColour(col
) ) 
 600     // we set the same colour for both the "empty" background and the items 
 602     COLORREF color 
= wxColourToRGB(col
); 
 603     ListView_SetBkColor(GetHwnd(), color
); 
 604     ListView_SetTextBkColor(GetHwnd(), color
); 
 609 // Gets information about this column 
 610 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const 
 615     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 617         lvCol
.mask 
|= LVCF_TEXT
; 
 618         lvCol
.pszText 
= new wxChar
[513]; 
 619         lvCol
.cchTextMax 
= 512; 
 622     bool success 
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0; 
 624     //  item.m_subItem = lvCol.iSubItem; 
 625     item
.m_width 
= lvCol
.cx
; 
 627     if ( (item
.m_mask 
& wxLIST_MASK_TEXT
) && lvCol
.pszText 
) 
 629         item
.m_text 
= lvCol
.pszText
; 
 630         delete[] lvCol
.pszText
; 
 633     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 635         if (lvCol
.fmt 
== LVCFMT_LEFT
) 
 636             item
.m_format 
= wxLIST_FORMAT_LEFT
; 
 637         else if (lvCol
.fmt 
== LVCFMT_RIGHT
) 
 638             item
.m_format 
= wxLIST_FORMAT_RIGHT
; 
 639         else if (lvCol
.fmt 
== LVCFMT_CENTER
) 
 640             item
.m_format 
= wxLIST_FORMAT_CENTRE
; 
 646 // Sets information about this column 
 647 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
) 
 650     wxConvertToMSWListCol(col
, item
, lvCol
); 
 652     return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0; 
 655 // Gets the column width 
 656 int wxListCtrl::GetColumnWidth(int col
) const 
 658     return ListView_GetColumnWidth(GetHwnd(), col
); 
 661 // Sets the column width 
 662 bool wxListCtrl::SetColumnWidth(int col
, int width
) 
 665     if ( m_windowStyle 
& wxLC_LIST 
) 
 669     if ( width2 
== wxLIST_AUTOSIZE
) 
 670         width2 
= LVSCW_AUTOSIZE
; 
 671     else if ( width2 
== wxLIST_AUTOSIZE_USEHEADER
) 
 672         width2 
= LVSCW_AUTOSIZE_USEHEADER
; 
 674     return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0; 
 677 // Gets the number of items that can fit vertically in the 
 678 // visible area of the list control (list or report view) 
 679 // or the total number of items in the list control (icon 
 680 // or small icon view) 
 681 int wxListCtrl::GetCountPerPage() const 
 683     return ListView_GetCountPerPage(GetHwnd()); 
 686 // Gets the edit control for editing labels. 
 687 wxTextCtrl
* wxListCtrl::GetEditControl() const 
 692 // Gets information about the item 
 693 bool wxListCtrl::GetItem(wxListItem
& info
) const 
 696     wxZeroMemory(lvItem
); 
 698     lvItem
.iItem 
= info
.m_itemId
; 
 699     lvItem
.iSubItem 
= info
.m_col
; 
 701     if ( info
.m_mask 
& wxLIST_MASK_TEXT 
) 
 703         lvItem
.mask 
|= LVIF_TEXT
; 
 704         lvItem
.pszText 
= new wxChar
[513]; 
 705         lvItem
.cchTextMax 
= 512; 
 709         lvItem
.pszText 
= NULL
; 
 712     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
 713         lvItem
.mask 
|= LVIF_PARAM
; 
 715     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
 716         lvItem
.mask 
|= LVIF_IMAGE
; 
 718     if ( info
.m_mask 
& wxLIST_MASK_STATE 
) 
 720         lvItem
.mask 
|= LVIF_STATE
; 
 721         // the other bits are hardly interesting anyhow 
 722         lvItem
.stateMask 
= LVIS_SELECTED 
| LVIS_FOCUSED
; 
 725     bool success 
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0; 
 728         wxLogError(_("Couldn't retrieve information about list control item %d."), 
 733         // give NULL as hwnd as we already have everything we need 
 734         wxConvertFromMSWListItem(NULL
, info
, lvItem
); 
 738         delete[] lvItem
.pszText
; 
 743 // Sets information about the item 
 744 bool wxListCtrl::SetItem(wxListItem
& info
) 
 747     wxConvertToMSWListItem(this, info
, item
); 
 749     // we could be changing only the attribute in which case we don't need to 
 750     // call ListView_SetItem() at all 
 754         if ( !ListView_SetItem(GetHwnd(), &item
) ) 
 756             wxLogDebug(_T("ListView_SetItem() failed")); 
 762     // we need to update the item immediately to show the new image 
 763     bool updateNow 
= (info
.m_mask 
& wxLIST_MASK_IMAGE
) != 0; 
 765     // check whether it has any custom attributes 
 766     if ( info
.HasAttributes() ) 
 768         wxListItemAttr 
*attr 
= (wxListItemAttr 
*)m_attrs
.Get(item
.iItem
); 
 771             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
 773             *attr 
= *info
.GetAttributes(); 
 777         // if the colour has changed, we must redraw the item 
 783         // we need this to make the change visible right now 
 784         RefreshItem(item
.iItem
); 
 790 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
) 
 794     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 795     info
.m_itemId 
= index
; 
 799         info
.m_image 
= imageId
; 
 800         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
 802     return SetItem(info
); 
 806 // Gets the item state 
 807 int wxListCtrl::GetItemState(long item
, long stateMask
) const 
 811     info
.m_mask 
= wxLIST_MASK_STATE
; 
 812     info
.m_stateMask 
= stateMask
; 
 813     info
.m_itemId 
= item
; 
 821 // Sets the item state 
 822 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
) 
 824     // NB: don't use SetItem() here as it doesn't work with the virtual list 
 827     wxZeroMemory(lvItem
); 
 829     wxConvertToMSWFlags(state
, stateMask
, lvItem
); 
 831     // for the virtual list controls we need to refresh the previously focused 
 832     // item manually when changing focus without changing selection 
 833     // programmatically because otherwise it keeps its focus rectangle until 
 834     // next repaint (yet another comctl32 bug) 
 837          (stateMask 
& wxLIST_STATE_FOCUSED
) && 
 838          (state 
& wxLIST_STATE_FOCUSED
) ) 
 840         focusOld 
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
); 
 847     if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
, 
 848                         (WPARAM
)item
, (LPARAM
)&lvItem
) ) 
 850         wxLogLastError(_T("ListView_SetItemState")); 
 855     if ( focusOld 
!= -1 ) 
 857         // no need to refresh the item if it was previously selected, it would 
 858         // only result in annoying flicker 
 859         if ( !(GetItemState(focusOld
, 
 860                             wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) ) 
 862             RefreshItem(focusOld
); 
 869 // Sets the item image 
 870 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
)) 
 874     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
 875     info
.m_image 
= image
; 
 876     info
.m_itemId 
= item
; 
 878     return SetItem(info
); 
 881 // Gets the item text 
 882 wxString 
wxListCtrl::GetItemText(long item
) const 
 886     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 887     info
.m_itemId 
= item
; 
 894 // Sets the item text 
 895 void wxListCtrl::SetItemText(long item
, const wxString
& str
) 
 899     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 900     info
.m_itemId 
= item
; 
 906 // Gets the item data 
 907 long wxListCtrl::GetItemData(long item
) const 
 911     info
.m_mask 
= wxLIST_MASK_DATA
; 
 912     info
.m_itemId 
= item
; 
 919 // Sets the item data 
 920 bool wxListCtrl::SetItemData(long item
, long data
) 
 924     info
.m_mask 
= wxLIST_MASK_DATA
; 
 925     info
.m_itemId 
= item
; 
 928     return SetItem(info
); 
 931 // Gets the item rectangle 
 932 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const 
 937     if ( code 
== wxLIST_RECT_BOUNDS 
) 
 938         codeWin 
= LVIR_BOUNDS
; 
 939     else if ( code 
== wxLIST_RECT_ICON 
) 
 941     else if ( code 
== wxLIST_RECT_LABEL 
) 
 942         codeWin 
= LVIR_LABEL
; 
 945         wxFAIL_MSG( _T("incorrect code in GetItemRect()") ); 
 947         codeWin 
= LVIR_BOUNDS
; 
 951     bool success 
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin 
) != 0; 
 953     bool success 
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0; 
 956     rect
.x 
= rectWin
.left
; 
 957     rect
.y 
= rectWin
.top
; 
 958     rect
.width 
= rectWin
.right 
- rectWin
.left
; 
 959     rect
.height 
= rectWin
.bottom 
- rectWin
.top
; 
 964 // Gets the item position 
 965 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const 
 969     bool success 
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0); 
 971     pos
.x 
= pt
.x
; pos
.y 
= pt
.y
; 
 975 // Sets the item position. 
 976 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
) 
 978     return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0); 
 981 // Gets the number of items in the list control 
 982 int wxListCtrl::GetItemCount() const 
 984     return ListView_GetItemCount(GetHwnd()); 
 987 // Retrieves the spacing between icons in pixels. 
 988 // If small is TRUE, gets the spacing for the small icon 
 989 // view, otherwise the large icon view. 
 990 int wxListCtrl::GetItemSpacing(bool isSmall
) const 
 992     return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
); 
 995 // Gets the number of selected items in the list control 
 996 int wxListCtrl::GetSelectedItemCount() const 
 998     return ListView_GetSelectedCount(GetHwnd()); 
1001 // Gets the text colour of the listview 
1002 wxColour 
wxListCtrl::GetTextColour() const 
1004     COLORREF ref 
= ListView_GetTextColor(GetHwnd()); 
1005     wxColour 
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
)); 
1009 // Sets the text colour of the listview 
1010 void wxListCtrl::SetTextColour(const wxColour
& col
) 
1012     ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue())); 
1015 // Gets the index of the topmost visible item when in 
1016 // list or report view 
1017 long wxListCtrl::GetTopItem() const 
1019     return (long) ListView_GetTopIndex(GetHwnd()); 
1022 // Searches for an item, starting from 'item'. 
1023 // 'geometry' is one of 
1024 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. 
1025 // 'state' is a state bit flag, one or more of 
1026 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. 
1027 // item can be -1 to find the first item that matches the 
1029 // Returns the item or -1 if unsuccessful. 
1030 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const 
1034     if ( geom 
== wxLIST_NEXT_ABOVE 
) 
1035         flags 
|= LVNI_ABOVE
; 
1036     if ( geom 
== wxLIST_NEXT_ALL 
) 
1038     if ( geom 
== wxLIST_NEXT_BELOW 
) 
1039         flags 
|= LVNI_BELOW
; 
1040     if ( geom 
== wxLIST_NEXT_LEFT 
) 
1041         flags 
|= LVNI_TOLEFT
; 
1042     if ( geom 
== wxLIST_NEXT_RIGHT 
) 
1043         flags 
|= LVNI_TORIGHT
; 
1045     if ( state 
& wxLIST_STATE_CUT 
) 
1047     if ( state 
& wxLIST_STATE_DROPHILITED 
) 
1048         flags 
|= LVNI_DROPHILITED
; 
1049     if ( state 
& wxLIST_STATE_FOCUSED 
) 
1050         flags 
|= LVNI_FOCUSED
; 
1051     if ( state 
& wxLIST_STATE_SELECTED 
) 
1052         flags 
|= LVNI_SELECTED
; 
1054     return (long) ListView_GetNextItem(GetHwnd(), item
, flags
); 
1058 wxImageList 
*wxListCtrl::GetImageList(int which
) const 
1060     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1062         return m_imageListNormal
; 
1064     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1066         return m_imageListSmall
; 
1068     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1070         return m_imageListState
; 
1075 void wxListCtrl::SetImageList(wxImageList 
*imageList
, int which
) 
1078     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1080         flags 
= LVSIL_NORMAL
; 
1081         if (m_ownsImageListNormal
) delete m_imageListNormal
; 
1082         m_imageListNormal 
= imageList
; 
1083         m_ownsImageListNormal 
= FALSE
; 
1085     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1087         flags 
= LVSIL_SMALL
; 
1088         if (m_ownsImageListSmall
) delete m_imageListSmall
; 
1089         m_imageListSmall 
= imageList
; 
1090         m_ownsImageListSmall 
= FALSE
; 
1092     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1094         flags 
= LVSIL_STATE
; 
1095         if (m_ownsImageListState
) delete m_imageListState
; 
1096         m_imageListState 
= imageList
; 
1097         m_ownsImageListState 
= FALSE
; 
1099     ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList 
? imageList
->GetHIMAGELIST() : 0, flags
); 
1102 void wxListCtrl::AssignImageList(wxImageList 
*imageList
, int which
) 
1104     SetImageList(imageList
, which
); 
1105     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1106         m_ownsImageListNormal 
= TRUE
; 
1107     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1108         m_ownsImageListSmall 
= TRUE
; 
1109     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1110         m_ownsImageListState 
= TRUE
; 
1113 // ---------------------------------------------------------------------------- 
1115 // ---------------------------------------------------------------------------- 
1117 // Arranges the items 
1118 bool wxListCtrl::Arrange(int flag
) 
1121     if ( flag 
== wxLIST_ALIGN_LEFT 
) 
1122         code 
= LVA_ALIGNLEFT
; 
1123     else if ( flag 
== wxLIST_ALIGN_TOP 
) 
1124         code 
= LVA_ALIGNTOP
; 
1125     else if ( flag 
== wxLIST_ALIGN_DEFAULT 
) 
1127     else if ( flag 
== wxLIST_ALIGN_SNAP_TO_GRID 
) 
1128         code 
= LVA_SNAPTOGRID
; 
1130     return (ListView_Arrange(GetHwnd(), code
) != 0); 
1134 bool wxListCtrl::DeleteItem(long item
) 
1136     if ( !ListView_DeleteItem(GetHwnd(), (int) item
) ) 
1138         wxLogLastError(_T("ListView_DeleteItem")); 
1142     // the virtual list control doesn't refresh itself correctly, help it 
1145         // we need to refresh all the lines below the one which was deleted 
1147         if ( item 
> 0 && GetItemCount() ) 
1149             GetItemRect(item 
- 1, rectItem
); 
1154             rectItem
.height 
= 0; 
1157         wxRect rectWin 
= GetRect(); 
1158         rectWin
.height 
= rectWin
.GetBottom() - rectItem
.GetBottom(); 
1159         rectWin
.y 
= rectItem
.GetBottom(); 
1161         RefreshRect(rectWin
); 
1167 // Deletes all items 
1168 bool wxListCtrl::DeleteAllItems() 
1170     return ListView_DeleteAllItems(GetHwnd()) != 0; 
1173 // Deletes all items 
1174 bool wxListCtrl::DeleteAllColumns() 
1176     while ( m_colCount 
> 0 ) 
1178         if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) 
1180             wxLogLastError(wxT("ListView_DeleteColumn")); 
1188     wxASSERT_MSG( m_colCount 
== 0, wxT("no columns should be left") ); 
1194 bool wxListCtrl::DeleteColumn(int col
) 
1196     bool success 
= (ListView_DeleteColumn(GetHwnd(), col
) != 0); 
1198     if ( success 
&& (m_colCount 
> 0) ) 
1203 // Clears items, and columns if there are any. 
1204 void wxListCtrl::ClearAll() 
1207     if ( m_colCount 
> 0 ) 
1211 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
) 
1213     wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) ); 
1215     // VS: ListView_EditLabel requires that the list has focus. 
1217     HWND hWnd 
= (HWND
) ListView_EditLabel(GetHwnd(), item
); 
1221         m_textCtrl
->SetHWND(0); 
1222         m_textCtrl
->UnsubclassWin(); 
1227     m_textCtrl 
= (wxTextCtrl
*) textControlClass
->CreateObject(); 
1228     m_textCtrl
->SetHWND((WXHWND
) hWnd
); 
1229     m_textCtrl
->SubclassWin((WXHWND
) hWnd
); 
1234 // End label editing, optionally cancelling the edit 
1235 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
)) 
1237     wxFAIL_MSG( _T("not implemented") ); 
1242 // Ensures this item is visible 
1243 bool wxListCtrl::EnsureVisible(long item
) 
1245     return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0; 
1248 // Find an item whose label matches this string, starting from the item after 'start' 
1249 // or the beginning if 'start' is -1. 
1250 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
) 
1252     LV_FINDINFO findInfo
; 
1254     findInfo
.flags 
= LVFI_STRING
; 
1256         findInfo
.flags 
|= LVFI_PARTIAL
; 
1259     // ListView_FindItem() excludes the first item from search and to look 
1260     // through all the items you need to start from -1 which is unnatural and 
1261     // inconsistent with the generic version - so we adjust the index 
1264     return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
); 
1267 // Find an item whose data matches this data, starting from the item after 'start' 
1268 // or the beginning if 'start' is -1. 
1269 long wxListCtrl::FindItem(long start
, long data
) 
1271     LV_FINDINFO findInfo
; 
1273     findInfo
.flags 
= LVFI_PARAM
; 
1274     findInfo
.lParam 
= data
; 
1276     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1279 // Find an item nearest this position in the specified direction, starting from 
1280 // the item after 'start' or the beginning if 'start' is -1. 
1281 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
) 
1283     LV_FINDINFO findInfo
; 
1285     findInfo
.flags 
= LVFI_NEARESTXY
; 
1286     findInfo
.pt
.x 
= pt
.x
; 
1287     findInfo
.pt
.y 
= pt
.y
; 
1288     findInfo
.vkDirection 
= VK_RIGHT
; 
1290     if ( direction 
== wxLIST_FIND_UP 
) 
1291         findInfo
.vkDirection 
= VK_UP
; 
1292     else if ( direction 
== wxLIST_FIND_DOWN 
) 
1293         findInfo
.vkDirection 
= VK_DOWN
; 
1294     else if ( direction 
== wxLIST_FIND_LEFT 
) 
1295         findInfo
.vkDirection 
= VK_LEFT
; 
1296     else if ( direction 
== wxLIST_FIND_RIGHT 
) 
1297         findInfo
.vkDirection 
= VK_RIGHT
; 
1299     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1302 // Determines which item (if any) is at the specified point, 
1303 // giving details in 'flags' (see wxLIST_HITTEST_... flags above) 
1304 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
) 
1306     LV_HITTESTINFO hitTestInfo
; 
1307     hitTestInfo
.pt
.x 
= (int) point
.x
; 
1308     hitTestInfo
.pt
.y 
= (int) point
.y
; 
1310     ListView_HitTest(GetHwnd(), & hitTestInfo
); 
1313     if ( hitTestInfo
.flags 
& LVHT_ABOVE 
) 
1314         flags 
|= wxLIST_HITTEST_ABOVE
; 
1315     if ( hitTestInfo
.flags 
& LVHT_BELOW 
) 
1316         flags 
|= wxLIST_HITTEST_BELOW
; 
1317     if ( hitTestInfo
.flags 
& LVHT_NOWHERE 
) 
1318         flags 
|= wxLIST_HITTEST_NOWHERE
; 
1319     if ( hitTestInfo
.flags 
& LVHT_ONITEMICON 
) 
1320         flags 
|= wxLIST_HITTEST_ONITEMICON
; 
1321     if ( hitTestInfo
.flags 
& LVHT_ONITEMLABEL 
) 
1322         flags 
|= wxLIST_HITTEST_ONITEMLABEL
; 
1323     if ( hitTestInfo
.flags 
& LVHT_ONITEMSTATEICON 
) 
1324         flags 
|= wxLIST_HITTEST_ONITEMSTATEICON
; 
1325     if ( hitTestInfo
.flags 
& LVHT_TOLEFT 
) 
1326         flags 
|= wxLIST_HITTEST_TOLEFT
; 
1327     if ( hitTestInfo
.flags 
& LVHT_TORIGHT 
) 
1328         flags 
|= wxLIST_HITTEST_TORIGHT
; 
1330     return (long) hitTestInfo
.iItem
; 
1333 // Inserts an item, returning the index of the new item if successful, 
1335 long wxListCtrl::InsertItem(wxListItem
& info
) 
1337     wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); 
1340     wxConvertToMSWListItem(this, info
, item
); 
1342     // check whether it has any custom attributes 
1343     if ( info
.HasAttributes() ) 
1346         wxListItemAttr 
*attr
; 
1347         attr 
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
); 
1351             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
1353         else *attr 
= *info
.GetAttributes(); 
1355         m_hasAnyAttr 
= TRUE
; 
1358     return (long) ListView_InsertItem(GetHwnd(), & item
); 
1361 long wxListCtrl::InsertItem(long index
, const wxString
& label
) 
1364     info
.m_text 
= label
; 
1365     info
.m_mask 
= wxLIST_MASK_TEXT
; 
1366     info
.m_itemId 
= index
; 
1367     return InsertItem(info
); 
1370 // Inserts an image item 
1371 long wxListCtrl::InsertItem(long index
, int imageIndex
) 
1374     info
.m_image 
= imageIndex
; 
1375     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
1376     info
.m_itemId 
= index
; 
1377     return InsertItem(info
); 
1380 // Inserts an image/string item 
1381 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
) 
1384     info
.m_image 
= imageIndex
; 
1385     info
.m_text 
= label
; 
1386     info
.m_mask 
= wxLIST_MASK_IMAGE 
| wxLIST_MASK_TEXT
; 
1387     info
.m_itemId 
= index
; 
1388     return InsertItem(info
); 
1391 // For list view mode (only), inserts a column. 
1392 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
) 
1395     wxConvertToMSWListCol(col
, item
, lvCol
); 
1397     if ( !(lvCol
.mask 
& LVCF_WIDTH
) ) 
1399         // always give some width to the new column: this one is compatible 
1400         // with the generic version 
1401         lvCol
.mask 
|= LVCF_WIDTH
; 
1405     // when we insert a column which can contain an image, we must specify this 
1406     // flag right now as doing it later in SetColumn() has no effect 
1408     // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no 
1409     // way to dynamically set/clear the bitmap as the column without a bitmap 
1410     // on the left looks ugly (there is a hole) 
1412     // unfortunately with my version of comctl32.dll (5.80), the left column 
1413     // image is always on the left and it seems that it's a "feature" - I 
1414     // didn't find any way to work around it in any case 
1415     if ( lvCol
.mask 
& LVCF_IMAGE 
) 
1417         lvCol
.mask 
|= LVCF_FMT
; 
1418         lvCol
.fmt 
|= LVCFMT_BITMAP_ON_RIGHT
; 
1421     bool success 
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1; 
1428         wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), 
1435 long wxListCtrl::InsertColumn(long col
, 
1436                               const wxString
& heading
, 
1441     item
.m_mask 
= wxLIST_MASK_TEXT 
| wxLIST_MASK_FORMAT
; 
1442     item
.m_text 
= heading
; 
1445         item
.m_mask 
|= wxLIST_MASK_WIDTH
; 
1446         item
.m_width 
= width
; 
1448     item
.m_format 
= format
; 
1450     return InsertColumn(col
, item
); 
1453 // Scrolls the list control. If in icon, small icon or report view mode, 
1454 // x specifies the number of pixels to scroll. If in list view mode, x 
1455 // specifies the number of columns to scroll. 
1456 // If in icon, small icon or list view mode, y specifies the number of pixels 
1457 // to scroll. If in report view mode, y specifies the number of lines to scroll. 
1458 bool wxListCtrl::ScrollList(int dx
, int dy
) 
1460     return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0); 
1465 // fn is a function which takes 3 long arguments: item1, item2, data. 
1466 // item1 is the long data associated with a first item (NOT the index). 
1467 // item2 is the long data associated with a second item (NOT the index). 
1468 // data is the same value as passed to SortItems. 
1469 // The return value is a negative number if the first item should precede the second 
1470 // item, a positive number of the second item should precede the first, 
1471 // or zero if the two items are equivalent. 
1473 // data is arbitrary data to be passed to the sort function. 
1475 // FIXME: this is horrible and MT-unsafe and everything else but I don't have 
1476 //        time for anything better right now (VZ) 
1477 static long gs_sortData 
= 0; 
1478 static wxListCtrl 
*gs_sortCtrl 
= NULL
; 
1479 static wxListCtrlCompare gs_sortFunction 
= NULL
; 
1481 int wxCMPFUNC_CONV 
wxListCtrlCompareFn(const void *arg1
, const void *arg2
) 
1483     int n1 
= *(const int *)arg1
, 
1484         n2 
= *(const int *)arg2
; 
1486     return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
), 
1487                            gs_sortCtrl
->GetItemData(n2
), 
1491 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
) 
1493     // sort the attributes too 
1497             count 
= GetItemCount(); 
1498         int *aItems 
= new int[count
]; 
1499         for ( n 
= 0; n 
< count
; n
++ ) 
1506         gs_sortFunction 
= fn
; 
1508         qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
); 
1512         gs_sortFunction 
= NULL
; 
1514         wxHashTable 
attrsNew(wxKEY_INTEGER
, 1000); 
1515         for ( n 
= 0; n 
< count
; n
++ ) 
1517             wxObject 
*attr 
= m_attrs
.Delete(aItems
[n
]); 
1520                 attrsNew
.Put(n
, attr
); 
1530     if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) ) 
1532         wxLogDebug(_T("ListView_SortItems() failed")); 
1542 // ---------------------------------------------------------------------------- 
1543 // message processing 
1544 // ---------------------------------------------------------------------------- 
1546 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
) 
1548     if (cmd 
== EN_UPDATE
) 
1550         wxCommandEvent 
event(wxEVT_COMMAND_TEXT_UPDATED
, id
); 
1551         event
.SetEventObject( this ); 
1552         ProcessCommand(event
); 
1555     else if (cmd 
== EN_KILLFOCUS
) 
1557         wxCommandEvent 
event(wxEVT_KILL_FOCUS
, id
); 
1558         event
.SetEventObject( this ); 
1559         ProcessCommand(event
); 
1566 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM 
*result
) 
1568     // prepare the event 
1569     // ----------------- 
1571     wxListEvent 
event(wxEVT_NULL
, m_windowId
); 
1572     event
.SetEventObject(this); 
1574     wxEventType eventType 
= wxEVT_NULL
; 
1576     NMHDR 
*nmhdr 
= (NMHDR 
*)lParam
; 
1578     // if your compiler is as broken as this, you should really change it: this 
1579     // code is needed for normal operation! #ifdef below is only useful for 
1580     // automatic rebuilds which are done with a very old compiler version 
1581 #ifdef HDN_BEGINTRACKA 
1583     // check for messages from the header (in report view) 
1584     HWND hwndHdr 
= ListView_GetHeader(GetHwnd()); 
1586     // is it a message from the header? 
1587     if ( nmhdr
->hwndFrom 
== hwndHdr 
) 
1589         HD_NOTIFY 
*nmHDR 
= (HD_NOTIFY 
*)nmhdr
; 
1591         event
.m_itemIndex 
= -1; 
1593         switch ( nmhdr
->code 
) 
1595             // yet another comctl32.dll bug: under NT/W2K it sends Unicode 
1596             // TRACK messages even to ANSI programs: on my system I get 
1597             // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all! 
1599             // work around is to simply catch both versions and hope that it 
1600             // works (why should this message exist in ANSI and Unicode is 
1601             // beyond me as it doesn't deal with strings at all...) 
1602             case HDN_BEGINTRACKA
: 
1603             case HDN_BEGINTRACKW
: 
1604                 eventType 
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
; 
1609                 if ( eventType 
== wxEVT_NULL 
) 
1610                     eventType 
= wxEVT_COMMAND_LIST_COL_DRAGGING
; 
1615                 if ( eventType 
== wxEVT_NULL 
) 
1616                     eventType 
= wxEVT_COMMAND_LIST_COL_END_DRAG
; 
1617                 event
.m_col 
= nmHDR
->iItem
; 
1622                     eventType 
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
; 
1625                     // find the column clicked: we have to search for it 
1626                     // ourselves as the notification message doesn't provide 
1629                     // where did the click occur? 
1631                     if ( !::GetCursorPos(&ptClick
) ) 
1633                         wxLogLastError(_T("GetCursorPos")); 
1636                     if ( !::ScreenToClient(hwndHdr
, &ptClick
) ) 
1638                         wxLogLastError(_T("ScreenToClient(listctrl header)")); 
1641                     event
.m_pointDrag
.x 
= ptClick
.x
; 
1642                     event
.m_pointDrag
.y 
= ptClick
.y
; 
1644                     int colCount 
= Header_GetItemCount(hwndHdr
); 
1647                     for ( int col 
= 0; col 
< colCount
; col
++ ) 
1649                         if ( Header_GetItemRect(hwndHdr
, col
, &rect
) ) 
1651                             if ( ::PtInRect(&rect
, ptClick
) ) 
1662                 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
1666 #endif // defined(HDN_BEGINTRACKA) 
1667         if ( nmhdr
->hwndFrom 
== GetHwnd() ) 
1669         // almost all messages use NM_LISTVIEW 
1670         NM_LISTVIEW 
*nmLV 
= (NM_LISTVIEW 
*)nmhdr
; 
1672         // this is true for almost all events 
1673         event
.m_item
.m_data 
= nmLV
->lParam
; 
1675         switch ( nmhdr
->code 
) 
1677             case LVN_BEGINRDRAG
: 
1678                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
; 
1682                 if ( eventType 
== wxEVT_NULL 
) 
1684                     eventType 
= wxEVT_COMMAND_LIST_BEGIN_DRAG
; 
1687                 event
.m_itemIndex 
= nmLV
->iItem
; 
1688                 event
.m_pointDrag
.x 
= nmLV
->ptAction
.x
; 
1689                 event
.m_pointDrag
.y 
= nmLV
->ptAction
.y
; 
1692             // NB: we have to handle both *A and *W versions here because some 
1693             //    versions of comctl32.dll send ANSI message to an Unicode app 
1694             case LVN_BEGINLABELEDITA
: 
1696                     eventType 
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
; 
1697                     wxLV_ITEM 
item(((LV_DISPINFOA 
*)lParam
)->item
); 
1698                     wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
); 
1699                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1702             case LVN_BEGINLABELEDITW
: 
1704                     eventType 
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
; 
1705                     wxLV_ITEM 
item(((LV_DISPINFOW 
*)lParam
)->item
); 
1706                     wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
); 
1707                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1711             case LVN_ENDLABELEDITA
: 
1713                     eventType 
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
; 
1714                     wxLV_ITEM 
item(((LV_DISPINFOA 
*)lParam
)->item
); 
1715                     wxConvertFromMSWListItem(NULL
, event
.m_item
, item
); 
1716                     if ( ((LV_ITEM
)item
).pszText 
== NULL 
||  
1717                          ((LV_ITEM
)item
).iItem 
== -1 ) 
1720                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1723             case LVN_ENDLABELEDITW
: 
1725                     eventType 
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
; 
1726                     wxLV_ITEM 
item(((LV_DISPINFOW 
*)lParam
)->item
); 
1727                     wxConvertFromMSWListItem(NULL
, event
.m_item
, item
); 
1728                     if ( ((LV_ITEM
)item
).pszText 
== NULL 
||  
1729                          ((LV_ITEM
)item
).iItem 
== -1 ) 
1732                     event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1736             case LVN_COLUMNCLICK
: 
1737                 eventType 
= wxEVT_COMMAND_LIST_COL_CLICK
; 
1738                 event
.m_itemIndex 
= -1; 
1739                 event
.m_col 
= nmLV
->iSubItem
; 
1742             case LVN_DELETEALLITEMS
: 
1743                 eventType 
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
; 
1744                 event
.m_itemIndex 
= -1; 
1750             case LVN_DELETEITEM
: 
1751                 eventType 
= wxEVT_COMMAND_LIST_DELETE_ITEM
; 
1752                 event
.m_itemIndex 
= nmLV
->iItem
; 
1756                     delete (wxListItemAttr 
*)m_attrs
.Delete(nmLV
->iItem
); 
1760             case LVN_SETDISPINFO
: 
1762                     eventType 
= wxEVT_COMMAND_LIST_SET_INFO
; 
1763                     LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1764                     wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1768             case LVN_INSERTITEM
: 
1769                 eventType 
= wxEVT_COMMAND_LIST_INSERT_ITEM
; 
1770                 event
.m_itemIndex 
= nmLV
->iItem
; 
1773             case LVN_ITEMCHANGED
: 
1774                 // we translate this catch all message into more interesting 
1775                 // (and more easy to process) wxWindows events 
1777                 // first of all, we deal with the state change events only 
1778                 if ( nmLV
->uChanged 
& LVIF_STATE 
) 
1780                     // temp vars for readability 
1781                     const UINT stOld 
= nmLV
->uOldState
; 
1782                     const UINT stNew 
= nmLV
->uNewState
; 
1784                     // has the focus changed? 
1785                     if ( !(stOld 
& LVIS_FOCUSED
) && (stNew 
& LVIS_FOCUSED
) ) 
1787                         eventType 
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
; 
1788                         event
.m_itemIndex 
= nmLV
->iItem
; 
1791                     if ( (stNew 
& LVIS_SELECTED
) != (stOld 
& LVIS_SELECTED
) ) 
1793                         if ( eventType 
!= wxEVT_NULL 
) 
1795                             // focus and selection have both changed: send the 
1796                             // focus event from here and the selection one 
1798                             event
.SetEventType(eventType
); 
1799                             (void)GetEventHandler()->ProcessEvent(event
); 
1801                         else // no focus event to send 
1803                             // then need to set m_itemIndex as it wasn't done 
1805                             event
.m_itemIndex 
= nmLV
->iItem
; 
1808                         eventType 
= stNew 
& LVIS_SELECTED
 
1809                                         ? wxEVT_COMMAND_LIST_ITEM_SELECTED
 
1810                                         : wxEVT_COMMAND_LIST_ITEM_DESELECTED
; 
1814                 if ( eventType 
== wxEVT_NULL 
) 
1816                     // not an interesting event for us 
1824                     LV_KEYDOWN 
*info 
= (LV_KEYDOWN 
*)lParam
; 
1825                     WORD wVKey 
= info
->wVKey
; 
1827                     // get the current selection 
1828                     long lItem 
= GetNextItem(-1, 
1830                                              wxLIST_STATE_SELECTED
); 
1832                     // <Enter> or <Space> activate the selected item if any (but 
1833                     // not with Shift and/or Ctrl as then they have a predefined 
1834                     // meaning for the list view) 
1836                          (wVKey 
== VK_RETURN 
|| wVKey 
== VK_SPACE
) && 
1837                          !(wxIsShiftDown() || wxIsCtrlDown()) ) 
1839                         eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1843                         eventType 
= wxEVT_COMMAND_LIST_KEY_DOWN
; 
1845                         // wxCharCodeMSWToWX() returns 0 if the key is an ASCII 
1846                         // value which should be used as is 
1847                         int code 
= wxCharCodeMSWToWX(wVKey
); 
1848                         event
.m_code 
= code 
? code 
: wVKey
; 
1852                     event
.m_item
.m_itemId 
= lItem
; 
1856                         // fill the other fields too 
1857                         event
.m_item
.m_text 
= GetItemText(lItem
); 
1858                         event
.m_item
.m_data 
= GetItemData(lItem
); 
1864                 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do 
1866                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1871                 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event 
1872                 // if it happened on an item (and not on empty place) 
1873                 if ( nmLV
->iItem 
== -1 ) 
1879                 eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1880                 event
.m_itemIndex 
= nmLV
->iItem
; 
1881                 event
.m_item
.m_text 
= GetItemText(nmLV
->iItem
); 
1882                 event
.m_item
.m_data 
= GetItemData(nmLV
->iItem
); 
1886                 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), 
1887                 // don't do anything else 
1888                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1893                 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event 
1894                 LV_HITTESTINFO lvhti
; 
1895                 wxZeroMemory(lvhti
); 
1897                 ::GetCursorPos(&(lvhti
.pt
)); 
1898                 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
)); 
1899                 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 ) 
1901                     if ( lvhti
.flags 
& LVHT_ONITEM 
) 
1903                         eventType 
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
; 
1904                         event
.m_itemIndex 
= lvhti
.iItem
; 
1905                         event
.m_pointDrag
.x 
= lvhti
.pt
.x
; 
1906                         event
.m_pointDrag
.y 
= lvhti
.pt
.y
; 
1911 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ 
1912         && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) 
1914                 *result 
= OnCustomDraw(lParam
); 
1917 #endif // _WIN32_IE >= 0x300 
1919             case LVN_ODCACHEHINT
: 
1921                     const NM_CACHEHINT 
*cacheHint 
= (NM_CACHEHINT 
*)lParam
; 
1923                     eventType 
= wxEVT_COMMAND_LIST_CACHE_HINT
; 
1925                     // we get some really stupid cache hints like ones for items in 
1926                     // range 0..0 for an empty control or, after deleting an item, 
1927                     // for items in invalid range - filter this garbage out 
1928                     if ( cacheHint
->iFrom 
< cacheHint
->iTo 
) 
1930                         event
.m_oldItemIndex 
= cacheHint
->iFrom
; 
1932                         long iMax 
= GetItemCount(); 
1933                         event
.m_itemIndex 
= cacheHint
->iTo 
< iMax 
? cacheHint
->iTo
 
1943             case LVN_GETDISPINFO
: 
1946                     LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1948                     LV_ITEM
& lvi 
= info
->item
; 
1949                     long item 
= lvi
.iItem
; 
1951                     if ( lvi
.mask 
& LVIF_TEXT 
) 
1953                         wxString text 
= OnGetItemText(item
, lvi
.iSubItem
); 
1954                         wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
); 
1957 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ 
1958         && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) ) 
1959                     if ( lvi
.mask 
& LVIF_IMAGE 
) 
1961                         lvi
.iImage 
= OnGetItemImage(item
); 
1965                     // a little dose of healthy paranoia: as we never use 
1966                     // LVM_SETCALLBACKMASK we're not supposed to get these ones 
1967                     wxASSERT_MSG( !(lvi
.mask 
& LVIF_STATE
), 
1968                                   _T("we don't support state callbacks yet!") ); 
1975                 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
1980         // where did this one come from? 
1984     // process the event 
1985     // ----------------- 
1987     event
.SetEventType(eventType
); 
1989     if ( !GetEventHandler()->ProcessEvent(event
) ) 
1995     switch ( nmhdr
->code 
) 
1997         case LVN_DELETEALLITEMS
: 
1998             // always return TRUE to suppress all additional LVN_DELETEITEM 
1999             // notifications - this makes deleting all items from a list ctrl 
2005         case LVN_ENDLABELEDITA
: 
2006         case LVN_ENDLABELEDITW
: 
2007             // logic here is inversed compared to all the other messages 
2008             *result 
= event
.IsAllowed(); 
2013     *result 
= !event
.IsAllowed(); 
2018 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 
2020 WXLPARAM 
wxListCtrl::OnCustomDraw(WXLPARAM lParam
) 
2022     LPNMLVCUSTOMDRAW lplvcd 
= (LPNMLVCUSTOMDRAW
)lParam
; 
2023     NMCUSTOMDRAW
& nmcd 
= lplvcd
->nmcd
; 
2024     switch ( nmcd
.dwDrawStage 
) 
2027             // if we've got any items with non standard attributes, 
2028             // notify us before painting each item 
2030             // for virtual controls, always suppose that we have attributes as 
2031             // there is no way to check for this 
2032             return IsVirtual() || m_hasAnyAttr 
? CDRF_NOTIFYITEMDRAW
 
2035         case CDDS_ITEMPREPAINT
: 
2037                 size_t item 
= (size_t)nmcd
.dwItemSpec
; 
2038                 if ( item 
>= (size_t)GetItemCount() ) 
2040                     // we get this message with item == 0 for an empty control, 
2041                     // we must ignore it as calling OnGetItemAttr() would be 
2043                     return CDRF_DODEFAULT
; 
2046                 wxListItemAttr 
*attr 
= 
2047                     IsVirtual() ? OnGetItemAttr(item
) 
2048                                 : (wxListItemAttr 
*)m_attrs
.Get(item
); 
2052                     // nothing to do for this item 
2053                     return CDRF_DODEFAULT
; 
2057                 wxColour colText
, colBack
; 
2058                 if ( attr
->HasFont() ) 
2060                     wxFont font 
= attr
->GetFont(); 
2061                     hFont 
= (HFONT
)font
.GetResourceHandle(); 
2068                 if ( attr
->HasTextColour() ) 
2070                     colText 
= attr
->GetTextColour(); 
2074                     colText 
= GetTextColour(); 
2077                 if ( attr
->HasBackgroundColour() ) 
2079                     colBack 
= attr
->GetBackgroundColour(); 
2083                     colBack 
= GetBackgroundColour(); 
2086                 lplvcd
->clrText 
= wxColourToRGB(colText
); 
2087                 lplvcd
->clrTextBk 
= wxColourToRGB(colBack
); 
2089                 // note that if we wanted to set colours for 
2090                 // individual columns (subitems), we would have 
2091                 // returned CDRF_NOTIFYSUBITEMREDRAW from here 
2094                     ::SelectObject(nmcd
.hdc
, hFont
); 
2096                     return CDRF_NEWFONT
; 
2099             // fall through to return CDRF_DODEFAULT 
2102             return CDRF_DODEFAULT
; 
2106 #endif // NM_CUSTOMDRAW supported 
2108 // Necessary for drawing hrules and vrules, if specified 
2109 void wxListCtrl::OnPaint(wxPaintEvent
& event
) 
2113     wxControl::OnPaint(event
); 
2115     // Reset the device origin since it may have been set 
2116     dc
.SetDeviceOrigin(0, 0); 
2118     bool drawHRules 
= ((GetWindowStyle() & wxLC_HRULES
) != 0); 
2119     bool drawVRules 
= ((GetWindowStyle() & wxLC_VRULES
) != 0); 
2121     if (!drawHRules 
&& !drawVRules
) 
2123     if ((GetWindowStyle() & wxLC_REPORT
) == 0) 
2126     wxPen 
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
); 
2128     dc
.SetBrush(* wxTRANSPARENT_BRUSH
); 
2130     wxSize clientSize 
= GetClientSize(); 
2134     int itemCount 
= GetItemCount(); 
2138         long top 
= GetTopItem(); 
2139         for (i 
= top
; i 
< top 
+ GetCountPerPage() + 1; i
++) 
2141             if (GetItemRect(i
, itemRect
)) 
2143                 cy 
= itemRect
.GetTop(); 
2144                 if (i 
!= 0) // Don't draw the first one 
2146                     dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
2149                 if (i 
== itemCount 
- 1) 
2151                     cy 
= itemRect
.GetBottom(); 
2152                     dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
2158     if (drawVRules 
&& (i 
> -1)) 
2160         wxRect firstItemRect
; 
2161         GetItemRect(0, firstItemRect
); 
2163         if (GetItemRect(i
, itemRect
)) 
2166             int x 
= itemRect
.GetX(); 
2167             for (col 
= 0; col 
< GetColumnCount(); col
++) 
2169                 int colWidth 
= GetColumnWidth(col
); 
2171                 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom()); 
2177 // ---------------------------------------------------------------------------- 
2178 // virtual list controls 
2179 // ---------------------------------------------------------------------------- 
2181 wxString 
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const 
2183     // this is a pure virtual function, in fact - which is not really pure 
2184     // because the controls which are not virtual don't need to implement it 
2185     wxFAIL_MSG( _T("not supposed to be called") ); 
2187     return wxEmptyString
; 
2190 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const 
2193     wxFAIL_MSG( _T("not supposed to be called") ); 
2198 wxListItemAttr 
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const 
2200     wxASSERT_MSG( item 
>= 0 && item 
< GetItemCount(), 
2201                   _T("invalid item index in OnGetItemAttr()") ); 
2203     // no attributes by default 
2207 void wxListCtrl::SetItemCount(long count
) 
2209     wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); 
2211     if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) ) 
2213         wxLogLastError(_T("ListView_SetItemCount")); 
2217 void wxListCtrl::RefreshItem(long item
) 
2219     // strangely enough, ListView_Update() results in much more flicker here 
2220     // than a dumb Refresh() -- why? 
2222     if ( !ListView_Update(GetHwnd(), item
) ) 
2224         wxLogLastError(_T("ListView_Update")); 
2228     GetItemRect(item
, rect
); 
2233 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
) 
2235     wxRect rect1
, rect2
; 
2236     GetItemRect(itemFrom
, rect1
); 
2237     GetItemRect(itemTo
, rect2
); 
2239     wxRect rect 
= rect1
; 
2240     rect
.height 
= rect2
.GetBottom() - rect1
.GetTop(); 
2245 // ---------------------------------------------------------------------------- 
2247 // ---------------------------------------------------------------------------- 
2249 // List item structure 
2250 wxListItem::wxListItem() 
2260     m_format 
= wxLIST_FORMAT_CENTRE
; 
2266 void wxListItem::Clear() 
2275     m_format 
= wxLIST_FORMAT_CENTRE
; 
2277     m_text 
= wxEmptyString
; 
2279     if (m_attr
) delete m_attr
; 
2283 void wxListItem::ClearAttributes() 
2285     if (m_attr
) delete m_attr
; 
2289 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
2293     info
.m_data 
= lvItem
.lParam
; 
2296     info
.m_stateMask 
= 0; 
2297     info
.m_itemId 
= lvItem
.iItem
; 
2299     long oldMask 
= lvItem
.mask
; 
2301     bool needText 
= FALSE
; 
2302     if (hwndListCtrl 
!= 0) 
2304         if ( lvItem
.mask 
& LVIF_TEXT 
) 
2311             lvItem
.pszText 
= new wxChar
[513]; 
2312             lvItem
.cchTextMax 
= 512; 
2314         lvItem
.mask 
|= LVIF_TEXT 
| LVIF_IMAGE 
| LVIF_PARAM
; 
2315         ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
); 
2318     if ( lvItem
.mask 
& LVIF_STATE 
) 
2320         info
.m_mask 
|= wxLIST_MASK_STATE
; 
2322         if ( lvItem
.stateMask 
& LVIS_CUT
) 
2324             info
.m_stateMask 
|= wxLIST_STATE_CUT
; 
2325             if ( lvItem
.state 
& LVIS_CUT 
) 
2326                 info
.m_state 
|= wxLIST_STATE_CUT
; 
2328         if ( lvItem
.stateMask 
& LVIS_DROPHILITED
) 
2330             info
.m_stateMask 
|= wxLIST_STATE_DROPHILITED
; 
2331             if ( lvItem
.state 
& LVIS_DROPHILITED 
) 
2332                 info
.m_state 
|= wxLIST_STATE_DROPHILITED
; 
2334         if ( lvItem
.stateMask 
& LVIS_FOCUSED
) 
2336             info
.m_stateMask 
|= wxLIST_STATE_FOCUSED
; 
2337             if ( lvItem
.state 
& LVIS_FOCUSED 
) 
2338                 info
.m_state 
|= wxLIST_STATE_FOCUSED
; 
2340         if ( lvItem
.stateMask 
& LVIS_SELECTED
) 
2342             info
.m_stateMask 
|= wxLIST_STATE_SELECTED
; 
2343             if ( lvItem
.state 
& LVIS_SELECTED 
) 
2344                 info
.m_state 
|= wxLIST_STATE_SELECTED
; 
2348     if ( lvItem
.mask 
& LVIF_TEXT 
) 
2350         info
.m_mask 
|= wxLIST_MASK_TEXT
; 
2351         info
.m_text 
= lvItem
.pszText
; 
2353     if ( lvItem
.mask 
& LVIF_IMAGE 
) 
2355         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
2356         info
.m_image 
= lvItem
.iImage
; 
2358     if ( lvItem
.mask 
& LVIF_PARAM 
) 
2359         info
.m_mask 
|= wxLIST_MASK_DATA
; 
2360     if ( lvItem
.mask 
& LVIF_DI_SETITEM 
) 
2361         info
.m_mask 
|= wxLIST_SET_ITEM
; 
2362     info
.m_col 
= lvItem
.iSubItem
; 
2367             delete[] lvItem
.pszText
; 
2369     lvItem
.mask 
= oldMask
; 
2372 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
) 
2374     if (stateMask 
& wxLIST_STATE_CUT
) 
2376         lvItem
.stateMask 
|= LVIS_CUT
; 
2377         if (state 
& wxLIST_STATE_CUT
) 
2378             lvItem
.state 
|= LVIS_CUT
; 
2380     if (stateMask 
& wxLIST_STATE_DROPHILITED
) 
2382         lvItem
.stateMask 
|= LVIS_DROPHILITED
; 
2383         if (state 
& wxLIST_STATE_DROPHILITED
) 
2384             lvItem
.state 
|= LVIS_DROPHILITED
; 
2386     if (stateMask 
& wxLIST_STATE_FOCUSED
) 
2388         lvItem
.stateMask 
|= LVIS_FOCUSED
; 
2389         if (state 
& wxLIST_STATE_FOCUSED
) 
2390             lvItem
.state 
|= LVIS_FOCUSED
; 
2392     if (stateMask 
& wxLIST_STATE_SELECTED
) 
2394         lvItem
.stateMask 
|= LVIS_SELECTED
; 
2395         if (state 
& wxLIST_STATE_SELECTED
) 
2396             lvItem
.state 
|= LVIS_SELECTED
; 
2400 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
2401                                    const wxListItem
& info
, 
2404     lvItem
.iItem 
= (int) info
.m_itemId
; 
2406     lvItem
.iImage 
= info
.m_image
; 
2407     lvItem
.lParam 
= info
.m_data
; 
2408     lvItem
.stateMask 
= 0; 
2411     lvItem
.iSubItem 
= info
.m_col
; 
2413     if (info
.m_mask 
& wxLIST_MASK_STATE
) 
2415         lvItem
.mask 
|= LVIF_STATE
; 
2417         wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
); 
2420     if (info
.m_mask 
& wxLIST_MASK_TEXT
) 
2422         lvItem
.mask 
|= LVIF_TEXT
; 
2423         if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT 
) 
2425             lvItem
.pszText 
= LPSTR_TEXTCALLBACK
; 
2429             // pszText is not const, hence the cast 
2430             lvItem
.pszText 
= (wxChar 
*)info
.m_text
.c_str(); 
2431             if ( lvItem
.pszText 
) 
2432                 lvItem
.cchTextMax 
= info
.m_text
.Length(); 
2434                 lvItem
.cchTextMax 
= 0; 
2437     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
2438         lvItem
.mask 
|= LVIF_IMAGE
; 
2439     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
2440         lvItem
.mask 
|= LVIF_PARAM
; 
2443 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
, 
2446     wxZeroMemory(lvCol
); 
2448     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
2450         lvCol
.mask 
|= LVCF_TEXT
; 
2451         lvCol
.pszText 
= (wxChar 
*)item
.m_text
.c_str(); // cast is safe 
2454     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
2456         lvCol
.mask 
|= LVCF_FMT
; 
2458         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
2459             lvCol
.fmt 
= LVCFMT_LEFT
; 
2460         else if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
2461             lvCol
.fmt 
= LVCFMT_RIGHT
; 
2462         else if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
2463             lvCol
.fmt 
= LVCFMT_CENTER
; 
2466     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
2468         lvCol
.mask 
|= LVCF_WIDTH
; 
2469         if ( item
.m_width 
== wxLIST_AUTOSIZE
) 
2470             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
2471         else if ( item
.m_width 
== wxLIST_AUTOSIZE_USEHEADER
) 
2472             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
2474             lvCol
.cx 
= item
.m_width
; 
2477 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ 
2478         && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) ) 
2479     if ( item
.m_mask 
& wxLIST_MASK_IMAGE 
) 
2481         if ( wxTheApp
->GetComCtl32Version() >= 470 ) 
2483             lvCol
.mask 
|= LVCF_IMAGE
; 
2484             lvCol
.iImage 
= item
.m_image
; 
2486         //else: it doesn't support item images anyhow 
2491 #endif // wxUSE_LISTCTRL