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
) 
 107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
) 
 109 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
) 
 110 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
) 
 112 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
) 
 113     EVT_PAINT(wxListCtrl::OnPaint
) 
 116 // ============================================================================ 
 118 // ============================================================================ 
 120 // ---------------------------------------------------------------------------- 
 122 // ---------------------------------------------------------------------------- 
 124 void wxListEvent::CopyObject(wxObject
& object_dest
) const 
 126     wxListEvent 
*obj 
= (wxListEvent 
*)&object_dest
; 
 128     wxNotifyEvent::CopyObject(object_dest
); 
 130     obj
->m_code 
= m_code
; 
 131     obj
->m_itemIndex 
= m_itemIndex
; 
 132     obj
->m_oldItemIndex 
= m_oldItemIndex
; 
 134     obj
->m_cancelled 
= m_cancelled
; 
 135     obj
->m_pointDrag 
= m_pointDrag
; 
 136     obj
->m_item
.m_mask 
= m_item
.m_mask
; 
 137     obj
->m_item
.m_itemId 
= m_item
.m_itemId
; 
 138     obj
->m_item
.m_col 
= m_item
.m_col
; 
 139     obj
->m_item
.m_state 
= m_item
.m_state
; 
 140     obj
->m_item
.m_stateMask 
= m_item
.m_stateMask
; 
 141     obj
->m_item
.m_text 
= m_item
.m_text
; 
 142     obj
->m_item
.m_image 
= m_item
.m_image
; 
 143     obj
->m_item
.m_data 
= m_item
.m_data
; 
 144     obj
->m_item
.m_format 
= m_item
.m_format
; 
 145     obj
->m_item
.m_width 
= m_item
.m_width
; 
 147     if ( m_item
.HasAttributes() ) 
 149         obj
->m_item
.SetTextColour(m_item
.GetTextColour()); 
 150         obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour()); 
 151         obj
->m_item
.SetFont(m_item
.GetFont()); 
 155 // ---------------------------------------------------------------------------- 
 156 // wxListCtrl construction 
 157 // ---------------------------------------------------------------------------- 
 159 void wxListCtrl::Init() 
 161     m_imageListNormal 
= NULL
; 
 162     m_imageListSmall 
= NULL
; 
 163     m_imageListState 
= NULL
; 
 164     m_ownsImageListNormal 
= m_ownsImageListSmall 
= m_ownsImageListState 
= FALSE
; 
 168     m_hasAnyAttr 
= FALSE
; 
 171 bool wxListCtrl::Create(wxWindow 
*parent
, 
 176                         const wxValidator
& validator
, 
 177                         const wxString
& name
) 
 180     SetValidator(validator
); 
 181 #endif // wxUSE_VALIDATORS 
 190     m_windowStyle 
= style
; 
 203     m_windowId 
= (id 
== -1) ? NewControlId() : id
; 
 205     DWORD wstyle 
= WS_VISIBLE 
| WS_CHILD 
| WS_TABSTOP 
| 
 206                    LVS_SHAREIMAGELISTS 
| LVS_SHOWSELALWAYS
; 
 208     if ( m_windowStyle 
& wxCLIP_SIBLINGS 
) 
 209         wstyle 
|= WS_CLIPSIBLINGS
; 
 211     if ( wxStyleHasBorder(m_windowStyle
) ) 
 213     m_baseStyle 
= wstyle
; 
 215     if ( !DoCreateControl(x
, y
, width
, height
) ) 
 219         parent
->AddChild(this); 
 224 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
) 
 226     DWORD wstyle 
= m_baseStyle
; 
 229     WXDWORD exStyle 
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
); 
 231     // Even with extended styles, need to combine with WS_BORDER 
 232     // for them to look right. 
 236     long oldStyle 
= 0; // Dummy 
 237     wstyle 
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
); 
 239     // Create the ListView control. 
 240     m_hWnd 
= (WXHWND
)CreateWindowEx(exStyle
, 
 245                                     GetWinHwnd(GetParent()), 
 252         wxLogError(_("Can't create list control window, check that comctl32.dll is installed.")); 
 257     // for comctl32.dll v 4.70+ we want to have this attribute because it's 
 258     // prettier (and also because wxGTK does it like this) 
 259     if ( (wstyle 
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 ) 
 261         ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 
 262                       0, LVS_EX_FULLROWSELECT
); 
 265     SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
)); 
 266     SetForegroundColour(GetParent()->GetForegroundColour()); 
 273 void wxListCtrl::UpdateStyle() 
 277         // The new window view style 
 279         DWORD dwStyleNew 
= ConvertToMSWStyle(dummy
, m_windowStyle
); 
 280         dwStyleNew 
|= m_baseStyle
; 
 282         // Get the current window style. 
 283         DWORD dwStyleOld 
= ::GetWindowLong(GetHwnd(), GWL_STYLE
); 
 285         // Only set the window style if the view bits have changed. 
 286         if ( dwStyleOld 
!= dwStyleNew 
) 
 288             ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
); 
 293 void wxListCtrl::FreeAllAttrs(bool dontRecreate
) 
 297         for ( wxNode 
*node 
= m_attrs
.Next(); node
; node 
= m_attrs
.Next() ) 
 299             delete (wxListItemAttr 
*)node
->Data(); 
 305             m_attrs
.Create(wxKEY_INTEGER
, 1000);        // just as def ctor 
 308         m_hasAnyAttr 
= FALSE
; 
 312 wxListCtrl::~wxListCtrl() 
 314     FreeAllAttrs(TRUE 
/* no need to recreate hash any more */); 
 318         m_textCtrl
->SetHWND(0); 
 319         m_textCtrl
->UnsubclassWin(); 
 324     if (m_ownsImageListNormal
) delete m_imageListNormal
; 
 325     if (m_ownsImageListSmall
) delete m_imageListSmall
; 
 326     if (m_ownsImageListState
) delete m_imageListState
; 
 329 // ---------------------------------------------------------------------------- 
 330 // set/get/change style 
 331 // ---------------------------------------------------------------------------- 
 333 // Add or remove a single window style 
 334 void wxListCtrl::SetSingleStyle(long style
, bool add
) 
 336     long flag 
= GetWindowStyleFlag(); 
 338     // Get rid of conflicting styles 
 341         if ( style 
& wxLC_MASK_TYPE
) 
 342             flag 
= flag 
& ~wxLC_MASK_TYPE
; 
 343         if ( style 
& wxLC_MASK_ALIGN 
) 
 344             flag 
= flag 
& ~wxLC_MASK_ALIGN
; 
 345         if ( style 
& wxLC_MASK_SORT 
) 
 346             flag 
= flag 
& ~wxLC_MASK_SORT
; 
 362     m_windowStyle 
= flag
; 
 367 // Set the whole window style 
 368 void wxListCtrl::SetWindowStyleFlag(long flag
) 
 370     m_windowStyle 
= flag
; 
 375 // Can be just a single style, or a bitlist 
 376 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const 
 379     if ( style 
& wxLC_ICON 
) 
 381         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 382             oldStyle 
-= LVS_SMALLICON
; 
 383         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 384             oldStyle 
-= LVS_REPORT
; 
 385         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 386             oldStyle 
