1 ///////////////////////////////////////////////////////////////////////////// 
   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 
  71 // ---------------------------------------------------------------------------- 
  73 // ---------------------------------------------------------------------------- 
  75 // convert our state and mask flags to LV_ITEM constants 
  76 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
); 
  78 // convert wxListItem to LV_ITEM 
  79 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
  80                                    const wxListItem
& info
, LV_ITEM
& lvItem
); 
  82 // convert LV_ITEM to wxListItem 
  83 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
  85                                      /* const */ LV_ITEM
& lvItem
); 
  87 // ---------------------------------------------------------------------------- 
  89 // ---------------------------------------------------------------------------- 
  91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
) 
  92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
) 
  93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
) 
  94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
) 
  95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
) 
  96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
) 
  97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
) 
  98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
) 
  99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
) 
 100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
) 
 101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
) 
 102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
) 
 103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
) 
 104 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
) 
 105 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
) 
 106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
) 
 108 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
) 
 109 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
) 
 111 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
) 
 112     EVT_PAINT(wxListCtrl::OnPaint
) 
 115 // ============================================================================ 
 117 // ============================================================================ 
 119 // ---------------------------------------------------------------------------- 
 121 // ---------------------------------------------------------------------------- 
 123 void wxListEvent::CopyObject(wxObject
& object_dest
) const 
 125     wxListEvent 
*obj 
= (wxListEvent 
*)&object_dest
; 
 127     wxNotifyEvent::CopyObject(object_dest
); 
 129     obj
->m_code 
= m_code
; 
 130     obj
->m_itemIndex 
= m_itemIndex
; 
 131     obj
->m_oldItemIndex 
= m_oldItemIndex
; 
 133     obj
->m_cancelled 
= m_cancelled
; 
 134     obj
->m_pointDrag 
= m_pointDrag
; 
 135     obj
->m_item
.m_mask 
= m_item
.m_mask
; 
 136     obj
->m_item
.m_itemId 
= m_item
.m_itemId
; 
 137     obj
->m_item
.m_col 
= m_item
.m_col
; 
 138     obj
->m_item
.m_state 
= m_item
.m_state
; 
 139     obj
->m_item
.m_stateMask 
= m_item
.m_stateMask
; 
 140     obj
->m_item
.m_text 
= m_item
.m_text
; 
 141     obj
->m_item
.m_image 
= m_item
.m_image
; 
 142     obj
->m_item
.m_data 
= m_item
.m_data
; 
 143     obj
->m_item
.m_format 
= m_item
.m_format
; 
 144     obj
->m_item
.m_width 
= m_item
.m_width
; 
 146     if ( m_item
.HasAttributes() ) 
 148         obj
->m_item
.SetTextColour(m_item
.GetTextColour()); 
 149         obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour()); 
 150         obj
->m_item
.SetFont(m_item
.GetFont()); 
 154 // ---------------------------------------------------------------------------- 
 155 // wxListCtrl construction 
 156 // ---------------------------------------------------------------------------- 
 158 void wxListCtrl::Init() 
 160     m_imageListNormal 
= NULL
; 
 161     m_imageListSmall 
= NULL
; 
 162     m_imageListState 
= NULL
; 
 163     m_ownsImageListNormal 
= m_ownsImageListSmall 
= m_ownsImageListState 
= FALSE
; 
 167     m_hasAnyAttr 
= FALSE
; 
 170 bool wxListCtrl::Create(wxWindow 
*parent
, 
 175                         const wxValidator
& validator
, 
 176                         const wxString
& name
) 
 179     SetValidator(validator
); 
 180 #endif // wxUSE_VALIDATORS 
 189     m_windowStyle 
= style
; 
 202     m_windowId 
= (id 
== -1) ? NewControlId() : id
; 
 204     DWORD wstyle 
= WS_VISIBLE 
| WS_CHILD 
| WS_TABSTOP 
| 
 205                    LVS_SHAREIMAGELISTS 
| LVS_SHOWSELALWAYS
; 
 207     if ( m_windowStyle 
& wxCLIP_SIBLINGS 
) 
 208         wstyle 
|= WS_CLIPSIBLINGS
; 
 210     if ( wxStyleHasBorder(m_windowStyle
) ) 
 212     m_baseStyle 
= wstyle
; 
 214     if ( !DoCreateControl(x
, y
, width
, height
) ) 
 218         parent
->AddChild(this); 
 223 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
) 
 225     DWORD wstyle 
= m_baseStyle
; 
 228     WXDWORD exStyle 
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
); 
 230     // Even with extended styles, need to combine with WS_BORDER 
 231     // for them to look right. 
 235     long oldStyle 
= 0; // Dummy 
 236     wstyle 
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
); 
 238     // Create the ListView control. 
 239     m_hWnd 
= (WXHWND
)CreateWindowEx(exStyle
, 
 244                                     GetWinHwnd(GetParent()), 
 251         wxLogError(_("Can't create list control window, check that comctl32.dll is installed.")); 
 256     // for comctl32.dll v 4.70+ we want to have this attribute because it's 
 257     // prettier (and also because wxGTK does it like this) 
 258     if ( (wstyle 
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 ) 
 260         ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 
 261                       0, LVS_EX_FULLROWSELECT
); 
 264     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
)); 
 265     SetForegroundColour(GetParent()->GetForegroundColour()); 
 272 void wxListCtrl::UpdateStyle() 
 276         // The new window view style 
 278         DWORD dwStyleNew 
= ConvertToMSWStyle(dummy
, m_windowStyle
); 
 279         dwStyleNew 
|= m_baseStyle
; 
 281         // Get the current window style. 
 282         DWORD dwStyleOld 
= ::GetWindowLong(GetHwnd(), GWL_STYLE
); 
 284         // Only set the window style if the view bits have changed. 
 285         if ( dwStyleOld 
!= dwStyleNew 
) 
 287             ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
); 
 292 void wxListCtrl::FreeAllAttrs(bool dontRecreate
) 
 296         for ( wxNode 
*node 
= m_attrs
.Next(); node
; node 
= m_attrs
.Next() ) 
 298             delete (wxListItemAttr 
*)node
->Data(); 
 304             m_attrs
.Create(wxKEY_INTEGER
, 1000);        // just as def ctor 
 307         m_hasAnyAttr 
= FALSE
; 
 311 wxListCtrl::~wxListCtrl() 
 313     FreeAllAttrs(TRUE 
/* no need to recreate hash any more */); 
 317         m_textCtrl
->SetHWND(0); 
 318         m_textCtrl
->UnsubclassWin(); 
 323     if (m_ownsImageListNormal
) delete m_imageListNormal
; 
 324     if (m_ownsImageListSmall
) delete m_imageListSmall
; 
 325     if (m_ownsImageListState
) delete m_imageListState
; 
 328 // ---------------------------------------------------------------------------- 
 329 // set/get/change style 
 330 // ---------------------------------------------------------------------------- 
 332 // Add or remove a single window style 
 333 void wxListCtrl::SetSingleStyle(long style
, bool add
) 
 335     long flag 
