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 // mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff
73 typedef struct tagNMLVCACHEHINT
80 #define NM_CACHEHINT NMLVCACHEHINT
83 #ifndef LVN_ODCACHEHINT
84 #define LVN_ODCACHEHINT (-113)
87 #ifndef ListView_GetHeader
88 #define ListView_GetHeader(w) (HWND)SendMessage((w),LVM_GETHEADER,0,0)
92 #define LVM_GETHEADER (LVM_FIRST+31)
95 #ifndef Header_GetItemRect
96 #define Header_GetItemRect(w,i,r) \
97 (BOOL)SendMessage((w),HDM_GETITEMRECT,(WPARAM)(i),(LPARAM)(r))
100 #ifndef HDM_GETITEMRECT
101 #define HDM_GETITEMRECT (HDM_FIRST+7)
105 #define LVCF_IMAGE 0x0010
108 #ifndef LVCFMT_BITMAP_ON_RIGHT
109 #define LVCFMT_BITMAP_ON_RIGHT 0x1000
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // convert our state and mask flags to LV_ITEM constants
117 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
119 // convert wxListItem to LV_ITEM
120 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
121 const wxListItem
& info
, LV_ITEM
& lvItem
);
123 // convert LV_ITEM to wxListItem
124 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
126 /* const */ LV_ITEM
& lvItem
);
128 // convert our wxListItem to LV_COLUMN
129 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
137 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
138 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
139 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
140 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
141 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
142 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
143 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
144 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
145 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
146 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
147 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
148 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
149 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
150 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
151 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
152 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
153 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
154 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
155 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
156 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
157 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
159 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
160 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
161 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
163 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
164 EVT_PAINT(wxListCtrl::OnPaint
)
167 // ============================================================================
169 // ============================================================================
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 void wxListEvent::CopyObject(wxObject
& object_dest
) const
177 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
179 wxNotifyEvent::CopyObject(object_dest
);
181 obj
->m_code
= m_code
;
182 obj
->m_itemIndex
= m_itemIndex
;
183 obj
->m_oldItemIndex
= m_oldItemIndex
;
185 obj
->m_cancelled
= m_cancelled
;
186 obj
->m_pointDrag
= m_pointDrag
;
187 obj
->m_item
.m_mask
= m_item
.m_mask
;
188 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
189 obj
->m_item
.m_col
= m_item
.m_col
;
190 obj
->m_item
.m_state
= m_item
.m_state
;
191 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
192 obj
->m_item
.m_text
= m_item
.m_text
;
193 obj
->m_item
.m_image
= m_item
.m_image
;
194 obj
->m_item
.m_data
= m_item
.m_data
;
195 obj
->m_item
.m_format
= m_item
.m_format
;
196 obj
->m_item
.m_width
= m_item
.m_width
;
198 if ( m_item
.HasAttributes() )
200 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
201 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
202 obj
->m_item
.SetFont(m_item
.GetFont());
206 // ----------------------------------------------------------------------------
207 // wxListCtrl construction
208 // ----------------------------------------------------------------------------
210 void wxListCtrl::Init()
212 m_imageListNormal
= NULL
;
213 m_imageListSmall
= NULL
;
214 m_imageListState
= NULL
;
215 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
219 m_hasAnyAttr
= FALSE
;
222 bool wxListCtrl::Create(wxWindow
*parent
,
227 const wxValidator
& validator
,
228 const wxString
& name
)
231 SetValidator(validator
);
232 #endif // wxUSE_VALIDATORS
241 m_windowStyle
= style
;
254 m_windowId
= (id
== -1) ? NewControlId() : id
;
256 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
257 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
259 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
260 wstyle
|= WS_CLIPSIBLINGS
;
262 if ( wxStyleHasBorder(m_windowStyle
) )
264 m_baseStyle
= wstyle
;
266 if ( !DoCreateControl(x
, y
, width
, height
) )
270 parent
->AddChild(this);
275 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
277 DWORD wstyle
= m_baseStyle
;
280 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
282 // Even with extended styles, need to combine with WS_BORDER
283 // for them to look right.
287 long oldStyle
= 0; // Dummy
288 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
290 // Create the ListView control.
291 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
296 GetWinHwnd(GetParent()),
303 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
308 // for comctl32.dll v 4.70+ we want to have this attribute because it's
309 // prettier (and also because wxGTK does it like this)
310 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
312 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
313 0, LVS_EX_FULLROWSELECT
);
316 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
317 SetForegroundColour(GetParent()->GetForegroundColour());
324 void wxListCtrl::UpdateStyle()
328 // The new window view style
330 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
331 dwStyleNew
|= m_baseStyle
;
333 // Get the current window style.
334 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
336 // Only set the window style if the view bits have changed.
337 if ( dwStyleOld
!= dwStyleNew
)
339 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
344 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
348 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
350 delete (wxListItemAttr
*)node
->Data();
356 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
359 m_hasAnyAttr
= FALSE
;
363 wxListCtrl::~wxListCtrl()
365 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
369 m_textCtrl
->SetHWND(0);
370 m_textCtrl
->UnsubclassWin();
375 if (m_ownsImageListNormal
) delete m_imageListNormal
;
376 if (m_ownsImageListSmall
) delete m_imageListSmall
;
377 if (m_ownsImageListState
) delete m_imageListState
;
380 // ----------------------------------------------------------------------------
381 // set/get/change style
382 // ----------------------------------------------------------------------------
384 // Add or remove a single window style
385 void wxListCtrl::SetSingleStyle(long style
, bool add
)
387 long flag
= GetWindowStyleFlag();
389 // Get rid of conflicting styles
392 if ( style
& wxLC_MASK_TYPE
)
393 flag
= flag
& ~wxLC_MASK_TYPE
;
394 if ( style
& wxLC_MASK_ALIGN
)
395 flag
= flag
& ~wxLC_MASK_ALIGN
;
396 if ( style
& wxLC_MASK_SORT
)
397 flag
= flag
& ~wxLC_MASK_SORT
;
413 m_windowStyle
= flag
;
418 // Set the whole window style
419 void wxListCtrl::SetWindowStyleFlag(long flag
)
421 m_windowStyle
= flag
;
426 // Can be just a single style, or a bitlist
427 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
430 if ( style
& wxLC_ICON
)
432 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
433 oldStyle
-= LVS_SMALLICON
;
434 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
435 oldStyle
-= LVS_REPORT
;
436 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
437 oldStyle
-= LVS_LIST
;
441 if ( style
& wxLC_SMALL_ICON
)
443 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
444 oldStyle
-= LVS_ICON
;
445 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
446 oldStyle
-= LVS_REPORT
;
447 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
448 oldStyle
-= LVS_LIST
;
449 wstyle
|= LVS_SMALLICON
;
452 if ( style
& wxLC_LIST
)
454 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
455 oldStyle
-= LVS_ICON
;
456 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
457 oldStyle
-= LVS_REPORT
;
458 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
459 oldStyle
-= LVS_SMALLICON
;
463 if ( style
& wxLC_REPORT
)
465 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
466 oldStyle
-= LVS_ICON
;
467 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
468 oldStyle
-= LVS_LIST
;
469 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
470 oldStyle
-= LVS_SMALLICON
;
472 wstyle
|= LVS_REPORT
;
475 if ( style
& wxLC_ALIGN_LEFT
)
477 if ( oldStyle
& LVS_ALIGNTOP
)
478 oldStyle
-= LVS_ALIGNTOP
;
479 wstyle
|= LVS_ALIGNLEFT
;
482 if ( style
& wxLC_ALIGN_TOP
)
484 if ( oldStyle
& LVS_ALIGNLEFT
)
485 oldStyle
-= LVS_ALIGNLEFT
;
486 wstyle
|= LVS_ALIGNTOP
;
489 if ( style
& wxLC_AUTOARRANGE
)
490 wstyle
|= LVS_AUTOARRANGE
;
492 if ( style
& wxLC_NO_SORT_HEADER
)
493 wstyle
|= LVS_NOSORTHEADER
;
495 if ( style
& wxLC_NO_HEADER
)
496 wstyle
|= LVS_NOCOLUMNHEADER
;
498 if ( style
& wxLC_EDIT_LABELS
)
499 wstyle
|= LVS_EDITLABELS
;
501 if ( style
& wxLC_SINGLE_SEL
)
502 wstyle
|= LVS_SINGLESEL
;
504 if ( style
& wxLC_SORT_ASCENDING
)
506 if ( oldStyle
& LVS_SORTDESCENDING
)
507 oldStyle
-= LVS_SORTDESCENDING
;
508 wstyle
|= LVS_SORTASCENDING
;
511 if ( style
& wxLC_SORT_DESCENDING
)
513 if ( oldStyle
& LVS_SORTASCENDING
)
514 oldStyle
-= LVS_SORTASCENDING
;
515 wstyle
|= LVS_SORTDESCENDING
;
518 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
519 if ( style
& wxLC_VIRTUAL
)
521 int ver
= wxTheApp
->GetComCtl32Version();
524 wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."),
525 ver
/ 100, ver
% 100);
528 wstyle
|= LVS_OWNERDATA
;
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 // Sets the foreground, i.e. text, colour
540 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
542 if ( !wxWindow::SetForegroundColour(col
) )
545 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
550 // Sets the background colour
551 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
553 if ( !wxWindow::SetBackgroundColour(col
) )
556 // we set the same colour for both the "empty" background and the items
558 COLORREF color
= wxColourToRGB(col
);
559 ListView_SetBkColor(GetHwnd(), color
);
560 ListView_SetTextBkColor(GetHwnd(), color
);
565 // Gets information about this column
566 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
571 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
573 lvCol
.mask
|= LVCF_TEXT
;
574 lvCol
.pszText
= new wxChar
[513];
575 lvCol
.cchTextMax
= 512;
578 bool success
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0;
580 // item.m_subItem = lvCol.iSubItem;
581 item
.m_width
= lvCol
.cx
;
583 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
585 item
.m_text
= lvCol
.pszText
;
586 delete[] lvCol
.pszText
;
589 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
591 if (lvCol
.fmt
== LVCFMT_LEFT
)
592 item
.m_format
= wxLIST_FORMAT_LEFT
;
593 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
594 item
.m_format
= wxLIST_FORMAT_RIGHT
;
595 else if (lvCol
.fmt
== LVCFMT_CENTER
)
596 item
.m_format
= wxLIST_FORMAT_CENTRE
;
602 // Sets information about this column
603 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
606 wxConvertToMSWListCol(col
, item
, lvCol
);
608 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
611 // Gets the column width
612 int wxListCtrl::GetColumnWidth(int col
) const
614 return ListView_GetColumnWidth(GetHwnd(), col
);
617 // Sets the column width
618 bool wxListCtrl::SetColumnWidth(int col
, int width
)
621 if ( m_windowStyle
& wxLC_LIST
)
625 if ( width2
== wxLIST_AUTOSIZE
)
626 width2
= LVSCW_AUTOSIZE
;
627 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
628 width2
= LVSCW_AUTOSIZE_USEHEADER
;
630 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
633 // Gets the number of items that can fit vertically in the
634 // visible area of the list control (list or report view)
635 // or the total number of items in the list control (icon
636 // or small icon view)
637 int wxListCtrl::GetCountPerPage() const
639 return ListView_GetCountPerPage(GetHwnd());
642 // Gets the edit control for editing labels.
643 wxTextCtrl
* wxListCtrl::GetEditControl() const
648 // Gets information about the item
649 bool wxListCtrl::GetItem(wxListItem
& info
) const
652 wxZeroMemory(lvItem
);
654 lvItem
.iItem
= info
.m_itemId
;
655 lvItem
.iSubItem
= info
.m_col
;
657 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
659 lvItem
.mask
|= LVIF_TEXT
;
660 lvItem
.pszText
= new wxChar
[513];
661 lvItem
.cchTextMax
= 512;
665 lvItem
.pszText
= NULL
;
668 if (info
.m_mask
& wxLIST_MASK_DATA
)
669 lvItem
.mask
|= LVIF_PARAM
;
671 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
672 lvItem
.mask
|= LVIF_IMAGE
;
674 if ( info
.m_mask
& wxLIST_MASK_STATE
)
676 lvItem
.mask
|= LVIF_STATE
;
677 // the other bits are hardly interesting anyhow
678 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
681 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
684 wxLogError(_("Couldn't retrieve information about list control item %d."),
689 // give NULL as hwnd as we already have everything we need
690 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
694 delete[] lvItem
.pszText
;
699 // Sets information about the item
700 bool wxListCtrl::SetItem(wxListItem
& info
)
703 wxConvertToMSWListItem(this, info
, item
);
706 if ( !ListView_SetItem(GetHwnd(), &item
) )
708 wxLogDebug(_T("ListView_SetItem() failed"));
713 // we need to update the item immediately to show the new image
714 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
716 // check whether it has any custom attributes
717 if ( info
.HasAttributes() )
719 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
722 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
724 *attr
= *info
.GetAttributes();
728 // if the colour has changed, we must redraw the item
734 // we need this to make the change visible right now
735 ListView_Update(GetHwnd(), item
.iItem
);
741 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
745 info
.m_mask
= wxLIST_MASK_TEXT
;
746 info
.m_itemId
= index
;
750 info
.m_image
= imageId
;
751 info
.m_mask
|= wxLIST_MASK_IMAGE
;
753 return SetItem(info
);
757 // Gets the item state
758 int wxListCtrl::GetItemState(long item
, long stateMask
) const
762 info
.m_mask
= wxLIST_MASK_STATE
;
763 info
.m_stateMask
= stateMask
;
764 info
.m_itemId
= item
;
772 // Sets the item state
773 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
775 // NB: don't use SetItem() here as it doesn't work with the virtual list
778 wxZeroMemory(lvItem
);
780 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
782 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
783 (WPARAM
)item
, (LPARAM
)&lvItem
) )
785 wxLogLastError(_T("ListView_SetItemState"));
793 // Sets the item image
794 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
798 info
.m_mask
= wxLIST_MASK_IMAGE
;
799 info
.m_image
= image
;
800 info
.m_itemId
= item
;
802 return SetItem(info
);
805 // Gets the item text
806 wxString
wxListCtrl::GetItemText(long item
) const
810 info
.m_mask
= wxLIST_MASK_TEXT
;
811 info
.m_itemId
= item
;
818 // Sets the item text
819 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
823 info
.m_mask
= wxLIST_MASK_TEXT
;
824 info
.m_itemId
= item
;
830 // Gets the item data
831 long wxListCtrl::GetItemData(long item
) const
835 info
.m_mask
= wxLIST_MASK_DATA
;
836 info
.m_itemId
= item
;
843 // Sets the item data
844 bool wxListCtrl::SetItemData(long item
, long data
)
848 info
.m_mask
= wxLIST_MASK_DATA
;
849 info
.m_itemId
= item
;
852 return SetItem(info
);
855 // Gets the item rectangle
856 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
860 int code2
= LVIR_BOUNDS
;
861 if ( code
== wxLIST_RECT_BOUNDS
)
863 else if ( code
== wxLIST_RECT_ICON
)
865 else if ( code
== wxLIST_RECT_LABEL
)
869 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
871 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
876 rect
.width
= rect2
.right
- rect2
.left
;
877 rect
.height
= rect2
.bottom
- rect2
.top
;
881 // Gets the item position
882 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
886 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
888 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
892 // Sets the item position.
893 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
895 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
898 // Gets the number of items in the list control
899 int wxListCtrl::GetItemCount() const
901 return ListView_GetItemCount(GetHwnd());
904 // Retrieves the spacing between icons in pixels.
905 // If small is TRUE, gets the spacing for the small icon
906 // view, otherwise the large icon view.
907 int wxListCtrl::GetItemSpacing(bool isSmall
) const
909 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
912 // Gets the number of selected items in the list control
913 int wxListCtrl::GetSelectedItemCount() const
915 return ListView_GetSelectedCount(GetHwnd());
918 // Gets the text colour of the listview
919 wxColour
wxListCtrl::GetTextColour() const
921 COLORREF ref
= ListView_GetTextColor(GetHwnd());
922 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
926 // Sets the text colour of the listview
927 void wxListCtrl::SetTextColour(const wxColour
& col
)
929 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
932 // Gets the index of the topmost visible item when in
933 // list or report view
934 long wxListCtrl::GetTopItem() const
936 return (long) ListView_GetTopIndex(GetHwnd());
939 // Searches for an item, starting from 'item'.
940 // 'geometry' is one of
941 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
942 // 'state' is a state bit flag, one or more of
943 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
944 // item can be -1 to find the first item that matches the
946 // Returns the item or -1 if unsuccessful.
947 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
951 if ( geom
== wxLIST_NEXT_ABOVE
)
953 if ( geom
== wxLIST_NEXT_ALL
)
955 if ( geom
== wxLIST_NEXT_BELOW
)
957 if ( geom
== wxLIST_NEXT_LEFT
)
958 flags
|= LVNI_TOLEFT
;
959 if ( geom
== wxLIST_NEXT_RIGHT
)
960 flags
|= LVNI_TORIGHT
;
962 if ( state
& wxLIST_STATE_CUT
)
964 if ( state
& wxLIST_STATE_DROPHILITED
)
965 flags
|= LVNI_DROPHILITED
;
966 if ( state
& wxLIST_STATE_FOCUSED
)
967 flags
|= LVNI_FOCUSED
;
968 if ( state
& wxLIST_STATE_SELECTED
)
969 flags
|= LVNI_SELECTED
;
971 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
975 wxImageList
*wxListCtrl::GetImageList(int which
) const
977 if ( which
== wxIMAGE_LIST_NORMAL
)
979 return m_imageListNormal
;
981 else if ( which
== wxIMAGE_LIST_SMALL
)
983 return m_imageListSmall
;
985 else if ( which
== wxIMAGE_LIST_STATE
)
987 return m_imageListState
;
992 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
995 if ( which
== wxIMAGE_LIST_NORMAL
)
997 flags
= LVSIL_NORMAL
;
998 if (m_ownsImageListNormal
) delete m_imageListNormal
;
999 m_imageListNormal
= imageList
;
1000 m_ownsImageListNormal
= FALSE
;
1002 else if ( which
== wxIMAGE_LIST_SMALL
)
1004 flags
= LVSIL_SMALL
;
1005 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1006 m_imageListSmall
= imageList
;
1007 m_ownsImageListSmall
= FALSE
;
1009 else if ( which
== wxIMAGE_LIST_STATE
)
1011 flags
= LVSIL_STATE
;
1012 if (m_ownsImageListState
) delete m_imageListState
;
1013 m_imageListState
= imageList
;
1014 m_ownsImageListState
= FALSE
;
1016 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1019 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1021 SetImageList(imageList
, which
);
1022 if ( which
== wxIMAGE_LIST_NORMAL
)
1023 m_ownsImageListNormal
= TRUE
;
1024 else if ( which
== wxIMAGE_LIST_SMALL
)
1025 m_ownsImageListSmall
= TRUE
;
1026 else if ( which
== wxIMAGE_LIST_STATE
)
1027 m_ownsImageListState
= TRUE
;
1030 // ----------------------------------------------------------------------------
1032 // ----------------------------------------------------------------------------
1034 // Arranges the items
1035 bool wxListCtrl::Arrange(int flag
)
1038 if ( flag
== wxLIST_ALIGN_LEFT
)
1039 code
= LVA_ALIGNLEFT
;
1040 else if ( flag
== wxLIST_ALIGN_TOP
)
1041 code
= LVA_ALIGNTOP
;
1042 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1044 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1045 code
= LVA_SNAPTOGRID
;
1047 return (ListView_Arrange(GetHwnd(), code
) != 0);
1051 bool wxListCtrl::DeleteItem(long item
)
1053 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1055 wxLogLastError(_T("ListView_DeleteItem"));
1059 // the virtual list control doesn't refresh itself correctly, help it
1062 // we need to refresh all the lines below the one which was deleted
1064 if ( item
> 0 && GetItemCount() )
1066 GetItemRect(item
- 1, rectItem
);
1071 rectItem
.height
= 0;
1074 wxRect rectWin
= GetRect();
1075 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1076 rectWin
.y
= rectItem
.GetBottom();
1078 RefreshRect(rectWin
);
1084 // Deletes all items
1085 bool wxListCtrl::DeleteAllItems()
1087 return ListView_DeleteAllItems(GetHwnd()) != 0;
1090 // Deletes all items
1091 bool wxListCtrl::DeleteAllColumns()
1093 while ( m_colCount
> 0 )
1095 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1097 wxLogLastError(wxT("ListView_DeleteColumn"));
1105 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1111 bool wxListCtrl::DeleteColumn(int col
)
1113 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1115 if ( success
&& (m_colCount
> 0) )
1120 // Clears items, and columns if there are any.
1121 void wxListCtrl::ClearAll()
1124 if ( m_colCount
> 0 )
1128 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1130 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1132 // VS: ListView_EditLabel requires that the list has focus.
1134 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1138 m_textCtrl
->SetHWND(0);
1139 m_textCtrl
->UnsubclassWin();
1144 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1145 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1146 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1151 // End label editing, optionally cancelling the edit
1152 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1154 wxFAIL_MSG( _T("not implemented") );
1159 // Ensures this item is visible
1160 bool wxListCtrl::EnsureVisible(long item
)
1162 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1165 // Find an item whose label matches this string, starting from the item after 'start'
1166 // or the beginning if 'start' is -1.
1167 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1169 LV_FINDINFO findInfo
;
1171 findInfo
.flags
= LVFI_STRING
;
1173 findInfo
.flags
|= LVFI_PARTIAL
;
1176 // ListView_FindItem() excludes the first item from search and to look
1177 // through all the items you need to start from -1 which is unnatural and
1178 // inconsistent with the generic version - so we adjust the index
1181 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1184 // Find an item whose data matches this data, starting from the item after 'start'
1185 // or the beginning if 'start' is -1.
1186 long wxListCtrl::FindItem(long start
, long data
)
1188 LV_FINDINFO findInfo
;
1190 findInfo
.flags
= LVFI_PARAM
;
1191 findInfo
.lParam
= data
;
1193 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1196 // Find an item nearest this position in the specified direction, starting from
1197 // the item after 'start' or the beginning if 'start' is -1.
1198 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1200 LV_FINDINFO findInfo
;
1202 findInfo
.flags
= LVFI_NEARESTXY
;
1203 findInfo
.pt
.x
= pt
.x
;
1204 findInfo
.pt
.y
= pt
.y
;
1205 findInfo
.vkDirection
= VK_RIGHT
;
1207 if ( direction
== wxLIST_FIND_UP
)
1208 findInfo
.vkDirection
= VK_UP
;
1209 else if ( direction
== wxLIST_FIND_DOWN
)
1210 findInfo
.vkDirection
= VK_DOWN
;
1211 else if ( direction
== wxLIST_FIND_LEFT
)
1212 findInfo
.vkDirection
= VK_LEFT
;
1213 else if ( direction
== wxLIST_FIND_RIGHT
)
1214 findInfo
.vkDirection
= VK_RIGHT
;
1216 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1219 // Determines which item (if any) is at the specified point,
1220 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1221 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1223 LV_HITTESTINFO hitTestInfo
;
1224 hitTestInfo
.pt
.x
= (int) point
.x
;
1225 hitTestInfo
.pt
.y
= (int) point
.y
;
1227 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1230 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1231 flags
|= wxLIST_HITTEST_ABOVE
;
1232 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1233 flags
|= wxLIST_HITTEST_BELOW
;
1234 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1235 flags
|= wxLIST_HITTEST_NOWHERE
;
1236 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1237 flags
|= wxLIST_HITTEST_ONITEMICON
;
1238 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1239 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1240 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1241 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1242 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1243 flags
|= wxLIST_HITTEST_TOLEFT
;
1244 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1245 flags
|= wxLIST_HITTEST_TORIGHT
;
1247 return (long) hitTestInfo
.iItem
;
1250 // Inserts an item, returning the index of the new item if successful,
1252 long wxListCtrl::InsertItem(wxListItem
& info
)
1254 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1257 wxConvertToMSWListItem(this, info
, item
);
1259 // check whether it has any custom attributes
1260 if ( info
.HasAttributes() )
1263 wxListItemAttr
*attr
;
1264 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1268 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1270 else *attr
= *info
.GetAttributes();
1272 m_hasAnyAttr
= TRUE
;
1275 return (long) ListView_InsertItem(GetHwnd(), & item
);
1278 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1281 info
.m_text
= label
;
1282 info
.m_mask
= wxLIST_MASK_TEXT
;
1283 info
.m_itemId
= index
;
1284 return InsertItem(info
);
1287 // Inserts an image item
1288 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1291 info
.m_image
= imageIndex
;
1292 info
.m_mask
= wxLIST_MASK_IMAGE
;
1293 info
.m_itemId
= index
;
1294 return InsertItem(info
);
1297 // Inserts an image/string item
1298 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1301 info
.m_image
= imageIndex
;
1302 info
.m_text
= label
;
1303 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1304 info
.m_itemId
= index
;
1305 return InsertItem(info
);
1308 // For list view mode (only), inserts a column.
1309 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1312 wxConvertToMSWListCol(col
, item
, lvCol
);
1314 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1316 // always give some width to the new column: this one is compatible
1317 // with the generic version
1318 lvCol
.mask
|= LVCF_WIDTH
;
1322 // when we insert a column which can contain an image, we must specify this
1323 // flag right now as doing it later in SetColumn() has no effect
1325 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1326 // way to dynamically set/clear the bitmap as the column without a bitmap
1327 // on the left looks ugly (there is a hole)
1329 // unfortunately with my version of comctl32.dll (5.80), the left column
1330 // image is always on the left and it seems that it's a "feature" - I
1331 // didn't find any way to work around it in any case
1332 if ( lvCol
.mask
& LVCF_IMAGE
)
1334 lvCol
.mask
|= LVCF_FMT
;
1335 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1338 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1345 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1352 long wxListCtrl::InsertColumn(long col
,
1353 const wxString
& heading
,
1358 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1359 item
.m_text
= heading
;
1362 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1363 item
.m_width
= width
;
1365 item
.m_format
= format
;
1367 return InsertColumn(col
, item
);
1370 // Scrolls the list control. If in icon, small icon or report view mode,
1371 // x specifies the number of pixels to scroll. If in list view mode, x
1372 // specifies the number of columns to scroll.
1373 // If in icon, small icon or list view mode, y specifies the number of pixels
1374 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1375 bool wxListCtrl::ScrollList(int dx
, int dy
)
1377 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1382 // fn is a function which takes 3 long arguments: item1, item2, data.
1383 // item1 is the long data associated with a first item (NOT the index).
1384 // item2 is the long data associated with a second item (NOT the index).
1385 // data is the same value as passed to SortItems.
1386 // The return value is a negative number if the first item should precede the second
1387 // item, a positive number of the second item should precede the first,
1388 // or zero if the two items are equivalent.
1390 // data is arbitrary data to be passed to the sort function.
1392 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1393 // time for anything better right now (VZ)
1394 static long gs_sortData
= 0;
1395 static wxListCtrl
*gs_sortCtrl
= NULL
;
1396 static wxListCtrlCompare gs_sortFunction
= NULL
;
1398 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1400 int n1
= *(const int *)arg1
,
1401 n2
= *(const int *)arg2
;
1403 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1404 gs_sortCtrl
->GetItemData(n2
),
1408 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1410 // sort the attributes too
1414 count
= GetItemCount();
1415 int *aItems
= new int[count
];
1416 for ( n
= 0; n
< count
; n
++ )
1423 gs_sortFunction
= fn
;
1425 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1429 gs_sortFunction
= NULL
;
1431 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1432 for ( n
= 0; n
< count
; n
++ )
1434 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1437 attrsNew
.Put(n
, attr
);
1447 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1449 wxLogDebug(_T("ListView_SortItems() failed"));
1459 // ----------------------------------------------------------------------------
1460 // message processing
1461 // ----------------------------------------------------------------------------
1463 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1465 if (cmd
== EN_UPDATE
)
1467 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1468 event
.SetEventObject( this );
1469 ProcessCommand(event
);
1472 else if (cmd
== EN_KILLFOCUS
)
1474 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1475 event
.SetEventObject( this );
1476 ProcessCommand(event
);
1483 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1485 // prepare the event
1486 // -----------------
1488 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1489 wxEventType eventType
= wxEVT_NULL
;
1491 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1493 // check for messages from the header (in report view)
1494 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1496 // is it a message from the header?
1497 if ( nmhdr
->hwndFrom
== hwndHdr
)
1499 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1500 event
.m_itemIndex
= -1;
1502 switch ( nmhdr
->code
)
1504 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1505 // TRACK messages even to ANSI programs: on my system I get
1506 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1508 // work around is to simply catch both versions and hope that it
1509 // works (why should this message exist in ANSI and Unicode is
1510 // beyond me as it doesn't deal with strings at all...)
1511 case HDN_BEGINTRACKA
:
1512 case HDN_BEGINTRACKW
:
1513 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1518 if ( eventType
== wxEVT_NULL
)
1519 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1524 if ( eventType
== wxEVT_NULL
)
1525 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1526 event
.m_col
= nmHDR
->iItem
;
1531 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1534 // find the column clicked: we have to search for it
1535 // ourselves as the notification message doesn't provide
1538 // where did the click occur?
1540 if ( !::GetCursorPos(&ptClick
) )
1542 wxLogLastError(_T("GetCursorPos"));
1545 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1547 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1550 event
.m_pointDrag
.x
= ptClick
.x
;
1551 event
.m_pointDrag
.y
= ptClick
.y
;
1553 int colCount
= Header_GetItemCount(hwndHdr
);
1556 for ( int col
= 0; col
< colCount
; col
++ )
1558 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1560 if ( ::PtInRect(&rect
, ptClick
) )
1571 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1574 else if ( nmhdr
->hwndFrom
== GetHwnd() )
1576 // almost all messages use NM_LISTVIEW
1577 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1579 // this is true for almost all events
1580 event
.m_item
.m_data
= nmLV
->lParam
;
1582 switch ( nmhdr
->code
)
1584 case LVN_BEGINRDRAG
:
1585 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1589 if ( eventType
== wxEVT_NULL
)
1591 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1594 event
.m_itemIndex
= nmLV
->iItem
;
1595 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1596 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1599 case LVN_BEGINLABELEDIT
:
1601 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1602 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1603 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1604 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1608 case LVN_COLUMNCLICK
:
1609 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1610 event
.m_itemIndex
= -1;
1611 event
.m_col
= nmLV
->iSubItem
;
1614 case LVN_DELETEALLITEMS
:
1615 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1616 event
.m_itemIndex
= -1;
1622 case LVN_DELETEITEM
:
1623 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1624 event
.m_itemIndex
= nmLV
->iItem
;
1628 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1632 case LVN_ENDLABELEDIT
:
1634 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1635 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1636 wxConvertFromMSWListItem(NULL
, event
.m_item
, info
->item
);
1637 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1640 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1644 case LVN_SETDISPINFO
:
1646 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1647 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1648 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1652 case LVN_INSERTITEM
:
1653 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1654 event
.m_itemIndex
= nmLV
->iItem
;
1657 case LVN_ITEMCHANGED
:
1658 // This needs to be sent to wxListCtrl as a rather more concrete
1659 // event. For now, just detect a selection or deselection.
1660 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1662 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1663 event
.m_itemIndex
= nmLV
->iItem
;
1665 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1667 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1668 event
.m_itemIndex
= nmLV
->iItem
;
1678 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1679 WORD wVKey
= info
->wVKey
;
1681 // get the current selection
1682 long lItem
= GetNextItem(-1,
1684 wxLIST_STATE_SELECTED
);
1686 // <Enter> or <Space> activate the selected item if any (but
1687 // not with Shift and/or Ctrl as then they have a predefined
1688 // meaning for the list view)
1690 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1691 !(wxIsShiftDown() || wxIsCtrlDown()) )
1693 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1697 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1699 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1700 // value which should be used as is
1701 int code
= wxCharCodeMSWToWX(wVKey
);
1702 event
.m_code
= code
? code
: wVKey
;
1706 event
.m_item
.m_itemId
= lItem
;
1710 // fill the other fields too
1711 event
.m_item
.m_text
= GetItemText(lItem
);
1712 event
.m_item
.m_data
= GetItemData(lItem
);
1718 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1720 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1725 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1726 // if it happened on an item (and not on empty place)
1727 if ( nmLV
->iItem
== -1 )
1733 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1734 event
.m_itemIndex
= nmLV
->iItem
;
1735 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1736 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1740 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1741 // don't do anything else
1742 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1747 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1748 LV_HITTESTINFO lvhti
;
1749 wxZeroMemory(lvhti
);
1751 ::GetCursorPos(&(lvhti
.pt
));
1752 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1753 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1755 if ( lvhti
.flags
& LVHT_ONITEM
)
1757 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1758 event
.m_itemIndex
= lvhti
.iItem
;
1759 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1760 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1765 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1766 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1768 *result
= OnCustomDraw(lParam
);
1771 #endif // _WIN32_IE >= 0x300
1773 case LVN_ODCACHEHINT
:
1775 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1777 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1779 // we get some really stupid cache hints like ones for items in
1780 // range 0..0 for an empty control or, after deleting an item,
1781 // for items in invalid range - filter this garbage out
1782 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1784 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1786 long iMax
= GetItemCount();
1787 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1797 case LVN_GETDISPINFO
:
1800 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1802 LV_ITEM
& lvi
= info
->item
;
1803 long item
= lvi
.iItem
;
1805 if ( lvi
.mask
& LVIF_TEXT
)
1807 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1808 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1811 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1812 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
1813 if ( lvi
.mask
& LVIF_IMAGE
)
1815 lvi
.iImage
= OnGetItemImage(item
);
1819 // a little dose of healthy paranoia: as we never use
1820 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1821 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1822 _T("we don't support state callbacks yet!") );
1829 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1834 // where did this one come from?
1838 // process the event
1839 // -----------------
1841 event
.SetEventObject( this );
1842 event
.SetEventType(eventType
);
1844 if ( !GetEventHandler()->ProcessEvent(event
) )
1850 switch ( nmhdr
->code
)
1852 case LVN_DELETEALLITEMS
:
1853 // always return TRUE to suppress all additional LVN_DELETEITEM
1854 // notifications - this makes deleting all items from a list ctrl
1860 case LVN_ENDLABELEDIT
:
1861 // logic here is inversed compared to all the other messages
1862 *result
= event
.IsAllowed();
1867 *result
= !event
.IsAllowed();
1872 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1874 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1876 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1877 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1878 switch ( nmcd
.dwDrawStage
)
1881 // if we've got any items with non standard attributes,
1882 // notify us before painting each item
1884 // for virtual controls, always suppose that we have attributes as
1885 // there is no way to check for this
1886 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1889 case CDDS_ITEMPREPAINT
:
1891 size_t item
= (size_t)nmcd
.dwItemSpec
;
1892 if ( item
>= (size_t)GetItemCount() )
1894 // we get this message with item == 0 for an empty control,
1895 // we must ignore it as calling OnGetItemAttr() would be
1897 return CDRF_DODEFAULT
;
1900 wxListItemAttr
*attr
=
1901 IsVirtual() ? OnGetItemAttr(item
)
1902 : (wxListItemAttr
*)m_attrs
.Get(item
);
1906 // nothing to do for this item
1907 return CDRF_DODEFAULT
;
1911 wxColour colText
, colBack
;
1912 if ( attr
->HasFont() )
1914 wxFont font
= attr
->GetFont();
1915 hFont
= (HFONT
)font
.GetResourceHandle();
1922 if ( attr
->HasTextColour() )
1924 colText
= attr
->GetTextColour();
1928 colText
= GetTextColour();
1931 if ( attr
->HasBackgroundColour() )
1933 colBack
= attr
->GetBackgroundColour();
1937 colBack
= GetBackgroundColour();
1940 lplvcd
->clrText
= wxColourToRGB(colText
);
1941 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1943 // note that if we wanted to set colours for
1944 // individual columns (subitems), we would have
1945 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1948 ::SelectObject(nmcd
.hdc
, hFont
);
1950 return CDRF_NEWFONT
;
1953 // fall through to return CDRF_DODEFAULT
1956 return CDRF_DODEFAULT
;
1960 #endif // NM_CUSTOMDRAW supported
1962 // Necessary for drawing hrules and vrules, if specified
1963 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1967 wxControl::OnPaint(event
);
1969 // Reset the device origin since it may have been set
1970 dc
.SetDeviceOrigin(0, 0);
1972 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1973 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1975 if (!drawHRules
&& !drawVRules
)
1977 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1980 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1982 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1984 wxSize clientSize
= GetClientSize();
1988 int itemCount
= GetItemCount();
1992 long top
= GetTopItem();
1993 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
1995 if (GetItemRect(i
, itemRect
))
1997 cy
= itemRect
.GetTop();
1998 if (i
!= 0) // Don't draw the first one
2000 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2003 if (i
== itemCount
- 1)
2005 cy
= itemRect
.GetBottom();
2006 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2012 if (drawVRules
&& (i
> -1))
2014 wxRect firstItemRect
;
2015 GetItemRect(0, firstItemRect
);
2017 if (GetItemRect(i
, itemRect
))
2020 int x
= itemRect
.GetX();
2021 for (col
= 0; col
< GetColumnCount(); col
++)
2023 int colWidth
= GetColumnWidth(col
);
2025 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
2031 // ----------------------------------------------------------------------------
2032 // virtual list controls
2033 // ----------------------------------------------------------------------------
2035 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2037 // this is a pure virtual function, in fact - which is not really pure
2038 // because the controls which are not virtual don't need to implement it
2039 wxFAIL_MSG( _T("not supposed to be called") );
2041 return wxEmptyString
;
2044 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2047 wxFAIL_MSG( _T("not supposed to be called") );
2052 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2054 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2055 _T("invalid item index in OnGetItemAttr()") );
2057 // no attributes by default
2061 void wxListCtrl::SetItemCount(long count
)
2063 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2065 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2067 wxLogLastError(_T("ListView_SetItemCount"));
2071 void wxListCtrl::RefreshItem(long item
)
2073 if ( !ListView_Update(GetHwnd(), item
) )
2075 wxLogLastError(_T("ListView_Update"));
2079 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2081 wxRect rect1
, rect2
;
2082 GetItemRect(itemFrom
, rect1
);
2083 GetItemRect(itemTo
, rect2
);
2085 wxRect rect
= rect1
;
2086 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2091 // ----------------------------------------------------------------------------
2093 // ----------------------------------------------------------------------------
2095 // List item structure
2096 wxListItem::wxListItem()
2106 m_format
= wxLIST_FORMAT_CENTRE
;
2112 void wxListItem::Clear()
2121 m_format
= wxLIST_FORMAT_CENTRE
;
2123 m_text
= wxEmptyString
;
2125 if (m_attr
) delete m_attr
;
2129 void wxListItem::ClearAttributes()
2131 if (m_attr
) delete m_attr
;
2135 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2139 info
.m_data
= lvItem
.lParam
;
2142 info
.m_stateMask
= 0;
2143 info
.m_itemId
= lvItem
.iItem
;
2145 long oldMask
= lvItem
.mask
;
2147 bool needText
= FALSE
;
2148 if (hwndListCtrl
!= 0)
2150 if ( lvItem
.mask
& LVIF_TEXT
)
2157 lvItem
.pszText
= new wxChar
[513];
2158 lvItem
.cchTextMax
= 512;
2160 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2161 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2164 if ( lvItem
.mask
& LVIF_STATE
)
2166 info
.m_mask
|= wxLIST_MASK_STATE
;
2168 if ( lvItem
.stateMask
& LVIS_CUT
)
2170 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2171 if ( lvItem
.state
& LVIS_CUT
)
2172 info
.m_state
|= wxLIST_STATE_CUT
;
2174 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2176 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2177 if ( lvItem
.state
& LVIS_DROPHILITED
)
2178 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2180 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2182 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2183 if ( lvItem
.state
& LVIS_FOCUSED
)
2184 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2186 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2188 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2189 if ( lvItem
.state
& LVIS_SELECTED
)
2190 info
.m_state
|= wxLIST_STATE_SELECTED
;
2194 if ( lvItem
.mask
& LVIF_TEXT
)
2196 info
.m_mask
|= wxLIST_MASK_TEXT
;
2197 info
.m_text
= lvItem
.pszText
;
2199 if ( lvItem
.mask
& LVIF_IMAGE
)
2201 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2202 info
.m_image
= lvItem
.iImage
;
2204 if ( lvItem
.mask
& LVIF_PARAM
)
2205 info
.m_mask
|= wxLIST_MASK_DATA
;
2206 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2207 info
.m_mask
|= wxLIST_SET_ITEM
;
2208 info
.m_col
= lvItem
.iSubItem
;
2213 delete[] lvItem
.pszText
;
2215 lvItem
.mask
= oldMask
;
2218 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2220 if (stateMask
& wxLIST_STATE_CUT
)
2222 lvItem
.stateMask
|= LVIS_CUT
;
2223 if (state
& wxLIST_STATE_CUT
)
2224 lvItem
.state
|= LVIS_CUT
;
2226 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2228 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2229 if (state
& wxLIST_STATE_DROPHILITED
)
2230 lvItem
.state
|= LVIS_DROPHILITED
;
2232 if (stateMask
& wxLIST_STATE_FOCUSED
)
2234 lvItem
.stateMask
|= LVIS_FOCUSED
;
2235 if (state
& wxLIST_STATE_FOCUSED
)
2236 lvItem
.state
|= LVIS_FOCUSED
;
2238 if (stateMask
& wxLIST_STATE_SELECTED
)
2240 lvItem
.stateMask
|= LVIS_SELECTED
;
2241 if (state
& wxLIST_STATE_SELECTED
)
2242 lvItem
.state
|= LVIS_SELECTED
;
2246 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2247 const wxListItem
& info
,
2250 lvItem
.iItem
= (int) info
.m_itemId
;
2252 lvItem
.iImage
= info
.m_image
;
2253 lvItem
.lParam
= info
.m_data
;
2254 lvItem
.stateMask
= 0;
2257 lvItem
.iSubItem
= info
.m_col
;
2259 if (info
.m_mask
& wxLIST_MASK_STATE
)
2261 lvItem
.mask
|= LVIF_STATE
;
2263 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2266 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2268 lvItem
.mask
|= LVIF_TEXT
;
2269 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2271 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2275 // pszText is not const, hence the cast
2276 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2277 if ( lvItem
.pszText
)
2278 lvItem
.cchTextMax
= info
.m_text
.Length();
2280 lvItem
.cchTextMax
= 0;
2283 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2284 lvItem
.mask
|= LVIF_IMAGE
;
2285 if (info
.m_mask
& wxLIST_MASK_DATA
)
2286 lvItem
.mask
|= LVIF_PARAM
;
2289 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
2292 wxZeroMemory(lvCol
);
2294 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2296 lvCol
.mask
|= LVCF_TEXT
;
2297 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2300 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2302 lvCol
.mask
|= LVCF_FMT
;
2304 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2305 lvCol
.fmt
= LVCFMT_LEFT
;
2306 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2307 lvCol
.fmt
= LVCFMT_RIGHT
;
2308 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2309 lvCol
.fmt
= LVCFMT_CENTER
;
2312 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2314 lvCol
.mask
|= LVCF_WIDTH
;
2315 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2316 lvCol
.cx
= LVSCW_AUTOSIZE
;
2317 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2318 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2320 lvCol
.cx
= item
.m_width
;
2323 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2324 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2325 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2327 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2329 lvCol
.mask
|= LVCF_IMAGE
;
2330 lvCol
.iImage
= item
.m_image
;
2332 //else: it doesn't support item images anyhow
2337 // ----------------------------------------------------------------------------
2339 // ----------------------------------------------------------------------------
2341 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2343 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2344 : wxNotifyEvent(commandType
, id
)
2350 m_cancelled
= FALSE
;
2353 #endif // wxUSE_LISTCTRL