-= LVS_LIST
; 
 390     if ( style 
& wxLC_SMALL_ICON 
) 
 392         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 393             oldStyle 
-= LVS_ICON
; 
 394         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 395             oldStyle 
-= LVS_REPORT
; 
 396         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 397             oldStyle 
-= LVS_LIST
; 
 398         wstyle 
|= LVS_SMALLICON
; 
 401     if ( style 
& wxLC_LIST 
) 
 403         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 404             oldStyle 
-= LVS_ICON
; 
 405         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_REPORT 
) 
 406             oldStyle 
-= LVS_REPORT
; 
 407         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 408             oldStyle 
-= LVS_SMALLICON
; 
 412     if ( style 
& wxLC_REPORT 
) 
 414         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_ICON 
) 
 415             oldStyle 
-= LVS_ICON
; 
 416         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_LIST 
) 
 417             oldStyle 
-= LVS_LIST
; 
 418         if ( (oldStyle 
& LVS_TYPEMASK
) == LVS_SMALLICON 
) 
 419             oldStyle 
-= LVS_SMALLICON
; 
 421         wstyle 
|= LVS_REPORT
; 
 424     if ( style 
& wxLC_ALIGN_LEFT 
) 
 426         if ( oldStyle 
& LVS_ALIGNTOP 
) 
 427             oldStyle 
-= LVS_ALIGNTOP
; 
 428         wstyle 
|= LVS_ALIGNLEFT
; 
 431     if ( style 
& wxLC_ALIGN_TOP 
) 
 433         if ( oldStyle 
& LVS_ALIGNLEFT 
) 
 434             oldStyle 
-= LVS_ALIGNLEFT
; 
 435         wstyle 
|= LVS_ALIGNTOP
; 
 438     if ( style 
& wxLC_AUTOARRANGE 
) 
 439         wstyle 
|= LVS_AUTOARRANGE
; 
 441     if ( style 
& wxLC_NO_SORT_HEADER 
) 
 442         wstyle 
|= LVS_NOSORTHEADER
; 
 444     if ( style 
& wxLC_NO_HEADER 
) 
 445         wstyle 
|= LVS_NOCOLUMNHEADER
; 
 447     if ( style 
& wxLC_EDIT_LABELS 
) 
 448         wstyle 
|= LVS_EDITLABELS
; 
 450     if ( style 
& wxLC_SINGLE_SEL 
) 
 451         wstyle 
|= LVS_SINGLESEL
; 
 453     if ( style 
& wxLC_SORT_ASCENDING 
) 
 455         if ( oldStyle 
& LVS_SORTDESCENDING 
) 
 456             oldStyle 
-= LVS_SORTDESCENDING
; 
 457         wstyle 
|= LVS_SORTASCENDING
; 
 460     if ( style 
& wxLC_SORT_DESCENDING 
) 
 462         if ( oldStyle 
& LVS_SORTASCENDING 
) 
 463             oldStyle 
-= LVS_SORTASCENDING
; 
 464         wstyle 
|= LVS_SORTDESCENDING
; 
 467     if ( style 
& wxLC_VIRTUAL 
) 
 469         int ver 
= wxTheApp
->GetComCtl32Version(); 
 472             wxLogWarning(_("Please install a newer version of comctl32.dll\n" 
 473                            "(at least version 4.70 is required but you have " 
 475                            "or this program won't operate correctly."), 
 476                         ver 
/ 100, ver 
% 100); 
 479         wstyle 
|= LVS_OWNERDATA
; 
 485 // ---------------------------------------------------------------------------- 
 487 // ---------------------------------------------------------------------------- 
 489 // Sets the foreground, i.e. text, colour 
 490 bool wxListCtrl::SetForegroundColour(const wxColour
& col
) 
 492     if ( !wxWindow::SetForegroundColour(col
) ) 
 495     ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
)); 
 500 // Sets the background colour 
 501 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
) 
 503     if ( !wxWindow::SetBackgroundColour(col
) ) 
 506     // we set the same colour for both the "empty" background and the items 
 508     COLORREF color 
= wxColourToRGB(col
); 
 509     ListView_SetBkColor(GetHwnd(), color
); 
 510     ListView_SetTextBkColor(GetHwnd(), color
); 
 515 // Gets information about this column 
 516 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const 
 521     lvCol
.pszText 
= NULL
; 
 523     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 525         lvCol
.mask 
|= LVCF_TEXT
; 
 526         lvCol
.pszText 
= new wxChar
[513]; 
 527         lvCol
.cchTextMax 
= 512; 
 530     bool success 
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0); 
 532     //  item.m_subItem = lvCol.iSubItem; 
 533     item
.m_width 
= lvCol
.cx
; 
 535     if ( (item
.m_mask 
& wxLIST_MASK_TEXT
) && lvCol
.pszText 
) 
 537         item
.m_text 
= lvCol
.pszText
; 
 538         delete[] lvCol
.pszText
; 
 541     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 543         if (lvCol
.fmt 
== LVCFMT_LEFT
) 
 544             item
.m_format 
= wxLIST_FORMAT_LEFT
; 
 545         else if (lvCol
.fmt 
== LVCFMT_RIGHT
) 
 546             item
.m_format 
= wxLIST_FORMAT_RIGHT
; 
 547         else if (lvCol
.fmt 
== LVCFMT_CENTER
) 
 548             item
.m_format 
= wxLIST_FORMAT_CENTRE
; 
 554 // Sets information about this column 
 555 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
) 
 560     lvCol
.pszText 
= NULL
; 
 562     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
 564         lvCol
.mask 
|= LVCF_TEXT
; 
 565         lvCol
.pszText 
= WXSTRINGCAST item
.m_text
; 
 566         lvCol
.cchTextMax 
= 0; // Ignored 
 568     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
 570         lvCol
.mask 
|= LVCF_FMT
; 
 572         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
 573             lvCol
.fmt 
= LVCFMT_LEFT
; 
 574         if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
 575             lvCol
.fmt 
= LVCFMT_RIGHT
; 
 576         if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
 577             lvCol
.fmt 
= LVCFMT_CENTER
; 
 580     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
 582         lvCol
.mask 
|= LVCF_WIDTH
; 
 583         lvCol
.cx 
= item
.m_width
; 
 585         if ( lvCol
.cx 
== wxLIST_AUTOSIZE
) 
 586             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
 587         else if ( lvCol
.cx 
== wxLIST_AUTOSIZE_USEHEADER
) 
 588             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
 590     lvCol
.mask 
|= LVCF_SUBITEM
; 
 591     lvCol
.iSubItem 
= col
; 
 592     return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0); 
 595 // Gets the column width 
 596 int wxListCtrl::GetColumnWidth(int col
) const 
 598     return ListView_GetColumnWidth(GetHwnd(), col
); 
 601 // Sets the column width 
 602 bool wxListCtrl::SetColumnWidth(int col
, int width
) 
 605     if ( m_windowStyle 
& wxLC_LIST 
) 
 609     if ( width2 
== wxLIST_AUTOSIZE
) 
 610         width2 
= LVSCW_AUTOSIZE
; 
 611     else if ( width2 
== wxLIST_AUTOSIZE_USEHEADER
) 
 612         width2 