= GetWindowStyleFlag(); 
 337     // Get rid of conflicting styles 
 340         if ( style 
& wxLC_MASK_TYPE
) 
 341             flag 
= flag 
& ~wxLC_MASK_TYPE
; 
 342         if ( style 
& wxLC_MASK_ALIGN 
) 
 343             flag 
= flag 
& ~wxLC_MASK_ALIGN
; 
 344         if ( style 
& wxLC_MASK_SORT 
) 
 345             flag 
= flag 
& ~wxLC_MASK_SORT
; 
 361     m_windowStyle 
= flag
; 
 366 // Set the whole window style 
 367 void wxListCtrl::SetWindowStyleFlag(long flag
) 
 369     m_windowStyle 
= flag
; 
 374 // Can be just a single style, or a bitlist 
 375 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const 
 378     if ( style 
& wxLC_ICON 
) 
 380         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 381             oldStyle 
-= LVS_SMALLICON
; 
 382         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 383             oldStyle 
-= LVS_REPORT
; 
 384         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 385             oldStyle 
-= LVS_LIST
; 
 389     if ( style 
& wxLC_SMALL_ICON 
) 
 391         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 392             oldStyle 
-= LVS_ICON
; 
 393         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 394             oldStyle 
-= LVS_REPORT
; 
 395         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 396             oldStyle 
-= LVS_LIST
; 
 397         wstyle 
|= LVS_SMALLICON
; 
 400     if ( style 
& wxLC_LIST 
) 
 402         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 403             oldStyle 
-= LVS_ICON
; 
 404         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 405             oldStyle 
-= LVS_REPORT
; 
 406         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 407             oldStyle 
-= LVS_SMALLICON
; 
 411     if ( style 
& wxLC_REPORT 
) 
 413         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 414             oldStyle 
-= LVS_ICON
; 
 415         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 416             oldStyle 
-= LVS_LIST
; 
 417         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 418             oldStyle 
-= LVS_SMALLICON
; 
 420         wstyle 
|= LVS_REPORT
; 
 423     if ( style 
& wxLC_ALIGN_LEFT 
) 
 425         if ( oldStyle 
& LVS_ALIGNTOP 
) 
 426             oldStyle 
-= LVS_ALIGNTOP
; 
 427         wstyle 
|= LVS_ALIGNLEFT
; 
 430     if ( style 
& wxLC_ALIGN_TOP 
) 
 432         if ( oldStyle 
& LVS_ALIGNLEFT 
) 
 433             oldStyle 
-= LVS_ALIGNLEFT
; 
 434         wstyle 
|= LVS_ALIGNTOP
; 
 437     if ( style 
& wxLC_AUTOARRANGE 
) 
 438         wstyle 
|= LVS_AUTOARRANGE
; 
 440     if ( style 
& wxLC_NO_SORT_HEADER 
) 
 441         wstyle 
|= LVS_NOSORTHEADER
; 
 443     if ( style 
& wxLC_NO_HEADER 
) 
 444         wstyle 
|= LVS_NOCOLUMNHEADER
; 
 446     if ( style 
& wxLC_EDIT_LABELS 
) 
 447         wstyle 
|= LVS_EDITLABELS
; 
 449     if ( style 
& wxLC_SINGLE_SEL 
) 
 450         wstyle 
|= LVS_SINGLESEL
; 
 452     if ( style 
& wxLC_SORT_ASCENDING 
) 
 454         if ( oldStyle 
& LVS_SORTDESCENDING 
) 
 455             oldStyle 
-= LVS_SORTDESCENDING
; 
 456         wstyle 
|= LVS_SORTASCENDING
; 
 459     if ( style 
& wxLC_SORT_DESCENDING 
) 
 461         if ( oldStyle 
& LVS_SORTASCENDING 
) 
 462             oldStyle 
-= LVS_SORTASCENDING
; 
 463         wstyle 
|= LVS_SORTDESCENDING
; 
 466     if ( style 
& wxLC_VIRTUAL 
) 
 468         int ver 
= wxTheApp
->GetComCtl32Version(); 
 471             wxLogWarning(_("Please install a newer version of comctl32.dll\n" 
 472                            "(at least version 4.70 is required but you have " 
 474                            "or this program won't operate correctly."), 
 475                         ver 
/ 100, ver 
% 100); 
 478         wstyle 
|= LVS_OWNERDATA
; 
 484 // ---------------------------------------------------------------------------- 
 486 // ---------------------------------------------------------------------------- 
 488 // Sets the foreground, i.e. text, colour 
 489 bool wxListCtrl::SetForegroundColour(const wxColour
& col
) 
 491     if ( !wxWindow::SetForegroundColour(col
) ) 
 494     ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
)); 
 499 // Sets the background colour 
 500 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
) 
 502     if ( !wxWindow::SetBackgroundColour(col
) ) 
 505     // we set the same colour for both the "empty" background and the items 
 507     COLORREF color 
= wxColourToRGB(col
); 
 508     ListView_SetBkColor(GetHwnd(), color
); 
 509     ListView_SetTextBkColor(GetHwnd(), color
); 
 514 // Gets information about this column 
 515 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const 
 520     lvCol
.pszText 
= NULL
; 
 522     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 524         lvCol
.mask 
|= LVCF_TEXT
; 
 525         lvCol
.pszText 
= new wxChar
[513]; 
 526         lvCol
.cchTextMax 
= 512; 
 529     bool success 
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0); 
 531     //  item.m_subItem = lvCol.iSubItem; 
 532     item
.m_width 
= lvCol
.cx
; 
 534     if ( (item
.m_mask 
& wxLIST_MASK_TEXT
) && lvCol
.pszText 
) 
 536         item
.m_text 
= lvCol
.pszText
; 
 537         delete[] lvCol
.pszText
; 
 540     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 542         if (lvCol
.fmt 
== LVCFMT_LEFT
) 
 543             item
.m_format 
= wxLIST_FORMAT_LEFT
; 
 544         else if (lvCol
.fmt 
== LVCFMT_RIGHT
) 
 545             item
.m_format 
= wxLIST_FORMAT_RIGHT
; 
 546         else if (lvCol
.fmt 
== LVCFMT_CENTER
) 
 547             item
.m_format 
= wxLIST_FORMAT_CENTRE
; 
 553 // Sets information about this column 
 554 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
) 
 559     lvCol
.pszText 
= NULL
; 
 561     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 563         lvCol
.mask 
|= LVCF_TEXT
; 
 564         lvCol
.pszText 
= WXSTRINGCAST item
.m_text
; 
 565         lvCol
.cchTextMax 
= 0; // Ignored 
 567     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 569         lvCol
.mask 
|= LVCF_FMT
; 
 571         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
 572             lvCol
.fmt 
= LVCFMT_LEFT
; 
 573         if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
 574             lvCol
.fmt 
= LVCFMT_RIGHT
; 
 575         if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
 576             lvCol
.fmt 
= LVCFMT_CENTER
; 
 579     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
 581         lvCol
