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 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // convert our state and mask flags to LV_ITEM constants
92 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
94 // convert wxListItem to LV_ITEM
95 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
96 const wxListItem
& info
, LV_ITEM
& lvItem
);
98 // convert LV_ITEM to wxListItem
99 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
101 /* const */ LV_ITEM
& lvItem
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
112 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
113 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
114 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
115 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
116 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
117 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
118 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
119 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
120 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
121 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
122 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
123 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
125 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
126 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
127 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
129 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
130 EVT_PAINT(wxListCtrl::OnPaint
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 void wxListEvent::CopyObject(wxObject
& object_dest
) const
143 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
145 wxNotifyEvent::CopyObject(object_dest
);
147 obj
->m_code
= m_code
;
148 obj
->m_itemIndex
= m_itemIndex
;
149 obj
->m_oldItemIndex
= m_oldItemIndex
;
151 obj
->m_cancelled
= m_cancelled
;
152 obj
->m_pointDrag
= m_pointDrag
;
153 obj
->m_item
.m_mask
= m_item
.m_mask
;
154 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
155 obj
->m_item
.m_col
= m_item
.m_col
;
156 obj
->m_item
.m_state
= m_item
.m_state
;
157 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
158 obj
->m_item
.m_text
= m_item
.m_text
;
159 obj
->m_item
.m_image
= m_item
.m_image
;
160 obj
->m_item
.m_data
= m_item
.m_data
;
161 obj
->m_item
.m_format
= m_item
.m_format
;
162 obj
->m_item
.m_width
= m_item
.m_width
;
164 if ( m_item
.HasAttributes() )
166 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
167 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
168 obj
->m_item
.SetFont(m_item
.GetFont());
172 // ----------------------------------------------------------------------------
173 // wxListCtrl construction
174 // ----------------------------------------------------------------------------
176 void wxListCtrl::Init()
178 m_imageListNormal
= NULL
;
179 m_imageListSmall
= NULL
;
180 m_imageListState
= NULL
;
181 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
185 m_hasAnyAttr
= FALSE
;
188 bool wxListCtrl::Create(wxWindow
*parent
,
193 const wxValidator
& validator
,
194 const wxString
& name
)
197 SetValidator(validator
);
198 #endif // wxUSE_VALIDATORS
207 m_windowStyle
= style
;
220 m_windowId
= (id
== -1) ? NewControlId() : id
;
222 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
223 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
225 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
226 wstyle
|= WS_CLIPSIBLINGS
;
228 if ( wxStyleHasBorder(m_windowStyle
) )
230 m_baseStyle
= wstyle
;
232 if ( !DoCreateControl(x
, y
, width
, height
) )
236 parent
->AddChild(this);
241 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
243 DWORD wstyle
= m_baseStyle
;
246 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
248 // Even with extended styles, need to combine with WS_BORDER
249 // for them to look right.
253 long oldStyle
= 0; // Dummy
254 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
256 // Create the ListView control.
257 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
262 GetWinHwnd(GetParent()),
269 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
274 // for comctl32.dll v 4.70+ we want to have this attribute because it's
275 // prettier (and also because wxGTK does it like this)
276 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
278 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
279 0, LVS_EX_FULLROWSELECT
);
282 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
283 SetForegroundColour(GetParent()->GetForegroundColour());
290 void wxListCtrl::UpdateStyle()
294 // The new window view style
296 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
297 dwStyleNew
|= m_baseStyle
;
299 // Get the current window style.
300 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
302 // Only set the window style if the view bits have changed.
303 if ( dwStyleOld
!= dwStyleNew
)
305 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
310 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
314 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
316 delete (wxListItemAttr
*)node
->Data();
322 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
325 m_hasAnyAttr
= FALSE
;
329 wxListCtrl::~wxListCtrl()
331 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
335 m_textCtrl
->SetHWND(0);
336 m_textCtrl
->UnsubclassWin();
341 if (m_ownsImageListNormal
) delete m_imageListNormal
;
342 if (m_ownsImageListSmall
) delete m_imageListSmall
;
343 if (m_ownsImageListState
) delete m_imageListState
;
346 // ----------------------------------------------------------------------------
347 // set/get/change style
348 // ----------------------------------------------------------------------------
350 // Add or remove a single window style
351 void wxListCtrl::SetSingleStyle(long style
, bool add
)
353 long flag
= GetWindowStyleFlag();
355 // Get rid of conflicting styles
358 if ( style
& wxLC_MASK_TYPE
)
359 flag
= flag
& ~wxLC_MASK_TYPE
;
360 if ( style
& wxLC_MASK_ALIGN
)
361 flag
= flag
& ~wxLC_MASK_ALIGN
;
362 if ( style
& wxLC_MASK_SORT
)
363 flag
= flag
& ~wxLC_MASK_SORT
;
379 m_windowStyle
= flag
;
384 // Set the whole window style
385 void wxListCtrl::SetWindowStyleFlag(long flag
)
387 m_windowStyle
= flag
;
392 // Can be just a single style, or a bitlist
393 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
396 if ( style
& wxLC_ICON
)
398 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
399 oldStyle
-= LVS_SMALLICON
;
400 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
401 oldStyle
-= LVS_REPORT
;
402 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
403 oldStyle
-= LVS_LIST
;
407 if ( style
& wxLC_SMALL_ICON
)
409 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
410 oldStyle
-= LVS_ICON
;
411 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
412 oldStyle
-= LVS_REPORT
;
413 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
414 oldStyle
-= LVS_LIST
;
415 wstyle
|= LVS_SMALLICON
;
418 if ( style
& wxLC_LIST
)
420 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
421 oldStyle
-= LVS_ICON
;
422 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
423 oldStyle
-= LVS_REPORT
;
424 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
425 oldStyle
-= LVS_SMALLICON
;
429 if ( style
& wxLC_REPORT
)
431 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
432 oldStyle
-= LVS_ICON
;
433 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
434 oldStyle
-= LVS_LIST
;
435 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
436 oldStyle
-= LVS_SMALLICON
;
438 wstyle
|= LVS_REPORT
;
441 if ( style
& wxLC_ALIGN_LEFT
)
443 if ( oldStyle
& LVS_ALIGNTOP
)
444 oldStyle
-= LVS_ALIGNTOP
;
445 wstyle
|= LVS_ALIGNLEFT
;
448 if ( style
& wxLC_ALIGN_TOP
)
450 if ( oldStyle
& LVS_ALIGNLEFT
)
451 oldStyle
-= LVS_ALIGNLEFT
;
452 wstyle
|= LVS_ALIGNTOP
;
455 if ( style
& wxLC_AUTOARRANGE
)
456 wstyle
|= LVS_AUTOARRANGE
;
458 if ( style
& wxLC_NO_SORT_HEADER
)
459 wstyle
|= LVS_NOSORTHEADER
;
461 if ( style
& wxLC_NO_HEADER
)
462 wstyle
|= LVS_NOCOLUMNHEADER
;
464 if ( style
& wxLC_EDIT_LABELS
)
465 wstyle
|= LVS_EDITLABELS
;
467 if ( style
& wxLC_SINGLE_SEL
)
468 wstyle
|= LVS_SINGLESEL
;
470 if ( style
& wxLC_SORT_ASCENDING
)
472 if ( oldStyle
& LVS_SORTDESCENDING
)
473 oldStyle
-= LVS_SORTDESCENDING
;
474 wstyle
|= LVS_SORTASCENDING
;
477 if ( style
& wxLC_SORT_DESCENDING
)
479 if ( oldStyle
& LVS_SORTASCENDING
)
480 oldStyle
-= LVS_SORTASCENDING
;
481 wstyle
|= LVS_SORTDESCENDING
;
484 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
485 if ( style
& wxLC_VIRTUAL
)
487 int ver
= wxTheApp
->GetComCtl32Version();
490 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."),
491 ver
/ 100, ver
% 100);
494 wstyle
|= LVS_OWNERDATA
;
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 // Sets the foreground, i.e. text, colour
506 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
508 if ( !wxWindow::SetForegroundColour(col
) )
511 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
516 // Sets the background colour
517 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
519 if ( !wxWindow::SetBackgroundColour(col
) )
522 // we set the same colour for both the "empty" background and the items
524 COLORREF color
= wxColourToRGB(col
);
525 ListView_SetBkColor(GetHwnd(), color
);
526 ListView_SetTextBkColor(GetHwnd(), color
);
531 // Gets information about this column
532 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
537 lvCol
.pszText
= NULL
;
539 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
541 lvCol
.mask
|= LVCF_TEXT
;
542 lvCol
.pszText
= new wxChar
[513];
543 lvCol
.cchTextMax
= 512;
546 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
548 // item.m_subItem = lvCol.iSubItem;
549 item
.m_width
= lvCol
.cx
;
551 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
553 item
.m_text
= lvCol
.pszText
;
554 delete[] lvCol
.pszText
;
557 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
559 if (lvCol
.fmt
== LVCFMT_LEFT
)
560 item
.m_format
= wxLIST_FORMAT_LEFT
;
561 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
562 item
.m_format
= wxLIST_FORMAT_RIGHT
;
563 else if (lvCol
.fmt
== LVCFMT_CENTER
)
564 item
.m_format
= wxLIST_FORMAT_CENTRE
;
570 // Sets information about this column
571 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
576 lvCol
.pszText
= NULL
;
578 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
580 lvCol
.mask
|= LVCF_TEXT
;
581 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
582 lvCol
.cchTextMax
= 0; // Ignored
584 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
586 lvCol
.mask
|= LVCF_FMT
;
588 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
589 lvCol
.fmt
= LVCFMT_LEFT
;
590 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
591 lvCol
.fmt
= LVCFMT_RIGHT
;
592 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
593 lvCol
.fmt
= LVCFMT_CENTER
;
596 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
598 lvCol
.mask
|= LVCF_WIDTH
;
599 lvCol
.cx
= item
.m_width
;
601 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
602 lvCol
.cx
= LVSCW_AUTOSIZE
;
603 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
604 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
606 lvCol
.mask
|= LVCF_SUBITEM
;
607 lvCol
.iSubItem
= col
;
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 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1056 // Deletes all items
1057 bool wxListCtrl::DeleteAllItems()
1059 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1062 // Deletes all items
1063 bool wxListCtrl::DeleteAllColumns()
1065 while ( m_colCount
> 0 )
1067 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1069 wxLogLastError(wxT("ListView_DeleteColumn"));
1077 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1083 bool wxListCtrl::DeleteColumn(int col
)
1085 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1087 if ( success
&& (m_colCount
> 0) )
1092 // Clears items, and columns if there are any.
1093 void wxListCtrl::ClearAll()
1096 if ( m_colCount
> 0 )
1100 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1102 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1104 // VS: ListView_EditLabel requires that the list has focus.
1106 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1110 m_textCtrl
->SetHWND(0);
1111 m_textCtrl
->UnsubclassWin();
1116 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1117 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1118 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1123 // End label editing, optionally cancelling the edit
1124 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1126 wxFAIL_MSG( _T("not implemented") );
1131 // Ensures this item is visible
1132 bool wxListCtrl::EnsureVisible(long item
)
1134 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1137 // Find an item whose label matches this string, starting from the item after 'start'
1138 // or the beginning if 'start' is -1.
1139 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1141 LV_FINDINFO findInfo
;
1143 findInfo
.flags
= LVFI_STRING
;
1145 findInfo
.flags
|= LVFI_PARTIAL
;
1148 // ListView_FindItem() excludes the first item from search and to look
1149 // through all the items you need to start from -1 which is unnatural and
1150 // inconsistent with the generic version - so we adjust the index
1153 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1156 // Find an item whose data matches this data, starting from the item after 'start'
1157 // or the beginning if 'start' is -1.
1158 long wxListCtrl::FindItem(long start
, long data
)
1160 LV_FINDINFO findInfo
;
1162 findInfo
.flags
= LVFI_PARAM
;
1163 findInfo
.lParam
= data
;
1165 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1168 // Find an item nearest this position in the specified direction, starting from
1169 // the item after 'start' or the beginning if 'start' is -1.
1170 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1172 LV_FINDINFO findInfo
;
1174 findInfo
.flags
= LVFI_NEARESTXY
;
1175 findInfo
.pt
.x
= pt
.x
;
1176 findInfo
.pt
.y
= pt
.y
;
1177 findInfo
.vkDirection
= VK_RIGHT
;
1179 if ( direction
== wxLIST_FIND_UP
)
1180 findInfo
.vkDirection
= VK_UP
;
1181 else if ( direction
== wxLIST_FIND_DOWN
)
1182 findInfo
.vkDirection
= VK_DOWN
;
1183 else if ( direction
== wxLIST_FIND_LEFT
)
1184 findInfo
.vkDirection
= VK_LEFT
;
1185 else if ( direction
== wxLIST_FIND_RIGHT
)
1186 findInfo
.vkDirection
= VK_RIGHT
;
1188 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1191 // Determines which item (if any) is at the specified point,
1192 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1193 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1195 LV_HITTESTINFO hitTestInfo
;
1196 hitTestInfo
.pt
.x
= (int) point
.x
;
1197 hitTestInfo
.pt
.y
= (int) point
.y
;
1199 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1202 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1203 flags
|= wxLIST_HITTEST_ABOVE
;
1204 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1205 flags
|= wxLIST_HITTEST_BELOW
;
1206 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1207 flags
|= wxLIST_HITTEST_NOWHERE
;
1208 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1209 flags
|= wxLIST_HITTEST_ONITEMICON
;
1210 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1211 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1212 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1213 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1214 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1215 flags
|= wxLIST_HITTEST_TOLEFT
;
1216 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1217 flags
|= wxLIST_HITTEST_TORIGHT
;
1219 return (long) hitTestInfo
.iItem
;
1222 // Inserts an item, returning the index of the new item if successful,
1224 long wxListCtrl::InsertItem(wxListItem
& info
)
1226 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1229 wxConvertToMSWListItem(this, info
, item
);
1231 // check whether it has any custom attributes
1232 if ( info
.HasAttributes() )
1235 wxListItemAttr
*attr
;
1236 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1240 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1242 else *attr
= *info
.GetAttributes();
1244 m_hasAnyAttr
= TRUE
;
1247 return (long) ListView_InsertItem(GetHwnd(), & item
);
1250 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1253 info
.m_text
= label
;
1254 info
.m_mask
= wxLIST_MASK_TEXT
;
1255 info
.m_itemId
= index
;
1256 return InsertItem(info
);
1259 // Inserts an image item
1260 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1263 info
.m_image
= imageIndex
;
1264 info
.m_mask
= wxLIST_MASK_IMAGE
;
1265 info
.m_itemId
= index
;
1266 return InsertItem(info
);
1269 // Inserts an image/string item
1270 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1273 info
.m_image
= imageIndex
;
1274 info
.m_text
= label
;
1275 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1276 info
.m_itemId
= index
;
1277 return InsertItem(info
);
1280 // For list view mode (only), inserts a column.
1281 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1286 lvCol
.pszText
= NULL
;
1288 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1290 lvCol
.mask
|= LVCF_TEXT
;
1291 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1292 lvCol
.cchTextMax
= 0; // Ignored
1294 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1296 lvCol
.mask
|= LVCF_FMT
;
1298 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1299 lvCol
.fmt
= LVCFMT_LEFT
;
1300 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1301 lvCol
.fmt
= LVCFMT_RIGHT
;
1302 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1303 lvCol
.fmt
= LVCFMT_CENTER
;
1306 lvCol
.mask
|= LVCF_WIDTH
;
1307 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1309 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1310 lvCol
.cx
= LVSCW_AUTOSIZE
;
1311 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1312 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1314 lvCol
.cx
= item
.m_width
;
1318 // always give some width to the new column: this one is compatible
1323 lvCol
.mask
|= LVCF_SUBITEM
;
1324 lvCol
.iSubItem
= col
;
1326 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1333 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1340 long wxListCtrl::InsertColumn(long col
,
1341 const wxString
& heading
,
1346 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1347 item
.m_text
= heading
;
1350 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1351 item
.m_width
= width
;
1353 item
.m_format
= format
;
1355 return InsertColumn(col
, item
);
1358 // Scrolls the list control. If in icon, small icon or report view mode,
1359 // x specifies the number of pixels to scroll. If in list view mode, x
1360 // specifies the number of columns to scroll.
1361 // If in icon, small icon or list view mode, y specifies the number of pixels
1362 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1363 bool wxListCtrl::ScrollList(int dx
, int dy
)
1365 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1370 // fn is a function which takes 3 long arguments: item1, item2, data.
1371 // item1 is the long data associated with a first item (NOT the index).
1372 // item2 is the long data associated with a second item (NOT the index).
1373 // data is the same value as passed to SortItems.
1374 // The return value is a negative number if the first item should precede the second
1375 // item, a positive number of the second item should precede the first,
1376 // or zero if the two items are equivalent.
1378 // data is arbitrary data to be passed to the sort function.
1380 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1381 // time for anything better right now (VZ)
1382 static long gs_sortData
= 0;
1383 static wxListCtrl
*gs_sortCtrl
= NULL
;
1384 static wxListCtrlCompare gs_sortFunction
= NULL
;
1386 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1388 int n1
= *(const int *)arg1
,
1389 n2
= *(const int *)arg2
;
1391 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1392 gs_sortCtrl
->GetItemData(n2
),
1396 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1398 // sort the attributes too
1402 count
= GetItemCount();
1403 int *aItems
= new int[count
];
1404 for ( n
= 0; n
< count
; n
++ )
1411 gs_sortFunction
= fn
;
1413 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1417 gs_sortFunction
= NULL
;
1419 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1420 for ( n
= 0; n
< count
; n
++ )
1422 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1425 attrsNew
.Put(n
, attr
);
1435 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1437 wxLogDebug(_T("ListView_SortItems() failed"));
1447 // ----------------------------------------------------------------------------
1448 // message processing
1449 // ----------------------------------------------------------------------------
1451 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1453 if (cmd
== EN_UPDATE
)
1455 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1456 event
.SetEventObject( this );
1457 ProcessCommand(event
);
1460 else if (cmd
== EN_KILLFOCUS
)
1462 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1463 event
.SetEventObject( this );
1464 ProcessCommand(event
);
1471 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1473 // prepare the event
1474 // -----------------
1476 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1477 wxEventType eventType
= wxEVT_NULL
;
1479 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1481 // almost all messages use NM_LISTVIEW
1482 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1484 // this is true for almost all events
1485 event
.m_item
.m_data
= nmLV
->lParam
;
1487 switch ( nmhdr
->code
)
1489 case LVN_BEGINRDRAG
:
1490 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1494 if ( eventType
== wxEVT_NULL
)
1496 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1499 event
.m_itemIndex
= nmLV
->iItem
;
1500 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1501 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1504 case LVN_BEGINLABELEDIT
:
1506 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1507 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1508 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1509 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1513 case LVN_COLUMNCLICK
:
1514 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1515 event
.m_itemIndex
= -1;
1516 event
.m_col
= nmLV
->iSubItem
;
1519 case LVN_DELETEALLITEMS
:
1520 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1521 event
.m_itemIndex
= -1;
1527 case LVN_DELETEITEM
:
1528 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1529 event
.m_itemIndex
= nmLV
->iItem
;
1533 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1537 case LVN_ENDLABELEDIT
:
1539 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1540 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1541 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1542 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1545 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1549 case LVN_SETDISPINFO
:
1551 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1552 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1553 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1557 case LVN_INSERTITEM
:
1558 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1559 event
.m_itemIndex
= nmLV
->iItem
;
1562 case LVN_ITEMCHANGED
:
1563 // This needs to be sent to wxListCtrl as a rather more concrete
1564 // event. For now, just detect a selection or deselection.
1565 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1567 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1568 event
.m_itemIndex
= nmLV
->iItem
;
1570 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1572 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1573 event
.m_itemIndex
= nmLV
->iItem
;
1583 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1584 WORD wVKey
= info
->wVKey
;
1586 // get the current selection
1587 long lItem
= GetNextItem(-1,
1589 wxLIST_STATE_SELECTED
);
1591 // <Enter> or <Space> activate the selected item if any (but
1592 // not with Shift and/or Ctrl as then they have a predefined
1593 // meaning for the list view)
1595 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1596 !(wxIsShiftDown() || wxIsCtrlDown()) )
1598 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1602 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1603 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1607 event
.m_item
.m_itemId
= lItem
;
1611 // fill the other fields too
1612 event
.m_item
.m_text
= GetItemText(lItem
);
1613 event
.m_item
.m_data
= GetItemData(lItem
);
1619 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1621 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1626 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1627 // if it happened on an item (and not on empty place)
1628 if ( nmLV
->iItem
== -1 )
1634 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1635 event
.m_itemIndex
= nmLV
->iItem
;
1636 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1637 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1641 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1642 subclass and check for the actual WM_RBUTTONDOWN message,
1643 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1644 before firing off. We want to have notify events for both down
1647 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1648 // don't do anything else
1649 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1654 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1655 LV_HITTESTINFO lvhti
;
1656 wxZeroMemory(lvhti
);
1658 ::GetCursorPos(&(lvhti
.pt
));
1659 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1660 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1662 if ( lvhti
.flags
& LVHT_ONITEM
)
1664 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1665 event
.m_itemIndex
= lvhti
.iItem
;
1666 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1667 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1674 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1676 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1678 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1683 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1684 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1685 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1686 event
.m_itemIndex
= hdr
->iItem
;
1691 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1692 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1694 *result
= OnCustomDraw(lParam
);
1697 #endif // _WIN32_IE >= 0x300
1699 case LVN_ODCACHEHINT
:
1701 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1703 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1705 // we get some really stupid cache hints like ones for items in
1706 // range 0..0 for an empty control or, after deleting an item,
1707 // for items in invalid range - filter this garbage out
1708 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1710 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1712 long iMax
= GetItemCount();
1713 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1723 case LVN_GETDISPINFO
:
1726 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1728 LV_ITEM
& lvi
= info
->item
;
1729 long item
= lvi
.iItem
;
1731 if ( lvi
.mask
& LVIF_TEXT
)
1733 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1734 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1737 if ( lvi
.mask
& LVIF_IMAGE
)
1739 lvi
.iImage
= OnGetItemImage(item
);
1742 // a little dose of healthy paranoia: as we never use
1743 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1744 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1745 _T("we don't support state callbacks yet!") );
1752 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1755 // process the event
1756 // -----------------
1758 event
.SetEventObject( this );
1759 event
.SetEventType(eventType
);
1761 if ( !GetEventHandler()->ProcessEvent(event
) )
1767 switch ( nmhdr
->code
)
1769 case LVN_DELETEALLITEMS
:
1770 // always return TRUE to suppress all additional LVN_DELETEITEM
1771 // notifications - this makes deleting all items from a list ctrl
1777 case LVN_ENDLABELEDIT
:
1778 // logic here is inversed compared to all the other messages
1779 *result
= event
.IsAllowed();
1784 *result
= !event
.IsAllowed();
1789 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1791 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1793 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1794 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1795 switch ( nmcd
.dwDrawStage
)
1798 // if we've got any items with non standard attributes,
1799 // notify us before painting each item
1801 // for virtual controls, always suppose that we have attributes as
1802 // there is no way to check for this
1803 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1806 case CDDS_ITEMPREPAINT
:
1808 size_t item
= (size_t)nmcd
.dwItemSpec
;
1809 if ( item
>= (size_t)GetItemCount() )
1811 // we get this message with item == 0 for an empty control,
1812 // we must ignore it as calling OnGetItemAttr() would be
1814 return CDRF_DODEFAULT
;
1817 wxListItemAttr
*attr
=
1818 IsVirtual() ? OnGetItemAttr(item
)
1819 : (wxListItemAttr
*)m_attrs
.Get(item
);
1823 // nothing to do for this item
1824 return CDRF_DODEFAULT
;
1828 wxColour colText
, colBack
;
1829 if ( attr
->HasFont() )
1831 wxFont font
= attr
->GetFont();
1832 hFont
= (HFONT
)font
.GetResourceHandle();
1839 if ( attr
->HasTextColour() )
1841 colText
= attr
->GetTextColour();
1845 colText
= GetTextColour();
1848 if ( attr
->HasBackgroundColour() )
1850 colBack
= attr
->GetBackgroundColour();
1854 colBack
= GetBackgroundColour();
1857 lplvcd
->clrText
= wxColourToRGB(colText
);
1858 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1860 // note that if we wanted to set colours for
1861 // individual columns (subitems), we would have
1862 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1865 ::SelectObject(nmcd
.hdc
, hFont
);
1867 return CDRF_NEWFONT
;
1870 // fall through to return CDRF_DODEFAULT
1873 return CDRF_DODEFAULT
;
1877 #endif // NM_CUSTOMDRAW supported
1879 // Necessary for drawing hrules and vrules, if specified
1880 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1884 wxControl::OnPaint(event
);
1886 // Reset the device origin since it may have been set
1887 dc
.SetDeviceOrigin(0, 0);
1889 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1890 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1892 if (!drawHRules
&& !drawVRules
)
1894 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1897 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1899 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1901 wxSize clientSize
= GetClientSize();
1905 int itemCount
= GetItemCount();
1909 for (i
= 0; i
< itemCount
; i
++)
1911 if (GetItemRect(i
, itemRect
))
1913 cy
= itemRect
.GetTop();
1914 if (i
!= 0) // Don't draw the first one
1916 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1919 if (i
== (GetItemCount() - 1))
1921 cy
= itemRect
.GetBottom();
1922 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1927 i
= (GetItemCount() - 1);
1928 if (drawVRules
&& (i
> -1))
1930 wxRect firstItemRect
;
1931 GetItemRect(0, firstItemRect
);
1933 if (GetItemRect(i
, itemRect
))
1936 int x
= itemRect
.GetX();
1937 for (col
= 0; col
< GetColumnCount(); col
++)
1939 int colWidth
= GetColumnWidth(col
);
1941 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1947 // ----------------------------------------------------------------------------
1948 // virtual list controls
1949 // ----------------------------------------------------------------------------
1951 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
1953 // this is a pure virtual function, in fact - which is not really pure
1954 // because the controls which are not virtual don't need to implement it
1955 wxFAIL_MSG( _T("not supposed to be called") );
1957 return wxEmptyString
;
1960 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
1963 wxFAIL_MSG( _T("not supposed to be called") );
1968 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
1970 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
1971 _T("invalid item index in OnGetItemAttr()") );
1973 // no attributes by default
1977 void wxListCtrl::SetItemCount(long count
)
1979 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1981 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
1983 wxLogLastError(_T("ListView_SetItemCount"));
1987 void wxListCtrl::RefreshItem(long item
)
1989 if ( !ListView_Update(GetHwnd(), item
) )
1991 wxLogLastError(_T("ListView_Update"));
1995 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
1997 wxRect rect1
, rect2
;
1998 GetItemRect(itemFrom
, rect1
);
1999 GetItemRect(itemTo
, rect2
);
2001 wxRect rect
= rect1
;
2002 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2007 // ----------------------------------------------------------------------------
2009 // ----------------------------------------------------------------------------
2011 // List item structure
2012 wxListItem::wxListItem()
2022 m_format
= wxLIST_FORMAT_CENTRE
;
2028 void wxListItem::Clear()
2037 m_format
= wxLIST_FORMAT_CENTRE
;
2039 m_text
= wxEmptyString
;
2041 if (m_attr
) delete m_attr
;
2045 void wxListItem::ClearAttributes()
2047 if (m_attr
) delete m_attr
;
2051 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2055 info
.m_data
= lvItem
.lParam
;
2058 info
.m_stateMask
= 0;
2059 info
.m_itemId
= lvItem
.iItem
;
2061 long oldMask
= lvItem
.mask
;
2063 bool needText
= FALSE
;
2064 if (hwndListCtrl
!= 0)
2066 if ( lvItem
.mask
& LVIF_TEXT
)
2073 lvItem
.pszText
= new wxChar
[513];
2074 lvItem
.cchTextMax
= 512;
2076 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2077 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2080 if ( lvItem
.mask
& LVIF_STATE
)
2082 info
.m_mask
|= wxLIST_MASK_STATE
;
2084 if ( lvItem
.stateMask
& LVIS_CUT
)
2086 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2087 if ( lvItem
.state
& LVIS_CUT
)
2088 info
.m_state
|= wxLIST_STATE_CUT
;
2090 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2092 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2093 if ( lvItem
.state
& LVIS_DROPHILITED
)
2094 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2096 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2098 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2099 if ( lvItem
.state
& LVIS_FOCUSED
)
2100 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2102 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2104 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2105 if ( lvItem
.state
& LVIS_SELECTED
)
2106 info
.m_state
|= wxLIST_STATE_SELECTED
;
2110 if ( lvItem
.mask
& LVIF_TEXT
)
2112 info
.m_mask
|= wxLIST_MASK_TEXT
;
2113 info
.m_text
= lvItem
.pszText
;
2115 if ( lvItem
.mask
& LVIF_IMAGE
)
2117 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2118 info
.m_image
= lvItem
.iImage
;
2120 if ( lvItem
.mask
& LVIF_PARAM
)
2121 info
.m_mask
|= wxLIST_MASK_DATA
;
2122 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2123 info
.m_mask
|= wxLIST_SET_ITEM
;
2124 info
.m_col
= lvItem
.iSubItem
;
2129 delete[] lvItem
.pszText
;
2131 lvItem
.mask
= oldMask
;
2134 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2136 if (stateMask
& wxLIST_STATE_CUT
)
2138 lvItem
.stateMask
|= LVIS_CUT
;
2139 if (state
& wxLIST_STATE_CUT
)
2140 lvItem
.state
|= LVIS_CUT
;
2142 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2144 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2145 if (state
& wxLIST_STATE_DROPHILITED
)
2146 lvItem
.state
|= LVIS_DROPHILITED
;
2148 if (stateMask
& wxLIST_STATE_FOCUSED
)
2150 lvItem
.stateMask
|= LVIS_FOCUSED
;
2151 if (state
& wxLIST_STATE_FOCUSED
)
2152 lvItem
.state
|= LVIS_FOCUSED
;
2154 if (stateMask
& wxLIST_STATE_SELECTED
)
2156 lvItem
.stateMask
|= LVIS_SELECTED
;
2157 if (state
& wxLIST_STATE_SELECTED
)
2158 lvItem
.state
|= LVIS_SELECTED
;
2162 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2163 const wxListItem
& info
,
2166 lvItem
.iItem
= (int) info
.m_itemId
;
2168 lvItem
.iImage
= info
.m_image
;
2169 lvItem
.lParam
= info
.m_data
;
2170 lvItem
.stateMask
= 0;
2173 lvItem
.iSubItem
= info
.m_col
;
2175 if (info
.m_mask
& wxLIST_MASK_STATE
)
2177 lvItem
.mask
|= LVIF_STATE
;
2179 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2182 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2184 lvItem
.mask
|= LVIF_TEXT
;
2185 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2187 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2191 // pszText is not const, hence the cast
2192 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2193 if ( lvItem
.pszText
)
2194 lvItem
.cchTextMax
= info
.m_text
.Length();
2196 lvItem
.cchTextMax
= 0;
2199 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2200 lvItem
.mask
|= LVIF_IMAGE
;
2201 if (info
.m_mask
& wxLIST_MASK_DATA
)
2202 lvItem
.mask
|= LVIF_PARAM
;
2205 // ----------------------------------------------------------------------------
2207 // ----------------------------------------------------------------------------
2209 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2211 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2212 : wxNotifyEvent(commandType
, id
)
2218 m_cancelled
= FALSE
;
2221 #endif // wxUSE_LISTCTRL