= LVSCW_AUTOSIZE_USEHEADER
; 
 614     return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0); 
 617 // Gets the number of items that can fit vertically in the 
 618 // visible area of the list control (list or report view) 
 619 // or the total number of items in the list control (icon 
 620 // or small icon view) 
 621 int wxListCtrl::GetCountPerPage() const 
 623     return ListView_GetCountPerPage(GetHwnd()); 
 626 // Gets the edit control for editing labels. 
 627 wxTextCtrl
* wxListCtrl::GetEditControl() const 
 632 // Gets information about the item 
 633 bool wxListCtrl::GetItem(wxListItem
& info
) const 
 636     wxZeroMemory(lvItem
); 
 638     lvItem
.iItem 
= info
.m_itemId
; 
 639     lvItem
.iSubItem 
= info
.m_col
; 
 641     if ( info
.m_mask 
& wxLIST_MASK_TEXT 
) 
 643         lvItem
.mask 
|= LVIF_TEXT
; 
 644         lvItem
.pszText 
= new wxChar
[513]; 
 645         lvItem
.cchTextMax 
= 512; 
 649         lvItem
.pszText 
= NULL
; 
 652     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
 653         lvItem
.mask 
|= LVIF_PARAM
; 
 655     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
 656         lvItem
.mask 
|= LVIF_IMAGE
; 
 658     if ( info
.m_mask 
& wxLIST_MASK_STATE 
) 
 660         lvItem
.mask 
|= LVIF_STATE
; 
 661         // the other bits are hardly interesting anyhow 
 662         lvItem
.stateMask 
= LVIS_SELECTED 
| LVIS_FOCUSED
; 
 665     bool success 
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0; 
 668         wxLogError(_("Couldn't retrieve information about list control item %d."), 
 673         // give NULL as hwnd as we already have everything we need 
 674         wxConvertFromMSWListItem(NULL
, info
, lvItem
); 
 678         delete[] lvItem
.pszText
; 
 683 // Sets information about the item 
 684 bool wxListCtrl::SetItem(wxListItem
& info
) 
 687     wxConvertToMSWListItem(this, info
, item
); 
 690     if ( !ListView_SetItem(GetHwnd(), &item
) ) 
 692         wxLogDebug(_T("ListView_SetItem() failed")); 
 697     // we need to update the item immediately to show the new image 
 698     bool updateNow 
= (info
.m_mask 
& wxLIST_MASK_IMAGE
) != 0; 
 700     // check whether it has any custom attributes 
 701     if ( info
.HasAttributes() ) 
 703         wxListItemAttr 
*attr 
= (wxListItemAttr 
*)m_attrs
.Get(item
.iItem
); 
 706             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
 708             *attr 
= *info
.GetAttributes(); 
 712         // if the colour has changed, we must redraw the item 
 718         // we need this to make the change visible right now 
 719         ListView_Update(GetHwnd(), item
.iItem
); 
 725 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
) 
 729     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 730     info
.m_itemId 
= index
; 
 734         info
.m_image 
= imageId
; 
 735         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
 737     return SetItem(info
); 
 741 // Gets the item state 
 742 int wxListCtrl::GetItemState(long item
, long stateMask
) const 
 746     info
.m_mask 
= wxLIST_MASK_STATE
; 
 747     info
.m_stateMask 
= stateMask
; 
 748     info
.m_itemId 
= item
; 
 756 // Sets the item state 
 757 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
) 
 759     // NB: don't use SetItem() here as it doesn't work with the virtual list 
 762     wxZeroMemory(lvItem
); 
 764     wxConvertToMSWFlags(state
, stateMask
, lvItem
); 
 766     if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
, 
 767                         (WPARAM
)item
, (LPARAM
)&lvItem
) ) 
 769         wxLogLastError(_T("ListView_SetItemState")); 
 777 // Sets the item image 
 778 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
)) 
 782     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
 783     info
.m_image 
= image
; 
 784     info
.m_itemId 
= item
; 
 786     return SetItem(info
); 
 789 // Gets the item text 
 790 wxString 
wxListCtrl::GetItemText(long item
) const 
 794     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 795     info
.m_itemId 
= item
; 
 802 // Sets the item text 
 803 void wxListCtrl::SetItemText(long item
, const wxString
& str
) 
 807     info
.m_mask 
= wxLIST_MASK_TEXT
; 
 808     info
.m_itemId 
= item
; 
 814 // Gets the item data 
 815 long wxListCtrl::GetItemData(long item
) const 
 819     info
.m_mask 
= wxLIST_MASK_DATA
; 
 820     info
.m_itemId 
= item
; 
 827 // Sets the item data 
 828 bool wxListCtrl::SetItemData(long item
, long data
) 
 832     info
.m_mask 
= wxLIST_MASK_DATA
; 
 833     info
.m_itemId 
= item
; 
 836     return SetItem(info
); 
 839 // Gets the item rectangle 
 840 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const 
 844     int code2 
= LVIR_BOUNDS
; 
 845     if ( code 
== wxLIST_RECT_BOUNDS 
) 
 847     else if ( code 
== wxLIST_RECT_ICON 
) 
 849     else if ( code 
== wxLIST_RECT_LABEL 
) 
 853     bool success 
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2 
) != 0); 
 855     bool success 
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0); 
 860     rect
.width 
= rect2
.right 
- rect2
.left
; 
 861     rect
.height 
= rect2
.bottom 
- rect2
.top
; 
 865 // Gets the item position 
 866 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const 
 870     bool success 
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0); 
 872     pos
.x 
= pt
.x
; pos
.y 
= pt
.y
; 
 876 // Sets the item position. 
 877 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
) 
 879     return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0); 
 882 // Gets the number of items in the list control 
 883 int wxListCtrl::GetItemCount() const 
 885     return ListView_GetItemCount(GetHwnd()); 
 888 // Retrieves the spacing between icons in pixels. 
 889 // If small is TRUE, gets the spacing for the small icon 
 890 // view, otherwise the large icon view. 
 891 int wxListCtrl::GetItemSpacing(bool isSmall
) const 
 893     return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
); 
 896 // Gets the number of selected items in the list control 
 897 int wxListCtrl::GetSelectedItemCount() const 
 899     return ListView_GetSelectedCount(GetHwnd()); 
 902 // Gets the text colour of the listview 
 903 wxColour 
wxListCtrl::GetTextColour() const 
 905     COLORREF ref 
= ListView_GetTextColor(GetHwnd()); 
 906     wxColour 
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
)); 
 910 // Sets the text colour of the listview 
 911 void wxListCtrl::SetTextColour(const wxColour
& col
) 
 913     ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue())); 
 916 // Gets the index of the topmost visible item when in 
 917 // list or report view 
 918 long wxListCtrl::GetTopItem() const 
 920     return (long) ListView_GetTopIndex(GetHwnd()); 
 923 // Searches for an item, starting from 'item'. 
 924 // 'geometry' is one of 
 925 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. 
 926 // 'state' is a state bit flag, one or more of 
 927 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. 
 928 // item can be -1 to find the first item that matches the 
 930 // Returns the item or -1 if unsuccessful. 
 931 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const 
 935     if ( geom 
== wxLIST_NEXT_ABOVE 
) 
 937     if ( geom 
== wxLIST_NEXT_ALL 
) 
 939     if ( geom 
== wxLIST_NEXT_BELOW 
) 
 941     if ( geom 
== wxLIST_NEXT_LEFT 
) 
 942         flags 