.mask 
|= LVCF_WIDTH
; 
 582         lvCol
.cx 
= item
.m_width
; 
 584         if ( lvCol
.cx 
== wxLIST_AUTOSIZE
) 
 585             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
 586         else if ( lvCol
.cx 
== wxLIST_AUTOSIZE_USEHEADER
) 
 587             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
 589     lvCol
.mask 
|= LVCF_SUBITEM
; 
 590     lvCol
.iSubItem 
= col
; 
 591     return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0); 
 594 // Gets the column width 
 595 int wxListCtrl::GetColumnWidth(int col
) const 
 597     return ListView_GetColumnWidth(GetHwnd(), col
); 
 600 // Sets the column width 
 601 bool wxListCtrl::SetColumnWidth(int col
, int width
) 
 604     if ( m_windowStyle 
& wxLC_LIST 
) 
 608     if ( width2 
== wxLIST_AUTOSIZE
) 
 609         width2 
= LVSCW_AUTOSIZE
; 
 610     else if ( width2 
== wxLIST_AUTOSIZE_USEHEADER
) 
 611         width2 
= LVSCW_AUTOSIZE_USEHEADER
; 
 613     return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0); 
 616 // Gets the number of items that can fit vertically in the 
 617 // visible area of the list control (list or report view) 
 618 // or the total number of items in the list control (icon 
 619 // or small icon view) 
 620 int wxListCtrl::GetCountPerPage() const 
 622     return ListView_GetCountPerPage(GetHwnd()); 
 625 // Gets the edit control for editing labels. 
 626 wxTextCtrl
* wxListCtrl::GetEditControl() const 
 631 // Gets information about the item 
 632 bool wxListCtrl::GetItem(wxListItem
& info
) const 
 635     wxZeroMemory(lvItem
); 
 637     lvItem
.iItem 
= info
.m_itemId
; 
 638     lvItem
.iSubItem 
= info
.m_col
; 
 640     if ( info
.m_mask 
& wxLIST_MASK_TEXT 
) 
 642         lvItem
.mask 
|= LVIF_TEXT
; 
 643         lvItem
.pszText 
= new wxChar
[513]; 
 644         lvItem
.cchTextMax 
= 512; 
 648         lvItem
.pszText 
= NULL
; 
 651     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
 652         lvItem
.mask 
|= LVIF_PARAM
; 
 654     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
 655         lvItem
.mask 
|= LVIF_IMAGE
; 
 657     if ( info
.m_mask 
& wxLIST_MASK_STATE 
) 
 659         lvItem
.mask 
|= LVIF_STATE
; 
 660         // the other bits are hardly interesting anyhow 
 661         lvItem
.stateMask 
= LVIS_SELECTED 
| LVIS_FOCUSED
; 
 664     bool success 
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0; 
 667         wxLogError(_("Couldn't retrieve information about list control item %d."), 
 672         // give NULL as hwnd as we already have everything we need 
 673         wxConvertFromMSWListItem(NULL
, info
, lvItem
); 
 677         delete[] lvItem
.pszText
; 
 682 // Sets information about the item 
 683 bool wxListCtrl::SetItem(wxListItem
& info
) 
 686     wxConvertToMSWListItem(this, info
, item
); 
 689     if ( !ListView_SetItem(GetHwnd(), &item
) ) 
 691         wxLogDebug(_T("ListView_SetItem() failed")); 
 696     // we need to update the item immediately to show the new image 
 697     bool updateNow 
= (info
.m_mask 
& wxLIST_MASK_IMAGE
) != 0; 
 699     // check whether it has any custom attributes 
 700     if ( info
.HasAttributes() ) 
 702         wxListItemAttr 
*attr 
= (wxListItemAttr 
*)m_attrs
.Get(item
.iItem
); 
 705             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
 707             *attr 
= *info
.GetAttributes(); 
 711         // if the colour has changed, we must redraw the item 
 717         // we need this to make the change visible right now 
 718         ListView_Update(GetHwnd(), item
.iItem
); 
 724 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
) 
 728     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 729     info
.m_itemId 
= index
; 
 733         info
.m_image 
= imageId
; 
 734         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
 736     return SetItem(info
); 
 740 // Gets the item state 
 741 int wxListCtrl::GetItemState(long item
, long stateMask
) const 
 745     info
.m_mask 
= wxLIST_MASK_STATE
; 
 746     info
.m_stateMask 
= stateMask
; 
 747     info
.m_itemId 
= item
; 
 755 // Sets the item state 
 756 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
) 
 758     // NB: don't use SetItem() here as it doesn't work with the virtual list 
 761     wxZeroMemory(lvItem
); 
 763     wxConvertToMSWFlags(state
, stateMask
, lvItem
); 
 765     if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
, 
 766                         (WPARAM
)item
, (LPARAM
)&lvItem
) ) 
 768         wxLogLastError(_T("ListView_SetItemState")); 
 776 // Sets the item image 
 777 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
)) 
 781     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
 782     info
.m_image 
= image
; 
 783     info
.m_itemId 
= item
; 
 785     return SetItem(info
); 
 788 // Gets the item text 
 789 wxString 
wxListCtrl::GetItemText(long item
) const 
 793     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 794     info
.m_itemId 
= item
; 
 801 // Sets the item text 
 802 void wxListCtrl::SetItemText(long item
, const wxString
& str
) 
 806     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 807     info
.m_itemId 
= item
; 
 813 // Gets the item data 
 814 long wxListCtrl::GetItemData(long item
) const 
 818     info
.m_mask 
= wxLIST_MASK_DATA
; 
 819     info
.m_itemId 
= item
; 
 826 // Sets the item data 
 827 bool wxListCtrl::SetItemData(long item
, long data
) 
 831     info
.m_mask 
= wxLIST_MASK_DATA
; 
 832     info
.m_itemId 
= item
; 
 835     return SetItem(info
); 
 838 // Gets the item rectangle 
 839 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const 
 843     int code2 
= LVIR_BOUNDS
; 
 844     if ( code 
== wxLIST_RECT_BOUNDS 
) 
 846     else if ( code 
== wxLIST_RECT_ICON 
) 
 848     else if ( code 
== wxLIST_RECT_LABEL 
) 
 852     bool success 
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2 
) != 0); 
 854     bool success 
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0); 
 859     rect
.width 
= rect2
.right 
- rect2
.left
; 
 860     rect
.height 
= rect2
.bottom 
- rect2
.top
; 
 864 // Gets the item position 
 865 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const 
 869     bool success 
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0); 
 871     pos
.x 
= pt
.x
; pos
.y 
= pt
.y
; 
 875 // Sets the item position. 
 876 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
) 
 878     return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0); 
 881 // Gets the number of items in the list control 
 882 int wxListCtrl::GetItemCount() const 
 884     return ListView_GetItemCount(GetHwnd()); 
 887 // Retrieves the spacing between icons in pixels. 
 888 // If small is TRUE, gets the spacing for the small icon 
 889 // view, otherwise the large icon view. 
 890 int wxListCtrl::GetItemSpacing(bool isSmall
) const 
 892     return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
); 
 895 // Gets the number of selected items in the list control 
 896 int wxListCtrl::GetSelectedItemCount() const 
 898     return ListView_GetSelectedCount(GetHwnd()); 
 901 // Gets the text colour of the listview 
 902 wxColour 