|= LVNI_TOLEFT
; 
 943     if ( geom 
== wxLIST_NEXT_RIGHT 
) 
 944         flags 
|= LVNI_TORIGHT
; 
 946     if ( state 
& wxLIST_STATE_CUT 
) 
 948     if ( state 
& wxLIST_STATE_DROPHILITED 
) 
 949         flags 
|= LVNI_DROPHILITED
; 
 950     if ( state 
& wxLIST_STATE_FOCUSED 
) 
 951         flags 
|= LVNI_FOCUSED
; 
 952     if ( state 
& wxLIST_STATE_SELECTED 
) 
 953         flags 
|= LVNI_SELECTED
; 
 955     return (long) ListView_GetNextItem(GetHwnd(), item
, flags
); 
 959 wxImageList 
*wxListCtrl::GetImageList(int which
) const 
 961     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
 963         return m_imageListNormal
; 
 965     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
 967         return m_imageListSmall
; 
 969     else if ( which 
== wxIMAGE_LIST_STATE 
) 
 971         return m_imageListState
; 
 976 void wxListCtrl::SetImageList(wxImageList 
*imageList
, int which
) 
 979     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
 981         flags 
= LVSIL_NORMAL
; 
 982         if (m_ownsImageListNormal
) delete m_imageListNormal
; 
 983         m_imageListNormal 
= imageList
; 
 984         m_ownsImageListNormal 
= FALSE
; 
 986     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
 989         if (m_ownsImageListSmall
) delete m_imageListSmall
; 
 990         m_imageListSmall 
= imageList
; 
 991         m_ownsImageListSmall 
= FALSE
; 
 993     else if ( which 
== wxIMAGE_LIST_STATE 
) 
 996         if (m_ownsImageListState
) delete m_imageListState
; 
 997         m_imageListState 
= imageList
; 
 998         m_ownsImageListState 