wxListCtrl::GetTextColour() const 
 904     COLORREF ref 
= ListView_GetTextColor(GetHwnd()); 
 905     wxColour 
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
)); 
 909 // Sets the text colour of the listview 
 910 void wxListCtrl::SetTextColour(const wxColour
& col
) 
 912     ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue())); 
 915 // Gets the index of the topmost visible item when in 
 916 // list or report view 
 917 long wxListCtrl::GetTopItem() const 
 919     return (long) ListView_GetTopIndex(GetHwnd()); 
 922 // Searches for an item, starting from 'item'. 
 923 // 'geometry' is one of 
 924 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. 
 925 // 'state' is a state bit flag, one or more of 
 926 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. 
 927 // item can be -1 to find the first item that matches the 
 929 // Returns the item or -1 if unsuccessful. 
 930 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const 
 934     if ( geom 
== wxLIST_NEXT_ABOVE 
) 
 936     if ( geom 
== wxLIST_NEXT_ALL 
) 
 938     if ( geom 
== wxLIST_NEXT_BELOW 
) 
 940     if ( geom 
== wxLIST_NEXT_LEFT 
) 
 941         flags 
|= LVNI_TOLEFT
; 
 942     if ( geom 
== wxLIST_NEXT_RIGHT 
) 
 943         flags 
|= LVNI_TORIGHT
; 
 945     if ( state 
& wxLIST_STATE_CUT 
) 
 947     if ( state 
& wxLIST_STATE_DROPHILITED 
) 
 948         flags 
|= LVNI_DROPHILITED
; 
 949     if ( state 
& wxLIST_STATE_FOCUSED 
) 
 950         flags 
|= LVNI_FOCUSED
; 
 951     if ( state 
& wxLIST_STATE_SELECTED 
) 
 952         flags 
|= LVNI_SELECTED
; 
 954     return (long) ListView_GetNextItem(GetHwnd(), item
, flags
); 
 958 wxImageList 
*wxListCtrl::GetImageList(int which
) const 
 960     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
 962         return m_imageListNormal
; 
 964     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
 966         return m_imageListSmall
; 
 968     else if ( which 
== wxIMAGE_LIST_STATE 
) 
 970         return m_imageListState
; 
 975 void wxListCtrl::SetImageList(wxImageList 
*imageList
, int which
) 
 978     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
 980         flags 
= LVSIL_NORMAL
; 
 981         if (m_ownsImageListNormal
) delete m_imageListNormal
; 
 982         m_imageListNormal 
= imageList
; 
 983         m_ownsImageListNormal 
= FALSE
; 
 985     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
 988         if (m_ownsImageListSmall
) delete m_imageListSmall
; 
 989         m_imageListSmall 
= imageList
; 
 990         m_ownsImageListSmall 
= FALSE
; 
 992     else if ( which 
== wxIMAGE_LIST_STATE 
) 
 995         if (m_ownsImageListState
) delete m_imageListState
; 
 996         m_imageListState 
= imageList
; 
 997         m_ownsImageListState 
= FALSE
; 
 999     ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList 