= FALSE
; 
1000     ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList 
? imageList
->GetHIMAGELIST() : 0, flags
); 
1003 void wxListCtrl::AssignImageList(wxImageList 
*imageList
, int which
) 
1005     SetImageList(imageList
, which
); 
1006     if ( which 
== wxIMAGE_LIST_NORMAL 
) 
1007         m_ownsImageListNormal 
= TRUE
; 
1008     else if ( which 
== wxIMAGE_LIST_SMALL 
) 
1009         m_ownsImageListSmall 
= TRUE
; 
1010     else if ( which 
== wxIMAGE_LIST_STATE 
) 
1011         m_ownsImageListState 
= TRUE
; 
1014 // ---------------------------------------------------------------------------- 
1016 // ---------------------------------------------------------------------------- 
1018 // Arranges the items 
1019 bool wxListCtrl::Arrange(int flag
) 
1022     if ( flag 
== wxLIST_ALIGN_LEFT 
) 
1023         code 
= LVA_ALIGNLEFT
; 
1024     else if ( flag 
== wxLIST_ALIGN_TOP 
) 
1025         code 
= LVA_ALIGNTOP
; 
1026     else if ( flag 
== wxLIST_ALIGN_DEFAULT 
) 
1028     else if ( flag 
== wxLIST_ALIGN_SNAP_TO_GRID 
) 
1029         code 
= LVA_SNAPTOGRID
; 
1031     return (ListView_Arrange(GetHwnd(), code
) != 0); 
1035 bool wxListCtrl::DeleteItem(long item
) 
1037     return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0); 
1040 // Deletes all items 
1041 bool wxListCtrl::DeleteAllItems() 
1043     return (ListView_DeleteAllItems(GetHwnd()) != 0); 
1046 // Deletes all items 
1047 bool wxListCtrl::DeleteAllColumns() 
1049     while ( m_colCount 
> 0 ) 
1051         if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) 
1053             wxLogLastError(wxT("ListView_DeleteColumn")); 
1061     wxASSERT_MSG( m_colCount 
== 0, wxT("no columns should be left") ); 
1067 bool wxListCtrl::DeleteColumn(int col
) 
1069     bool success 
= (ListView_DeleteColumn(GetHwnd(), col
) != 0); 
1071     if ( success 
&& (m_colCount 
> 0) ) 
1076 // Clears items, and columns if there are any. 
1077 void wxListCtrl::ClearAll() 
1080     if ( m_colCount 
> 0 ) 
1084 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
) 
1086     wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) ); 
1088     // VS: ListView_EditLabel requires that the list has focus.   
1090     HWND hWnd 
= (HWND
) ListView_EditLabel(GetHwnd(), item
); 
1094         m_textCtrl
->SetHWND(0); 
1095         m_textCtrl
->UnsubclassWin(); 
1100     m_textCtrl 
= (wxTextCtrl
*) textControlClass
->CreateObject(); 
1101     m_textCtrl
->SetHWND((WXHWND
) hWnd
); 
1102     m_textCtrl
->SubclassWin((WXHWND
) hWnd
); 
1107 // End label editing, optionally cancelling the edit 
1108 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
)) 
1110     wxFAIL_MSG( _T("not implemented") ); 
1115 // Ensures this item is visible 
1116 bool wxListCtrl::EnsureVisible(long item
) 
1118     return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0; 
1121 // Find an item whose label matches this string, starting from the item after 'start' 
1122 // or the beginning if 'start' is -1. 
1123 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
) 
1125     LV_FINDINFO findInfo
; 
1127     findInfo
.flags 
= LVFI_STRING
; 
1129         findInfo
.flags 
|= LVFI_PARTIAL
; 
1132     // ListView_FindItem() excludes the first item from search and to look 
1133     // through all the items you need to start from -1 which is unnatural and 
1134     // inconsistent with the generic version - so we adjust the index 
1137     return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
); 
1140 // Find an item whose data matches this data, starting from the item after 'start' 
1141 // or the beginning if 'start' is -1. 
1142 long wxListCtrl::FindItem(long start
, long data
) 
1144     LV_FINDINFO findInfo
; 
1146     findInfo
.flags 
= LVFI_PARAM
; 
1147     findInfo
.lParam 
= data
; 
1149     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1152 // Find an item nearest this position in the specified direction, starting from 
1153 // the item after 'start' or the beginning if 'start' is -1. 
1154 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
) 
1156     LV_FINDINFO findInfo
; 
1158     findInfo
.flags 
= LVFI_NEARESTXY
; 
1159     findInfo
.pt
.x 
= pt
.x
; 
1160     findInfo
.pt
.y 
= pt
.y
; 
1161     findInfo
.vkDirection 
= VK_RIGHT
; 
1163     if ( direction 
== wxLIST_FIND_UP 
) 
1164         findInfo
.vkDirection 
= VK_UP
; 
1165     else if ( direction 
== wxLIST_FIND_DOWN 
) 
1166         findInfo
.vkDirection 
= VK_DOWN
; 
1167     else if ( direction 
== wxLIST_FIND_LEFT 
) 
1168         findInfo
.vkDirection 
= VK_LEFT
; 
1169     else if ( direction 
== wxLIST_FIND_RIGHT 
) 
1170         findInfo
.vkDirection 
= VK_RIGHT
; 
1172     return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
); 
1175 // Determines which item (if any) is at the specified point, 
1176 // giving details in 'flags' (see wxLIST_HITTEST_... flags above) 
1177 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
) 
1179     LV_HITTESTINFO hitTestInfo
; 
1180     hitTestInfo
.pt
.x 
= (int) point
.x
; 
1181     hitTestInfo
.pt
.y 
= (int) point
.y
; 
1183     ListView_HitTest(GetHwnd(), & hitTestInfo
); 
1186     if ( hitTestInfo
.flags 
& LVHT_ABOVE 
) 
1187         flags 
|= wxLIST_HITTEST_ABOVE
; 
1188     if ( hitTestInfo
.flags 
& LVHT_BELOW 
) 
1189         flags 
|= wxLIST_HITTEST_BELOW
; 
1190     if ( hitTestInfo
.flags 
& LVHT_NOWHERE 
) 
1191         flags 
|= wxLIST_HITTEST_NOWHERE
; 
1192     if ( hitTestInfo
.flags 
& LVHT_ONITEMICON 
) 
1193         flags 
|= wxLIST_HITTEST_ONITEMICON
; 
1194     if ( hitTestInfo
.flags 
& LVHT_ONITEMLABEL 
) 
1195         flags 
|= wxLIST_HITTEST_ONITEMLABEL
; 
1196     if ( hitTestInfo
.flags 
& LVHT_ONITEMSTATEICON 
) 
1197         flags 
|= wxLIST_HITTEST_ONITEMSTATEICON
; 
1198     if ( hitTestInfo
.flags 
& LVHT_TOLEFT 
) 
1199         flags 
|= wxLIST_HITTEST_TOLEFT
; 
1200     if ( hitTestInfo
.flags 
& LVHT_TORIGHT 
) 
1201         flags 
|= wxLIST_HITTEST_TORIGHT
; 
1203     return (long) hitTestInfo
.iItem
; 
1206 // Inserts an item, returning the index of the new item if successful, 
1208 long wxListCtrl::InsertItem(wxListItem
& info
) 
1210     wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); 
1213     wxConvertToMSWListItem(this, info
, item
); 
1215     // check whether it has any custom attributes 
1216     if ( info
.HasAttributes() ) 
1219         wxListItemAttr 
*attr
; 
1220         attr 
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
); 
1224             m_attrs
.Put(item
.iItem
, (wxObject 
*)new wxListItemAttr(*info
.GetAttributes())); 
1226         else *attr 
= *info
.GetAttributes(); 
1228         m_hasAnyAttr 
= TRUE
; 
1231     return (long) ListView_InsertItem(GetHwnd(), & item
); 
1234 long wxListCtrl::InsertItem(long index
, const wxString
& label
) 
1237     info
.m_text 
= label
; 
1238     info
.m_mask 
= wxLIST_MASK_TEXT
; 
1239     info
.m_itemId 
= index
; 
1240     return InsertItem(info
); 
1243 // Inserts an image item 
1244 long wxListCtrl::InsertItem(long index
, int imageIndex
) 
1247     info
.m_image 
= imageIndex
; 
1248     info
.m_mask 
= wxLIST_MASK_IMAGE
; 
1249     info
.m_itemId 
= index
; 
1250     return InsertItem(info
); 
1253 // Inserts an image/string item 
1254 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
) 
1257     info
.m_image 
= imageIndex
; 
1258     info
.m_text 
= label
; 
1259     info
.m_mask 
= wxLIST_MASK_IMAGE 
| wxLIST_MASK_TEXT
; 
1260     info
.m_itemId 
= index
; 
1261     return InsertItem(info
); 
1264 // For list view mode (only), inserts a column. 
1265 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
) 
1270     lvCol
.pszText 
= NULL
; 
1272     if ( item
.m_mask 
& wxLIST_MASK_TEXT 
) 
1274         lvCol
.mask 
|= LVCF_TEXT
; 
1275         lvCol
.pszText 
= WXSTRINGCAST item
.m_text
; 
1276         lvCol
.cchTextMax 
= 0; // Ignored 
1278     if ( item
.m_mask 
& wxLIST_MASK_FORMAT 
) 
1280         lvCol
.mask 
|= LVCF_FMT
; 
1282         if ( item
.m_format 
== wxLIST_FORMAT_LEFT 
) 
1283             lvCol
.fmt 
= LVCFMT_LEFT
; 
1284         if ( item
.m_format 
== wxLIST_FORMAT_RIGHT 
) 
1285             lvCol
.fmt 
= LVCFMT_RIGHT
; 
1286         if ( item
.m_format 
== wxLIST_FORMAT_CENTRE 
) 
1287             lvCol
.fmt 
= LVCFMT_CENTER
; 
1290     lvCol
.mask 
|= LVCF_WIDTH
; 
1291     if ( item
.m_mask 
& wxLIST_MASK_WIDTH 
) 
1293         if ( item
.m_width 
== wxLIST_AUTOSIZE
) 
1294             lvCol
.cx 
= LVSCW_AUTOSIZE
; 
1295         else if ( item
.m_width 
== wxLIST_AUTOSIZE_USEHEADER
) 
1296             lvCol
.cx 
= LVSCW_AUTOSIZE_USEHEADER
; 
1298             lvCol
.cx 
= item
.m_width
; 
1302         // always give some width to the new column: this one is compatible 
1307     lvCol
.mask 
|= LVCF_SUBITEM
; 
1308     lvCol
.iSubItem 
= col
; 
1310     bool success 
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1; 
1317         wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), 
1324 long wxListCtrl::InsertColumn(long col
, 
1325                               const wxString
& heading
, 
1330     item
.m_mask 
= wxLIST_MASK_TEXT 
| wxLIST_MASK_FORMAT
; 
1331     item
.m_text 
= heading
; 
1334         item
.m_mask 
|= wxLIST_MASK_WIDTH
; 
1335         item
.m_width 
= width
; 
1337     item
.m_format 
= format
; 
1339     return InsertColumn(col
, item
); 
1342 // Scrolls the list control. If in icon, small icon or report view mode, 
1343 // x specifies the number of pixels to scroll. If in list view mode, x 
1344 // specifies the number of columns to scroll. 
1345 // If in icon, small icon or list view mode, y specifies the number of pixels 
1346 // to scroll. If in report view mode, y specifies the number of lines to scroll. 
1347 bool wxListCtrl::ScrollList(int dx
, int dy
) 
1349     return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0); 
1354 // fn is a function which takes 3 long arguments: item1, item2, data. 
1355 // item1 is the long data associated with a first item (NOT the index). 
1356 // item2 is the long data associated with a second item (NOT the index). 
1357 // data is the same value as passed to SortItems. 
1358 // The return value is a negative number if the first item should precede the second 
1359 // item, a positive number of the second item should precede the first, 
1360 // or zero if the two items are equivalent. 
1362 // data is arbitrary data to be passed to the sort function. 
1363 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
) 
1365     return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0); 
1368 // ---------------------------------------------------------------------------- 
1369 // message processing 
1370 // ---------------------------------------------------------------------------- 
1372 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
) 
1374     if (cmd 
== EN_UPDATE
) 
1376         wxCommandEvent 
event(wxEVT_COMMAND_TEXT_UPDATED
, id
); 
1377         event
.SetEventObject( this ); 
1378         ProcessCommand(event
); 
1381     else if (cmd 
== EN_KILLFOCUS
) 
1383         wxCommandEvent 
event(wxEVT_KILL_FOCUS
, id
); 
1384         event
.SetEventObject( this ); 
1385         ProcessCommand(event
); 
1392 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM 
*result
) 
1394     // prepare the event 
1395     // ----------------- 
1397     wxListEvent 
event(wxEVT_NULL
, m_windowId
); 
1398     wxEventType eventType 
= wxEVT_NULL
; 
1400     NMHDR 
*nmhdr 
= (NMHDR 
*)lParam
; 
1402     // almost all messages use NM_LISTVIEW 
1403     NM_LISTVIEW 
*nmLV 
= (NM_LISTVIEW 
*)nmhdr
; 
1405     // this is true for almost all events 
1406     event
.m_item
.m_data 
= nmLV
->lParam
; 
1408     switch ( nmhdr
->code 
) 
1410         case LVN_BEGINRDRAG
: 
1411             eventType 
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
; 
1415             if ( eventType 
== wxEVT_NULL 
) 
1417                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_DRAG
; 
1420             event
.m_itemIndex 
= nmLV
->iItem
; 
1421             event
.m_pointDrag
.x 
= nmLV
->ptAction
.x
; 
1422             event
.m_pointDrag
.y 
= nmLV
->ptAction
.y
; 
1425         case LVN_BEGINLABELEDIT
: 
1427                 eventType 
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
; 
1428                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1429                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1430                 event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1434         case LVN_COLUMNCLICK
: 
1435             eventType 
= wxEVT_COMMAND_LIST_COL_CLICK
; 
1436             event
.m_itemIndex 
= -1; 
1437             event
.m_col 
= nmLV
->iSubItem
; 
1440         case LVN_DELETEALLITEMS
: 
1441             eventType 
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
; 
1442             event
.m_itemIndex 
= -1; 
1448         case LVN_DELETEITEM
: 
1449             eventType 
= wxEVT_COMMAND_LIST_DELETE_ITEM
; 
1450             event
.m_itemIndex 
= nmLV
->iItem
; 
1454                 delete (wxListItemAttr 
*)m_attrs
.Delete(nmLV
->iItem
); 
1458         case LVN_ENDLABELEDIT
: 
1460                 eventType 
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
; 
1461                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1462                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1463                 if ( info
->item
.pszText 
== NULL 
|| info
->item
.iItem 
== -1 ) 
1466                 event
.m_itemIndex 
= event
.m_item
.m_itemId
; 
1470         case LVN_SETDISPINFO
: 
1472                 eventType 
= wxEVT_COMMAND_LIST_SET_INFO
; 
1473                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1474                 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
); 
1478         case LVN_INSERTITEM
: 
1479             eventType 
= wxEVT_COMMAND_LIST_INSERT_ITEM
; 
1480             event
.m_itemIndex 
= nmLV
->iItem
; 
1483         case LVN_ITEMCHANGED
: 
1484             // This needs to be sent to wxListCtrl as a rather more concrete 
1485             // event. For now, just detect a selection or deselection. 
1486             if ( (nmLV
->uNewState 
& LVIS_SELECTED
) && !(nmLV
->uOldState 
& LVIS_SELECTED
) ) 
1488                 eventType 
= wxEVT_COMMAND_LIST_ITEM_SELECTED
; 
1489                 event
.m_itemIndex 
= nmLV
->iItem
; 
1491             else if ( !(nmLV
->uNewState 
& LVIS_SELECTED
) && (nmLV
->uOldState 
& LVIS_SELECTED
) ) 
1493                 eventType 
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
; 
1494                 event
.m_itemIndex 
= nmLV
->iItem
; 
1504                 LV_KEYDOWN 
*info 
= (LV_KEYDOWN 
*)lParam
; 
1505                 WORD wVKey 
= info
->wVKey
; 
1507                 // get the current selection 
1508                 long lItem 
= GetNextItem(-1, 
1510                                          wxLIST_STATE_SELECTED
); 
1512                 // <Enter> or <Space> activate the selected item if any (but 
1513                 // not with Shift and/or Ctrl as then they have a predefined 
1514                 // meaning for the list view) 
1516                      (wVKey 
== VK_RETURN 
|| wVKey 
== VK_SPACE
) && 
1517                      !(wxIsShiftDown() || wxIsCtrlDown()) ) 
1519                     eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1523                     eventType 
= wxEVT_COMMAND_LIST_KEY_DOWN
; 
1524                     event
.m_code 
= wxCharCodeMSWToWX(wVKey
); 
1528                 event
.m_item
.m_itemId 
= lItem
; 
1532                     // fill the other fields too 
1533                     event
.m_item
.m_text 
= GetItemText(lItem
); 
1534                     event
.m_item
.m_data 
= GetItemData(lItem
); 
1540             // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do 
1542             if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1547             // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event 
1548             // if it happened on an item (and not on empty place) 
1549             if ( nmLV
->iItem 
== -1 ) 
1555             eventType 
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
; 
1556             event
.m_itemIndex 
= nmLV
->iItem
; 
1557             event
.m_item
.m_text 
= GetItemText(nmLV
->iItem
); 
1558             event
.m_item
.m_data 
= GetItemData(nmLV
->iItem
); 
1562             /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to 
1563                subclass and check for the actual WM_RBUTTONDOWN message, 
1564                because NM_RCLICK waits for the WM_RBUTTONUP message as well 
1565                before firing off. We want to have notify events for both down 
1568                 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), 
1569                 // don't do anything else 
1570                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1575                 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event 
1576                 LV_HITTESTINFO lvhti
; 
1577                 wxZeroMemory(lvhti
); 
1579                 ::GetCursorPos(&(lvhti
.pt
)); 
1580                 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
)); 
1581                 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 ) 
1583                     if ( lvhti
.flags 
& LVHT_ONITEM 
) 
1585                         eventType 
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
; 
1586                         event
.m_itemIndex 
= lvhti
.iItem
; 
1587                         event
.m_pointDrag
.x 
= lvhti
.pt
.x
; 
1588                         event
.m_pointDrag
.y 
= lvhti
.pt
.y
; 
1595         case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ****** 
1597                 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do 
1599                 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) ) 
1604                 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event 
1605                 eventType 
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
; 
1606                 NMITEMACTIVATE
* hdr 
= (NMITEMACTIVATE
*)lParam
; 
1607                 event
.m_itemIndex 
= hdr
->iItem
; 
1612 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 
1614             *result 
= OnCustomDraw(lParam
); 
1617 #endif // _WIN32_IE >= 0x300 
1619         case LVN_ODCACHEHINT
: 
1621                 const NM_CACHEHINT 
*cacheHint 
= (NM_CACHEHINT 
*)lParam
; 
1623                 eventType 
= wxEVT_COMMAND_LIST_CACHE_HINT
; 
1624                 event
.m_oldItemIndex 
= cacheHint
->iFrom
; 
1625                 event
.m_itemIndex 
= cacheHint
->iTo
; 
1629         case LVN_GETDISPINFO
: 
1632                 LV_DISPINFO 
*info 
= (LV_DISPINFO 
*)lParam
; 
1634                 LV_ITEM
& lvi 
= info
->item
; 
1635                 long item 
= lvi
.iItem
; 
1637                 if ( lvi
.mask 
& LVIF_TEXT 
) 
1639                     wxString text 
= OnGetItemText(item
, lvi
.iSubItem
); 
1640                     wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
); 
1643                 if ( lvi
.mask 
& LVIF_IMAGE 
) 
1645                     lvi
.iImage 
= OnGetItemImage(item
); 
1648                 // a little dose of healthy paranoia: as we never use 
1649                 // LVM_SETCALLBACKMASK we're not supposed to get these ones 
1650                 wxASSERT_MSG( !(lvi
.mask 
& LVIF_STATE
), 
1651                               _T("we don't support state callbacks yet!") ); 
1658             return wxControl::MSWOnNotify(idCtrl
, lParam
, result
); 
1661     // process the event 
1662     // ----------------- 
1664     event
.SetEventObject( this ); 
1665     event
.SetEventType(eventType
); 
1667     if ( !GetEventHandler()->ProcessEvent(event
) ) 
1673     switch ( nmhdr
->code 
) 
1675         case LVN_DELETEALLITEMS
: 
1676             // always return TRUE to suppress all additional LVN_DELETEITEM 
1677             // notifications - this makes deleting all items from a list ctrl 
1683         case LVN_ENDLABELEDIT
: 
1684             // logic here is inversed compared to all the other messages 
1685             *result 
= event
.IsAllowed(); 
1690     *result 
= !event
.IsAllowed(); 
1695 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 
1697 WXLPARAM 
wxListCtrl::OnCustomDraw(WXLPARAM lParam
) 
1699     LPNMLVCUSTOMDRAW lplvcd 
= (LPNMLVCUSTOMDRAW
)lParam
; 
1700     NMCUSTOMDRAW
& nmcd 
= lplvcd
->nmcd
; 
1701     switch ( nmcd
.dwDrawStage 
) 
1704             // if we've got any items with non standard attributes, 
1705             // notify us before painting each item 
1707             // for virtual controls, always suppose that we have attributes as 
1708             // there is no way to check for this 
1709             return IsVirtual() || m_hasAnyAttr 
? CDRF_NOTIFYITEMDRAW
 