? imageList
->GetHIMAGELIST() : 0, flags
); 
1002 void wxListCtrl::AssignImageList(wxImageList 
*imageList
, int which
) 
1004     SetImageList(imageList
, which
); 
1005     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1006         m_ownsImageListNormal 
= TRUE
; 
1007     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1008         m_ownsImageListSmall 
= TRUE
; 
1009     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1010         m_ownsImageListState 
= TRUE
; 
1013 // ---------------------------------------------------------------------------- 
1015 // ---------------------------------------------------------------------------- 
1017 // Arranges the items 
1018 bool wxListCtrl::Arrange(int flag
) 
1021     if ( flag 
== wxLIST_ALIGN_LEFT 
) 
1022         code 
= LVA_ALIGNLEFT
; 
1023     else if ( flag 
== wxLIST_ALIGN_TOP 
) 
1024         code 
= LVA_ALIGNTOP
; 
1025     else if ( flag 
== wxLIST_ALIGN_DEFAULT 
) 
1027     else if ( flag 
== wxLIST_ALIGN_SNAP_TO_GRID 
) 
1028         code 
= LVA_SNAPTOGRID
; 
1030     return (ListView_Arrange(GetHwnd(), code
) != 0); 
1034 bool wxListCtrl::DeleteItem(long item
) 
1036     return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0); 
1039 // Deletes all items 
1040 bool wxListCtrl::DeleteAllItems() 
1042     return (ListView_DeleteAllItems(GetHwnd()) != 0); 
1045 // Deletes all items 
1046 bool wxListCtrl::DeleteAllColumns() 
1048     while ( m_colCount 
> 0 ) 
1050         if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) 
1052             wxLogLastError(wxT("ListView_DeleteColumn")); 
1060     wxASSERT_MSG( m_colCount 
== 0, wxT("no columns should be left") ); 
1066 bool wxListCtrl::DeleteColumn(int col
) 
1068     bool success 
= (ListView_DeleteColumn(GetHwnd(), col
) != 0); 
1070     if ( success 
&& (m_colCount 
> 0) ) 
1075 // Clears items, and columns if there are any. 
1076 void wxListCtrl::ClearAll() 
1079     if ( m_colCount 
> 0 ) 
1083 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
) 
1085     wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) ); 
1087     // VS: ListView_EditLabel requires that the list has focus.   
1089     HWND hWnd 
= (HWND
) ListView_EditLabel(GetHwnd(), item
); 
1093         m_textCtrl
->SetHWND(0); 
1094         m_textCtrl
->UnsubclassWin(); 
1099     m_textCtrl 
= (wxTextCtrl
*) textControlClass
->CreateObject(); 
1100     m_textCtrl
->SetHWND((WXHWND
) hWnd
); 
1101     m_textCtrl
->SubclassWin((WXHWND
) hWnd
); 
1106 // End label editing, optionally cancelling the edit 
1107 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
)) 
1109     wxFAIL_MSG( _T("not implemented") ); 
1114 // Ensures this item is visible 
1115 bool wxListCtrl::EnsureVisible(long item
) 
1117     return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0; 
1120 // Find an item whose label matches this string, starting from the item after 'start' 
1121 // or the beginning if 'start' is -1. 
1122 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
) 
1124     LV_FINDINFO findInfo
; 
1126     findInfo
.flags 
= LVFI_STRING
; 
1128         findInfo
.flags 
|= LVFI_PARTIAL
; 
1131     // ListView_FindItem() excludes the first item from search and to look 
1132     // through all the items you need to start from -1 which is unnatural and 
1133     // inconsistent with the generic version - so we adjust the index 
1136     return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
); 
1139 // Find an item whose data matches this data, starting from the item after 'start' 
1140 // or the beginning if 'start' is -1. 
1141 long wxListCtrl::FindItem(long start
, long data
) 
1143     LV_FINDINFO findInfo
; 
1145     findInfo
.flags 
= LVFI_PARAM
; 
1146     findInfo
.lParam 
= data
; 
1148     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1151 // Find an item nearest this position in the specified direction, starting from 
1152 // the item after 'start' or the beginning if 'start' is -1. 
1153 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
) 
1155     LV_FINDINFO findInfo
; 
1157     findInfo
.flags 
= LVFI_NEARESTXY
; 
1158     findInfo
.pt
.x 
= pt
.x
; 
1159     findInfo
.pt
.y 
= pt
.y
; 
1160     findInfo
.vkDirection 
= VK_RIGHT
; 
1162     if ( direction 
== wxLIST_FIND_UP 
) 
1163         findInfo
.vkDirection 
= VK_UP
; 
1164     else if ( direction 
== wxLIST_FIND_DOWN 
) 
1165         findInfo
.vkDirection 
= VK_DOWN
; 
1166     else if ( direction 
== wxLIST_FIND_LEFT 
) 
1167         findInfo
.vkDirection 
= VK_LEFT
; 
1168     else if ( direction 
== wxLIST_FIND_RIGHT 
) 
1169         findInfo
.vkDirection 
= VK_RIGHT
; 
1171     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1174 // Determines which item (if any) is at the specified point, 
1175 // giving details in 'flags' (see wxLIST_HITTEST_... flags above) 
1176 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
) 
1178     LV_HITTESTINFO hitTestInfo
; 
1179     hitTestInfo
.pt
.x 
= (int) point
.x
; 
1180     hitTestInfo
.pt
.y 
= (int) point
.y
; 
1182     ListView_HitTest(GetHwnd(), & hitTestInfo
); 
1185     if ( hitTestInfo
.flags 
& LVHT_ABOVE 
) 
1186         flags 
|= wxLIST_HITTEST_ABOVE
; 
1187     if ( hitTestInfo
.flags 
& LVHT_BELOW 
) 
1188         flags 
|= wxLIST_HITTEST_BELOW
; 
1189     if ( hitTestInfo
.flags 
& LVHT_NOWHERE 
) 
1190         flags 
|= wxLIST_HITTEST_NOWHERE
; 
1191     if ( hitTestInfo
.flags 
& LVHT_ONITEMICON 
) 
1192         flags 
|= wxLIST_HITTEST_ONITEMICON
; 
1193     if ( hitTestInfo
.flags 
& LVHT_ONITEMLABEL 
) 
1194         flags 
|= wxLIST_HITTEST_ONITEMLABEL
; 
1195     if ( hitTestInfo
.flags 
& LVHT_ONITEMSTATEICON 
) 
1196         flags 
|= wxLIST_HITTEST_ONITEMSTATEICON
; 
1197     if ( hitTestInfo
.flags 
& LVHT_TOLEFT 
) 
1198         flags 
|= wxLIST_HITTEST_TOLEFT
; 
1199     if ( hitTestInfo
.flags 
& LVHT_TORIGHT 
) 
1200         flags 
|= wxLIST_HITTEST_TORIGHT
; 
1202     return (long) hitTestInfo
.iItem
; 
1205 // Inserts an item, returning the index of the new item if successful, 
1207 long wxListCtrl::InsertItem(wxListItem
& info
) 
1209     wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); 
1212     wxConvertToMSWListItem(this, info
, item
); 
1214     // check whether it has any custom attributes 
1215     if ( info
.HasAttributes() ) 
1218         wxListItemAttr 
*attr
; 
1219         attr 
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
); 
1223             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
1225         else *attr 
= *info
.GetAttributes(); 
1227         m_hasAnyAttr 
= TRUE
; 
1230     return (long) ListView_InsertItem(GetHwnd(), & item
); 
1233 long wxListCtrl::InsertItem(long index
, const wxString
& label
) 
1236     info
.m_text 
= label
; 
1237     info
.m_mask 
= wxLIST_MASK_TEXT
; 
1238     info
.m_itemId 
= index
; 
1239     return InsertItem(info
); 
1242 // Inserts an image item 
1243 long wxListCtrl::InsertItem(long index
, int imageIndex
) 
1246     info
.m_image 
= imageIndex
; 
1247     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
1248     info
.m_itemId 
= index
; 
1249     return InsertItem(info
); 
1252 // Inserts an image/string item 
1253 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
) 
1256     info
.m_image 
= imageIndex
; 
1257     info
.m_text 
= label
; 
1258     info
.m_mask 
= wxLIST_MASK_IMAGE 
| wxLIST_MASK_TEXT
; 
1259     info
.m_itemId 
= index
; 
1260     return InsertItem(info
); 
1263 // For list view mode (only), inserts a column. 
1264 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
) 
1269     lvCol
.pszText 
= NULL
; 
1271     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
1273         lvCol
.mask 
|= LVCF_TEXT
; 
1274         lvCol
.pszText 
= WXSTRINGCAST item
.m_text
; 
1275         lvCol
.cchTextMax 
= 0; // Ignored 
1277     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
1279         lvCol
.mask 
|= LVCF_FMT
; 
1281         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
1282             lvCol
.fmt 
= LVCFMT_LEFT
; 
1283         if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
1284             lvCol
.fmt 
= LVCFMT_RIGHT
; 
1285         if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
1286             lvCol
.fmt 
= LVCFMT_CENTER
; 
1289     lvCol
.mask 
|= LVCF_WIDTH
; 
1290     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
1292         if ( item
.m_width 
== wxLIST_AUTOSIZE
) 
1293             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
1294         else if ( item
.m_width 
== wxLIST_AUTOSIZE_USEHEADER
) 
1295             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
1297             lvCol
.cx 
= item
.m_width
; 
1301         // always give some width to the new column: this one is compatible 
1306     lvCol
.mask 
|= LVCF_SUBITEM
; 
1307     lvCol
.iSubItem 
= col
; 
1309     bool success 
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1; 
1316         wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), 
1323 long wxListCtrl::InsertColumn(long col
, 
1324                               const wxString
& heading
, 
1329     item
.m_mask 
= wxLIST_MASK_TEXT 
| wxLIST_MASK_FORMAT
; 
1330     item
.m_text 
= heading
; 
1333         item
.m_mask 
|= wxLIST_MASK_WIDTH
; 
1334         item
.m_width 
= width
; 
1336     item
.m_format 
= format
; 
1338     return InsertColumn(col
, item
); 
1341 // Scrolls the list control. If in icon, small icon or report view mode, 
1342 // x specifies the number of pixels to scroll. If in list view mode, x 
1343 // specifies the number of columns to scroll. 
1344 // If in icon, small icon or list view mode, y specifies the number of pixels 
1345 // to scroll. If in report view mode, y specifies the number of lines to scroll. 
1346 bool wxListCtrl::ScrollList(int dx
, int dy
) 
1348     return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0); 
1353 // fn is a function which takes 3 long arguments: item1, item2, data. 
1354 // item1 is the long data associated with a first item (NOT the index). 
1355 // item2 is the long data associated with a second item (NOT the index). 
1356 // data is the same value as passed to SortItems. 
1357 // The return value is a negative number if the first item should precede the second 
1358 // item, a positive number of the second item should precede the first, 
1359 // or zero if the two items are equivalent. 
1361 // data is arbitrary data to be passed to the sort function. 
1362 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
) 
1364     return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0); 
1367 // ---------------------------------------------------------------------------- 
1368 // message processing 
1369 // ---------------------------------------------------------------------------- 
1371 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
) 
1373     if (cmd 
== EN_UPDATE
) 
1375         wxCommandEvent 
event(wxEVT_COMMAND_TEXT_UPDATED
, id
); 
1376         event
.SetEventObject( this ); 
1377         ProcessCommand(event
); 
1380     else if (cmd 
== EN_KILLFOCUS
) 
1382         wxCommandEvent 
event(wxEVT_KILL_FOCUS
, id
); 
1383         event
.SetEventObject( this ); 
1384         ProcessCommand(event
); 
1391 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM 
*result
) 
1393     // prepare the event 
1394     // ----------------- 
1396     wxListEvent 
event(wxEVT_NULL
, m_windowId
); 
1397     wxEventType eventType 
= wxEVT_NULL
; 
1399     NMHDR 
*nmhdr 
= (NMHDR 
*)lParam
; 
1401     // almost all messages use NM_LISTVIEW 
1402     NM_LISTVIEW 
*nmLV 
= (NM_LISTVIEW 
*)nmhdr
; 
1404     // this is true for almost all events 
1405     event
.m_item
.m_data 
= nmLV
->lParam
; 
1407     switch ( nmhdr
->code 
) 
1409         case LVN_BEGINRDRAG
: 
1410             eventType 
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
; 
1414             if ( eventType 
== wxEVT_NULL 
) 
1416                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_DRAG
; 
1419             event
.m_itemIndex 
= nmLV
->iItem
; 
1420             event
.m_pointDrag
.x 
= nmLV
->ptAction
.x
; 
1421             event
.m_pointDrag
.y 
= nmLV
->ptAction
.y
; 
1424         case LVN_BEGINLABELEDIT
: 
1426                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
; 
1427                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1428                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1429                 event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1433         case LVN_COLUMNCLICK
: 
1434             eventType 
= wxEVT_COMMAND_LIST_COL_CLICK
; 
1435             event
.m_itemIndex 
= -1; 
1436             event
.m_col 
= nmLV
->iSubItem
; 
1439         case LVN_DELETEALLITEMS
: 
1440             eventType 
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
; 
1441             event
.m_itemIndex 
= -1; 
1447         case LVN_DELETEITEM
: 
1448             eventType 
= wxEVT_COMMAND_LIST_DELETE_ITEM
; 
1449             event
.m_itemIndex 
= nmLV
->iItem
; 
1453                 delete (wxListItemAttr 
*)m_attrs
.Delete(nmLV
->iItem
); 
1457         case LVN_ENDLABELEDIT
: 
1459                 eventType 
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
; 
1460                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1461                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1462                 if ( info
->item
.pszText 
== NULL 
|| info
->item
.iItem 
== -1 ) 
1465                 event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1469         case LVN_SETDISPINFO
: 
1471                 eventType 
= wxEVT_COMMAND_LIST_SET_INFO
; 
1472                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1473                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1477         case LVN_INSERTITEM
: 
1478             eventType 
= wxEVT_COMMAND_LIST_INSERT_ITEM
; 
1479             event
.m_itemIndex 
= nmLV
->iItem
; 
1482         case LVN_ITEMCHANGED
: 
1483             // This needs to be sent to wxListCtrl as a rather more concrete 
1484             // event. For now, just detect a selection or deselection. 
1485             if ( (nmLV
->uNewState 
& LVIS_SELECTED
) && !(nmLV
->uOldState 
& LVIS_SELECTED
) ) 
1487                 eventType 
= wxEVT_COMMAND_LIST_ITEM_SELECTED
; 
1488                 event
.m_itemIndex 
= nmLV
->iItem
; 
1490             else if ( !(nmLV
->uNewState 
& LVIS_SELECTED
) && (nmLV
->uOldState 
& LVIS_SELECTED
) ) 
1492                 eventType 
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
; 
1493                 event
.m_itemIndex 
= nmLV
->iItem
; 
1503                 LV_KEYDOWN 
*info 
= (LV_KEYDOWN 
*)lParam
; 
1504                 WORD wVKey 
= info
->wVKey
; 
1506                 // get the current selection 
1507                 long lItem 
= GetNextItem(-1, 
1509                                          wxLIST_STATE_SELECTED
); 
1511                 // <Enter> or <Space> activate the selected item if any (but 
1512                 // not with Shift and/or Ctrl as then they have a predefined 
1513                 // meaning for the list view) 
1515                      (wVKey 
== VK_RETURN 
|| wVKey 
== VK_SPACE
) && 
1516                      !(wxIsShiftDown() || wxIsCtrlDown()) ) 
1518                     eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1522                     eventType 
= wxEVT_COMMAND_LIST_KEY_DOWN
; 
1523                     event
.m_code 
= wxCharCodeMSWToWX(wVKey
); 
1527                 event
.m_item
.m_itemId 
= lItem
; 
1531                     // fill the other fields too 
1532                     event
.m_item
.m_text 
= GetItemText(lItem
); 
1533                     event
.m_item
.m_data 
= GetItemData(lItem
); 
1539             // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do 
1541             if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1546             // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event 
1547             // if it happened on an item (and not on empty place) 
1548             if ( nmLV
->iItem 
== -1 ) 
1554             eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1555             event
.m_itemIndex 
= nmLV
->iItem
; 
1556             event
.m_item
.m_text 
= GetItemText(nmLV
->iItem
); 
1557             event
.m_item
.m_data 
= GetItemData(nmLV
->iItem
); 
1561             /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to 
1562                subclass and check for the actual WM_RBUTTONDOWN message, 
1563                because NM_RCLICK waits for the WM_RBUTTONUP message as well 
1564                before firing off. We want to have notify events for both down 
1567                 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), 
1568                 // don't do anything else 
1569                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1574                 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event 
1575                 LV_HITTESTINFO lvhti
; 
1576                 wxZeroMemory(lvhti
); 
1578                 ::GetCursorPos(&(lvhti
.pt
)); 
1579                 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
)); 
1580                 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 ) 
1582                     if ( lvhti
.flags 
& LVHT_ONITEM 
) 
1584                         eventType 
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
; 
1585                         event
.m_itemIndex 
= lvhti
.iItem
; 
1586                         event
.m_pointDrag
.x 
= lvhti
.pt
.x
; 
1587                         event
.m_pointDrag
.y 
= lvhti
.pt
.y
; 
1594         case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ****** 
1596                 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do 
1598                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1603                 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event 
1604                 eventType 
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
; 
1605                 NMITEMACTIVATE
* hdr 
= (NMITEMACTIVATE
*)lParam
; 
1606                 event
.m_itemIndex 
= hdr
->iItem
; 
1611 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 
1613             *result 
= OnCustomDraw(lParam
); 
1616 #endif // _WIN32_IE >= 0x300 
1618         case LVN_GETDISPINFO
: 
1621                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1623                 LV_ITEM
& lvi 
= info
->item
; 
1624                 long item 
= lvi
.iItem
; 
1626                 if ( lvi
.mask 
& LVIF_TEXT 
) 
1628                     wxString text 
= OnGetItemText(item
, lvi
.iSubItem
); 
1629                     wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
); 
1632                 if ( lvi
.mask 
& LVIF_IMAGE 
) 
1634                     lvi
.iImage 
= OnGetItemImage(item
); 
1637                 // a little dose of healthy paranoia: as we never use 
1638                 // LVM_SETCALLBACKMASK we're not supposed to get these ones 
1639                 wxASSERT_MSG( !(lvi
.mask 
& LVIF_STATE
), 
1640                               _T("we don't support state callbacks yet!") ); 
1647             return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
1650     // process the event 
1651     // ----------------- 
1653     event
.SetEventObject( this ); 
1654     event
.SetEventType(eventType
); 
1656     if ( !GetEventHandler()->ProcessEvent(event
) ) 
1662     switch ( nmhdr
->code 
) 
1664         case LVN_DELETEALLITEMS
: 
1665             // always return TRUE to suppress all additional LVN_DELETEITEM 
1666             // notifications - this makes deleting all items from a list ctrl 
1672         case LVN_ENDLABELEDIT
: 
1674                 *result 
= event
.IsAllowed(); 
1679     *result 
= !event
.IsAllowed(); 
1684 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 
1686 WXLPARAM 
wxListCtrl::OnCustomDraw(WXLPARAM lParam
) 
1688     LPNMLVCUSTOMDRAW lplvcd 
= (LPNMLVCUSTOMDRAW
)lParam
; 
1689     NMCUSTOMDRAW
& nmcd 
= lplvcd
->nmcd
; 
1690     switch ( nmcd
.dwDrawStage 
) 
1693             // if we've got any items with non standard attributes, 
1694             // notify us before painting each item 
1696             // for virtual controls, always suppose that we have attributes as 
1697             // there is no way to check for this 
1698             return IsVirtual() || m_hasAnyAttr 
? CDRF_NOTIFYITEMDRAW
 
1701         case CDDS_ITEMPREPAINT
: 
1703                 size_t item 
= (size_t)nmcd
.dwItemSpec
; 
1704                 if ( item 
>= (size_t)GetItemCount() ) 
1706                     // we get this message with item == 0 for an empty control, 
1707                     // we must ignore it as calling OnGetItemAttr() would be 
1709                     return CDRF_DODEFAULT
; 
1712                 wxListItemAttr 
*attr 
= 
1713                     IsVirtual() ? OnGetItemAttr(item
) 
1714                                 : (wxListItemAttr 
*)m_attrs
.Get(item
); 
1718                     // nothing to do for this item 
1719                     return CDRF_DODEFAULT
; 
1723                 wxColour colText
, colBack
; 
1724                 if ( attr
->HasFont() ) 
1726                     wxFont font 
= attr
->GetFont(); 
1727                     hFont 
= (HFONT
)font
.GetResourceHandle(); 
1734                 if ( attr
->HasTextColour() ) 
1736                     colText 
= attr
->GetTextColour(); 
1740                     colText 
= GetTextColour(); 
1743                 if ( attr
->HasBackgroundColour() ) 
1745                     colBack 
= attr
->GetBackgroundColour(); 
1749                     colBack 
= GetBackgroundColour(); 
1752                 lplvcd
->clrText 
= wxColourToRGB(colText
); 
1753                 lplvcd
->clrTextBk 
= wxColourToRGB(colBack
); 
1755                 // note that if we wanted to set colours for 
1756                 // individual columns (subitems), we would have 
1757                 // returned CDRF_NOTIFYSUBITEMREDRAW from here 
1760                     ::SelectObject(nmcd
.hdc
, hFont
); 
1762                     return CDRF_NEWFONT
; 
1765             // fall through to return CDRF_DODEFAULT 
1768             return CDRF_DODEFAULT
; 
1772 #endif // NM_CUSTOMDRAW supported 
1774 // Necessary for drawing hrules and vrules, if specified 
1775 void wxListCtrl::OnPaint(wxPaintEvent
& event
) 
1779     wxControl::OnPaint(event
); 
1781     // Reset the device origin since it may have been set 
1782     dc
.SetDeviceOrigin(0, 0); 
1784     bool drawHRules 
= ((GetWindowStyle() & wxLC_HRULES
) != 0); 
1785     bool drawVRules 
= ((GetWindowStyle() & wxLC_VRULES
) != 0); 
1787     if (!drawHRules 
&& !drawVRules
) 
1789     if ((GetWindowStyle() & wxLC_REPORT
) == 0) 
1792     wxPen 
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
); 
1794     dc
.SetBrush(* wxTRANSPARENT_BRUSH
); 
1796     wxSize clientSize 
= GetClientSize(); 
1800     int itemCount 
= GetItemCount(); 
1802     for (i 
= 0; i 
< itemCount
; i
++) 
1804         if (GetItemRect(i
, itemRect
)) 
1806             cy 
= itemRect
.GetTop(); 
1807             if (i 
!= 0) // Don't draw the first one 
1809                 dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
1812             if (i 
== (GetItemCount() - 1)) 
1814                 cy 
= itemRect
.GetBottom(); 
1815                 dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
1819     i 
= (GetItemCount() - 1); 
1820     if (drawVRules 
&& (i 
> -1)) 
1822         wxRect firstItemRect
; 
1823         GetItemRect(0, firstItemRect
); 
1825         if (GetItemRect(i
, itemRect
)) 
1828             int x 
= itemRect
.GetX(); 
1829             for (col 
= 0; col 
< GetColumnCount(); col
++) 
1831                 int colWidth 
= GetColumnWidth(col
); 
1833                 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom()); 
1839 // ---------------------------------------------------------------------------- 
1840 // virtual list controls 
1841 // ---------------------------------------------------------------------------- 
1843 wxString 
wxListCtrl::OnGetItemText(long item
, long col
) const 
1845     // this is a pure virtual function, in fact - which is not really pure 
1846     // because the controls which are not virtual don't need to implement it 
1847     wxFAIL_MSG( _T("not supposed to be called") ); 
1849     return wxEmptyString
; 
1852 int wxListCtrl::OnGetItemImage(long item
) const 
1855     wxFAIL_MSG( _T("not supposed to be called") ); 
1860 wxListItemAttr 
*wxListCtrl::OnGetItemAttr(long item
) const 
1862     wxASSERT_MSG( item 
>= 0 && item 
< GetItemCount(), 
1863                   _T("invalid item index in OnGetItemAttr()") ); 
1865     // no attributes by default 
1869 void wxListCtrl::SetItemCount(long count
) 
1871     wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); 
1873     if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) ) 
1875         wxLogLastError(_T("ListView_SetItemCount")); 
1879 void wxListCtrl::RefreshItem(long item
) 
1881     if ( !ListView_Update(GetHwnd(), item
) ) 
1883         wxLogLastError(_T("ListView_Update")); 
1887 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
) 
1889     for ( long item 
= itemFrom
; item 
<= itemTo
; item
++ ) 
1895 // ---------------------------------------------------------------------------- 
1897 // ---------------------------------------------------------------------------- 
1899 // List item structure 
1900 wxListItem::wxListItem() 
1910     m_format 
= wxLIST_FORMAT_CENTRE
; 
1916 void wxListItem::Clear() 
1925     m_format 
= wxLIST_FORMAT_CENTRE
; 
1927     m_text 
= wxEmptyString
; 
1929     if (m_attr
) delete m_attr
; 
1933 void wxListItem::ClearAttributes() 
1935     if (m_attr
) delete m_attr
; 
1939 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
1943     info
.m_data 
= lvItem
.lParam
; 
1946     info
.m_stateMask 
= 0; 
1947     info
.m_itemId 
= lvItem
.iItem
; 
1949     long oldMask 
= lvItem
.mask
; 
1951     bool needText 
= FALSE
; 
1952     if (hwndListCtrl 
!= 0) 
1954         if ( lvItem
.mask 
& LVIF_TEXT 
) 
1961             lvItem
.pszText 
= new wxChar
[513]; 
1962             lvItem
.cchTextMax 
= 512; 
1964         lvItem
.mask 
|= LVIF_TEXT 
| LVIF_IMAGE 
| LVIF_PARAM
; 
1965         ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
); 
1968     if ( lvItem
.mask 
& LVIF_STATE 
) 
1970         info
.m_mask 
|= wxLIST_MASK_STATE
; 
1972         if ( lvItem
.stateMask 
& LVIS_CUT
) 
1974             info
.m_stateMask 
|= wxLIST_STATE_CUT
; 
1975             if ( lvItem
.state 
& LVIS_CUT 
) 
1976                 info
.m_state 
|= wxLIST_STATE_CUT
; 
1978         if ( lvItem
.stateMask 
& LVIS_DROPHILITED
) 
1980             info
.m_stateMask 
|= wxLIST_STATE_DROPHILITED
; 
1981             if ( lvItem
.state 
& LVIS_DROPHILITED 
) 
1982                 info
.m_state 
|= wxLIST_STATE_DROPHILITED
; 
1984         if ( lvItem
.stateMask 
& LVIS_FOCUSED
) 
1986             info
.m_stateMask 
|= wxLIST_STATE_FOCUSED
; 
1987             if ( lvItem
.state 
& LVIS_FOCUSED 
) 
1988                 info
.m_state 
|= wxLIST_STATE_FOCUSED
; 
1990         if ( lvItem
.stateMask 
& LVIS_SELECTED
) 
1992             info
.m_stateMask 
|= wxLIST_STATE_SELECTED
; 
1993             if ( lvItem
.state 
& LVIS_SELECTED 
) 
1994                 info
.m_state 
|= wxLIST_STATE_SELECTED
; 
1998     if ( lvItem
.mask 
& LVIF_TEXT 
) 
2000         info
.m_mask 
|= wxLIST_MASK_TEXT
; 
2001         info
.m_text 
= lvItem
.pszText
; 
2003     if ( lvItem
.mask 
& LVIF_IMAGE 
) 
2005         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
2006         info
.m_image 
= lvItem
.iImage
; 
2008     if ( lvItem
.mask 
& LVIF_PARAM 
) 
2009         info
.m_mask 
|= wxLIST_MASK_DATA
; 
2010     if ( lvItem
.mask 
& LVIF_DI_SETITEM 
) 
2011         info
.m_mask 
|= wxLIST_SET_ITEM
; 
2012     info
.m_col 
= lvItem
.iSubItem
; 
2017             delete[] lvItem
.pszText
; 
2019     lvItem
.mask 
= oldMask
; 
2022 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
) 
2024     if (stateMask 
& wxLIST_STATE_CUT
) 
2026         lvItem
.stateMask 
|= LVIS_CUT
; 
2027         if (state 
& wxLIST_STATE_CUT
) 
2028             lvItem
.state 
|= LVIS_CUT
; 
2030     if (stateMask 
& wxLIST_STATE_DROPHILITED
) 
2032         lvItem
.stateMask 
|= LVIS_DROPHILITED
; 
2033         if (state 
& wxLIST_STATE_DROPHILITED
) 
2034             lvItem
.state 
|= LVIS_DROPHILITED
; 
2036     if (stateMask 
& wxLIST_STATE_FOCUSED
) 
2038         lvItem
.stateMask 
|= LVIS_FOCUSED
; 
2039         if (state 
& wxLIST_STATE_FOCUSED
) 
2040             lvItem
.state 
|= LVIS_FOCUSED
; 
2042     if (stateMask 
& wxLIST_STATE_SELECTED
) 
2044         lvItem
.stateMask 
|= LVIS_SELECTED
; 
2045         if (state 
& wxLIST_STATE_SELECTED
) 
2046             lvItem
.state 
|= LVIS_SELECTED
; 
2050 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
2051                                    const wxListItem
& info
, 
2054     lvItem
.iItem 
= (int) info
.m_itemId
; 
2056     lvItem
.iImage 
= info
.m_image
; 
2057     lvItem
.lParam 
= info
.m_data
; 
2058     lvItem
.stateMask 
= 0; 
2061     lvItem
.iSubItem 
= info
.m_col
; 
2063     if (info
.m_mask 
& wxLIST_MASK_STATE
) 
2065         lvItem
.mask 
|= LVIF_STATE
; 
2067         wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
); 
2070     if (info
.m_mask 
& wxLIST_MASK_TEXT
) 
2072         lvItem
.mask 
|= LVIF_TEXT
; 
2073         if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT 
) 
2075             lvItem
.pszText 
= LPSTR_TEXTCALLBACK
; 
2079             // pszText is not const, hence the cast 
2080             lvItem
.pszText 
= (wxChar 
*)info
.m_text
.c_str(); 
2081             if ( lvItem
.pszText 
) 
2082                 lvItem
.cchTextMax 
= info
.m_text
.Length(); 
2084                 lvItem
.cchTextMax 
= 0; 
2087     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
2088         lvItem
.mask 
|= LVIF_IMAGE
; 
2089     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
2090         lvItem
.mask 
|= LVIF_PARAM
; 
2093 // ---------------------------------------------------------------------------- 
2095 // ---------------------------------------------------------------------------- 
2097 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
) 
2099 wxListEvent::wxListEvent(wxEventType commandType
, int id
) 
2100            : wxNotifyEvent(commandType
, id
) 
2106     m_cancelled 
= FALSE
; 
2109 #endif // wxUSE_LISTCTRL