1712         case CDDS_ITEMPREPAINT
: 
1714                 size_t item 
= (size_t)nmcd
.dwItemSpec
; 
1715                 if ( item 
>= (size_t)GetItemCount() ) 
1717                     // we get this message with item == 0 for an empty control, 
1718                     // we must ignore it as calling OnGetItemAttr() would be 
1720                     return CDRF_DODEFAULT
; 
1723                 wxListItemAttr 
*attr 
= 
1724                     IsVirtual() ? OnGetItemAttr(item
) 
1725                                 : (wxListItemAttr 
*)m_attrs
.Get(item
); 
1729                     // nothing to do for this item 
1730                     return CDRF_DODEFAULT
; 
1734                 wxColour colText
, colBack
; 
1735                 if ( attr
->HasFont() ) 
1737                     wxFont font 
= attr
->GetFont(); 
1738                     hFont 
= (HFONT
)font
.GetResourceHandle(); 
1745                 if ( attr
->HasTextColour() ) 
1747                     colText 
= attr
->GetTextColour(); 
1751                     colText 
= GetTextColour(); 
1754                 if ( attr
->HasBackgroundColour() ) 
1756                     colBack 
= attr
->GetBackgroundColour(); 
1760                     colBack 
= GetBackgroundColour(); 
1763                 lplvcd
->clrText 
= wxColourToRGB(colText
); 
1764                 lplvcd
->clrTextBk 
= wxColourToRGB(colBack
); 
1766                 // note that if we wanted to set colours for 
1767                 // individual columns (subitems), we would have 
1768                 // returned CDRF_NOTIFYSUBITEMREDRAW from here 
1771                     ::SelectObject(nmcd
.hdc
, hFont
); 
1773                     return CDRF_NEWFONT
; 
1776             // fall through to return CDRF_DODEFAULT 
1779             return CDRF_DODEFAULT
; 
1783 #endif // NM_CUSTOMDRAW supported 
1785 // Necessary for drawing hrules and vrules, if specified 
1786 void wxListCtrl::OnPaint(wxPaintEvent
& event
) 
1790     wxControl::OnPaint(event
); 
1792     // Reset the device origin since it may have been set 
1793     dc
.SetDeviceOrigin(0, 0); 
1795     bool drawHRules 
= ((GetWindowStyle() & wxLC_HRULES
) != 0); 
1796     bool drawVRules 
= ((GetWindowStyle() & wxLC_VRULES
) != 0); 
1798     if (!drawHRules 
&& !drawVRules
) 
1800     if ((GetWindowStyle() & wxLC_REPORT
) == 0) 
1803     wxPen 
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
); 
1805     dc
.SetBrush(* wxTRANSPARENT_BRUSH
); 
1807     wxSize clientSize 
= GetClientSize(); 
1811     int itemCount 
= GetItemCount(); 
1813     for (i 
= 0; i 
< itemCount
; i
++) 
1815         if (GetItemRect(i
, itemRect
)) 
1817             cy 
= itemRect
.GetTop(); 
1818             if (i 
!= 0) // Don't draw the first one 
1820                 dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
1823             if (i 
== (GetItemCount() - 1)) 
1825                 cy 
= itemRect
.GetBottom(); 
1826                 dc
.DrawLine(0, cy
, clientSize
.x
, cy
); 
1830     i 
= (GetItemCount() - 1); 
1831     if (drawVRules 
&& (i 
> -1)) 
1833         wxRect firstItemRect
; 
1834         GetItemRect(0, firstItemRect
); 
1836         if (GetItemRect(i
, itemRect
)) 
1839             int x 
= itemRect
.GetX(); 
1840             for (col 
= 0; col 
< GetColumnCount(); col
++) 
1842                 int colWidth 
= GetColumnWidth(col
); 
1844                 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom()); 
1850 // ---------------------------------------------------------------------------- 
1851 // virtual list controls 
1852 // ---------------------------------------------------------------------------- 
1854 wxString 
wxListCtrl::OnGetItemText(long item
, long col
) const 
1856     // this is a pure virtual function, in fact - which is not really pure 
1857     // because the controls which are not virtual don't need to implement it 
1858     wxFAIL_MSG( _T("not supposed to be called") ); 
1860     return wxEmptyString
; 
1863 int wxListCtrl::OnGetItemImage(long item
) const 
1866     wxFAIL_MSG( _T("not supposed to be called") ); 
1871 wxListItemAttr 
*wxListCtrl::OnGetItemAttr(long item
) const 
1873     wxASSERT_MSG( item 
>= 0 && item 
< GetItemCount(), 
1874                   _T("invalid item index in OnGetItemAttr()") ); 
1876     // no attributes by default 
1880 void wxListCtrl::SetItemCount(long count
) 
1882     wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); 
1884     if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) ) 
1886         wxLogLastError(_T("ListView_SetItemCount")); 
1890 void wxListCtrl::RefreshItem(long item
) 
1892     if ( !ListView_Update(GetHwnd(), item
) ) 
1894         wxLogLastError(_T("ListView_Update")); 
1898 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
) 
1900     for ( long item 
= itemFrom
; item 
<= itemTo
; item
++ ) 
1906 // ---------------------------------------------------------------------------- 
1908 // ---------------------------------------------------------------------------- 
1910 // List item structure 
1911 wxListItem::wxListItem() 
1921     m_format 
= wxLIST_FORMAT_CENTRE
; 
1927 void wxListItem::Clear() 
1936     m_format 
= wxLIST_FORMAT_CENTRE
; 
1938     m_text 
= wxEmptyString
; 
1940     if (m_attr
) delete m_attr
; 
1944 void wxListItem::ClearAttributes() 
1946     if (m_attr
) delete m_attr
; 
1950 static void wxConvertFromMSWListItem(HWND hwndListCtrl
, 
1954     info
.m_data 
= lvItem
.lParam
; 
1957     info
.m_stateMask 
= 0; 
1958     info
.m_itemId 
= lvItem
.iItem
; 
1960     long oldMask 
= lvItem
.mask
; 
1962     bool needText 
= FALSE
; 
1963     if (hwndListCtrl 
!= 0) 
1965         if ( lvItem
.mask 
& LVIF_TEXT 
) 
1972             lvItem
.pszText 
= new wxChar
[513]; 
1973             lvItem
.cchTextMax 
= 512; 
1975         lvItem
.mask 
|= LVIF_TEXT 
| LVIF_IMAGE 
| LVIF_PARAM
; 
1976         ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
); 
1979     if ( lvItem
.mask 
& LVIF_STATE 
) 
1981         info
.m_mask 
|= wxLIST_MASK_STATE
; 
1983         if ( lvItem
.stateMask 
& LVIS_CUT
) 
1985             info
.m_stateMask 
|= wxLIST_STATE_CUT
; 
1986             if ( lvItem
.state 
& LVIS_CUT 
) 
1987                 info
.m_state 
|= wxLIST_STATE_CUT
; 
1989         if ( lvItem
.stateMask 
& LVIS_DROPHILITED
) 
1991             info
.m_stateMask 
|= wxLIST_STATE_DROPHILITED
; 
1992             if ( lvItem
.state 
& LVIS_DROPHILITED 
) 
1993                 info
.m_state 
|= wxLIST_STATE_DROPHILITED
; 
1995         if ( lvItem
.stateMask 
& LVIS_FOCUSED
) 
1997             info
.m_stateMask 
|= wxLIST_STATE_FOCUSED
; 
1998             if ( lvItem
.state 
& LVIS_FOCUSED 
) 
1999                 info
.m_state 
|= wxLIST_STATE_FOCUSED
; 
2001         if ( lvItem
.stateMask 
& LVIS_SELECTED
) 
2003             info
.m_stateMask 
|= wxLIST_STATE_SELECTED
; 
2004             if ( lvItem
.state 
& LVIS_SELECTED 
) 
2005                 info
.m_state 
|= wxLIST_STATE_SELECTED
; 
2009     if ( lvItem
.mask 
& LVIF_TEXT 
) 
2011         info
.m_mask 
|= wxLIST_MASK_TEXT
; 
2012         info
.m_text 
= lvItem
.pszText
; 
2014     if ( lvItem
.mask 
& LVIF_IMAGE 
) 
2016         info
.m_mask 
|= wxLIST_MASK_IMAGE
; 
2017         info
.m_image 
= lvItem
.iImage
; 
2019     if ( lvItem
.mask 
& LVIF_PARAM 
) 
2020         info
.m_mask 
|= wxLIST_MASK_DATA
; 
2021     if ( lvItem
.mask 
& LVIF_DI_SETITEM 
) 
2022         info
.m_mask 
|= wxLIST_SET_ITEM
; 
2023     info
.m_col 
= lvItem
.iSubItem
; 
2028             delete[] lvItem
.pszText
; 
2030     lvItem
.mask 
= oldMask
; 
2033 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
) 
2035     if (stateMask 
& wxLIST_STATE_CUT
) 
2037         lvItem
.stateMask 
|= LVIS_CUT
; 
2038         if (state 
& wxLIST_STATE_CUT
) 
2039             lvItem
.state 
|= LVIS_CUT
; 
2041     if (stateMask 
& wxLIST_STATE_DROPHILITED
) 
2043         lvItem
.stateMask 
|= LVIS_DROPHILITED
; 
2044         if (state 
& wxLIST_STATE_DROPHILITED
) 
2045             lvItem
.state 
|= LVIS_DROPHILITED
; 
2047     if (stateMask 
& wxLIST_STATE_FOCUSED
) 
2049         lvItem
.stateMask 
|= LVIS_FOCUSED
; 
2050         if (state 
& wxLIST_STATE_FOCUSED
) 
2051             lvItem
.state 
|= LVIS_FOCUSED
; 
2053     if (stateMask 
& wxLIST_STATE_SELECTED
) 
2055         lvItem
.stateMask 
|= LVIS_SELECTED
; 
2056         if (state 
& wxLIST_STATE_SELECTED
) 
2057             lvItem
.state 
|= LVIS_SELECTED
; 
2061 static void wxConvertToMSWListItem(const wxListCtrl 
*ctrl
, 
2062                                    const wxListItem
& info
, 
2065     lvItem
.iItem 
= (int) info
.m_itemId
; 
2067     lvItem
.iImage 
= info
.m_image
; 
2068     lvItem
.lParam 
= info
.m_data
; 
2069     lvItem
.stateMask 
= 0; 
2072     lvItem
.iSubItem 
= info
.m_col
; 
2074     if (info
.m_mask 
& wxLIST_MASK_STATE
) 
2076         lvItem
.mask 
|= LVIF_STATE
; 
2078         wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
); 
2081     if (info
.m_mask 
& wxLIST_MASK_TEXT
) 
2083         lvItem
.mask 
|= LVIF_TEXT
; 
2084         if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT 
) 
2086             lvItem
.pszText 
= LPSTR_TEXTCALLBACK
; 
2090             // pszText is not const, hence the cast 
2091             lvItem
.pszText 
= (wxChar 
*)info
.m_text
.c_str(); 
2092             if ( lvItem
.pszText 
) 
2093                 lvItem
.cchTextMax 
= info
.m_text
.Length(); 
2095                 lvItem
.cchTextMax 
= 0; 
2098     if (info
.m_mask 
& wxLIST_MASK_IMAGE
) 
2099         lvItem
.mask 
|= LVIF_IMAGE
; 
2100     if (info
.m_mask 
& wxLIST_MASK_DATA
) 
2101         lvItem
.mask 
|= LVIF_PARAM
; 
2104 // ---------------------------------------------------------------------------- 
2106 // ---------------------------------------------------------------------------- 
2108 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
) 
2110 wxListEvent::wxListEvent(wxEventType commandType
, int id
) 
2111            : wxNotifyEvent(commandType
, id
) 
2117     m_cancelled 
= FALSE
; 
2120 #endif // wxUSE_LISTCTRL