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)
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // convert our state and mask flags to LV_ITEM constants
109 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
111 // convert wxListItem to LV_ITEM
112 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
113 const wxListItem
& info
, LV_ITEM
& lvItem
);
115 // convert LV_ITEM to wxListItem
116 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
118 /* const */ LV_ITEM
& lvItem
);
120 // convert our wxListItem to LV_COLUMN
121 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
129 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
130 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
131 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
132 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
133 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
134 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
135 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
136 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
137 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
138 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
139 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
140 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
141 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
142 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
143 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
144 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
145 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
146 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
147 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
148 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
150 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
151 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
152 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
154 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
155 EVT_PAINT(wxListCtrl::OnPaint
)
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 void wxListEvent::CopyObject(wxObject
& object_dest
) const
168 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
170 wxNotifyEvent::CopyObject(object_dest
);
172 obj
->m_code
= m_code
;
173 obj
->m_itemIndex
= m_itemIndex
;
174 obj
->m_oldItemIndex
= m_oldItemIndex
;
176 obj
->m_cancelled
= m_cancelled
;
177 obj
->m_pointDrag
= m_pointDrag
;
178 obj
->m_item
.m_mask
= m_item
.m_mask
;
179 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
180 obj
->m_item
.m_col
= m_item
.m_col
;
181 obj
->m_item
.m_state
= m_item
.m_state
;
182 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
183 obj
->m_item
.m_text
= m_item
.m_text
;
184 obj
->m_item
.m_image
= m_item
.m_image
;
185 obj
->m_item
.m_data
= m_item
.m_data
;
186 obj
->m_item
.m_format
= m_item
.m_format
;
187 obj
->m_item
.m_width
= m_item
.m_width
;
189 if ( m_item
.HasAttributes() )
191 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
192 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
193 obj
->m_item
.SetFont(m_item
.GetFont());
197 // ----------------------------------------------------------------------------
198 // wxListCtrl construction
199 // ----------------------------------------------------------------------------
201 void wxListCtrl::Init()
203 m_imageListNormal
= NULL
;
204 m_imageListSmall
= NULL
;
205 m_imageListState
= NULL
;
206 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
210 m_hasAnyAttr
= FALSE
;
213 bool wxListCtrl::Create(wxWindow
*parent
,
218 const wxValidator
& validator
,
219 const wxString
& name
)
222 SetValidator(validator
);
223 #endif // wxUSE_VALIDATORS
232 m_windowStyle
= style
;
245 m_windowId
= (id
== -1) ? NewControlId() : id
;
247 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
248 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
250 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
251 wstyle
|= WS_CLIPSIBLINGS
;
253 if ( wxStyleHasBorder(m_windowStyle
) )
255 m_baseStyle
= wstyle
;
257 if ( !DoCreateControl(x
, y
, width
, height
) )
261 parent
->AddChild(this);
266 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
268 DWORD wstyle
= m_baseStyle
;
271 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
273 // Even with extended styles, need to combine with WS_BORDER
274 // for them to look right.
278 long oldStyle
= 0; // Dummy
279 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
281 // Create the ListView control.
282 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
287 GetWinHwnd(GetParent()),
294 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
299 // for comctl32.dll v 4.70+ we want to have this attribute because it's
300 // prettier (and also because wxGTK does it like this)
301 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
303 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
304 0, LVS_EX_FULLROWSELECT
);
307 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
308 SetForegroundColour(GetParent()->GetForegroundColour());
315 void wxListCtrl::UpdateStyle()
319 // The new window view style
321 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
322 dwStyleNew
|= m_baseStyle
;
324 // Get the current window style.
325 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
327 // Only set the window style if the view bits have changed.
328 if ( dwStyleOld
!= dwStyleNew
)
330 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
335 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
339 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
341 delete (wxListItemAttr
*)node
->Data();
347 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
350 m_hasAnyAttr
= FALSE
;
354 wxListCtrl::~wxListCtrl()
356 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
360 m_textCtrl
->SetHWND(0);
361 m_textCtrl
->UnsubclassWin();
366 if (m_ownsImageListNormal
) delete m_imageListNormal
;
367 if (m_ownsImageListSmall
) delete m_imageListSmall
;
368 if (m_ownsImageListState
) delete m_imageListState
;
371 // ----------------------------------------------------------------------------
372 // set/get/change style
373 // ----------------------------------------------------------------------------
375 // Add or remove a single window style
376 void wxListCtrl::SetSingleStyle(long style
, bool add
)
378 long flag
= GetWindowStyleFlag();
380 // Get rid of conflicting styles
383 if ( style
& wxLC_MASK_TYPE
)
384 flag
= flag
& ~wxLC_MASK_TYPE
;
385 if ( style
& wxLC_MASK_ALIGN
)
386 flag
= flag
& ~wxLC_MASK_ALIGN
;
387 if ( style
& wxLC_MASK_SORT
)
388 flag
= flag
& ~wxLC_MASK_SORT
;
404 m_windowStyle
= flag
;
409 // Set the whole window style
410 void wxListCtrl::SetWindowStyleFlag(long flag
)
412 m_windowStyle
= flag
;
417 // Can be just a single style, or a bitlist
418 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
421 if ( style
& wxLC_ICON
)
423 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
424 oldStyle
-= LVS_SMALLICON
;
425 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
426 oldStyle
-= LVS_REPORT
;
427 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
428 oldStyle
-= LVS_LIST
;
432 if ( style
& wxLC_SMALL_ICON
)
434 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
435 oldStyle
-= LVS_ICON
;
436 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
437 oldStyle
-= LVS_REPORT
;
438 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
439 oldStyle
-= LVS_LIST
;
440 wstyle
|= LVS_SMALLICON
;
443 if ( style
& wxLC_LIST
)
445 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
446 oldStyle
-= LVS_ICON
;
447 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
448 oldStyle
-= LVS_REPORT
;
449 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
450 oldStyle
-= LVS_SMALLICON
;
454 if ( style
& wxLC_REPORT
)
456 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
457 oldStyle
-= LVS_ICON
;
458 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
459 oldStyle
-= LVS_LIST
;
460 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
461 oldStyle
-= LVS_SMALLICON
;
463 wstyle
|= LVS_REPORT
;
466 if ( style
& wxLC_ALIGN_LEFT
)
468 if ( oldStyle
& LVS_ALIGNTOP
)
469 oldStyle
-= LVS_ALIGNTOP
;
470 wstyle
|= LVS_ALIGNLEFT
;
473 if ( style
& wxLC_ALIGN_TOP
)
475 if ( oldStyle
& LVS_ALIGNLEFT
)
476 oldStyle
-= LVS_ALIGNLEFT
;
477 wstyle
|= LVS_ALIGNTOP
;
480 if ( style
& wxLC_AUTOARRANGE
)
481 wstyle
|= LVS_AUTOARRANGE
;
483 if ( style
& wxLC_NO_SORT_HEADER
)
484 wstyle
|= LVS_NOSORTHEADER
;
486 if ( style
& wxLC_NO_HEADER
)
487 wstyle
|= LVS_NOCOLUMNHEADER
;
489 if ( style
& wxLC_EDIT_LABELS
)
490 wstyle
|= LVS_EDITLABELS
;
492 if ( style
& wxLC_SINGLE_SEL
)
493 wstyle
|= LVS_SINGLESEL
;
495 if ( style
& wxLC_SORT_ASCENDING
)
497 if ( oldStyle
& LVS_SORTDESCENDING
)
498 oldStyle
-= LVS_SORTDESCENDING
;
499 wstyle
|= LVS_SORTASCENDING
;
502 if ( style
& wxLC_SORT_DESCENDING
)
504 if ( oldStyle
& LVS_SORTASCENDING
)
505 oldStyle
-= LVS_SORTASCENDING
;
506 wstyle
|= LVS_SORTDESCENDING
;
509 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
510 if ( style
& wxLC_VIRTUAL
)
512 int ver
= wxTheApp
->GetComCtl32Version();
515 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."),
516 ver
/ 100, ver
% 100);
519 wstyle
|= LVS_OWNERDATA
;
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 // Sets the foreground, i.e. text, colour
531 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
533 if ( !wxWindow::SetForegroundColour(col
) )
536 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
541 // Sets the background colour
542 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
544 if ( !wxWindow::SetBackgroundColour(col
) )
547 // we set the same colour for both the "empty" background and the items
549 COLORREF color
= wxColourToRGB(col
);
550 ListView_SetBkColor(GetHwnd(), color
);
551 ListView_SetTextBkColor(GetHwnd(), color
);
556 // Gets information about this column
557 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
562 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
564 lvCol
.mask
|= LVCF_TEXT
;
565 lvCol
.pszText
= new wxChar
[513];
566 lvCol
.cchTextMax
= 512;
569 bool success
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0;
571 // item.m_subItem = lvCol.iSubItem;
572 item
.m_width
= lvCol
.cx
;
574 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
576 item
.m_text
= lvCol
.pszText
;
577 delete[] lvCol
.pszText
;
580 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
582 if (lvCol
.fmt
== LVCFMT_LEFT
)
583 item
.m_format
= wxLIST_FORMAT_LEFT
;
584 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
585 item
.m_format
= wxLIST_FORMAT_RIGHT
;
586 else if (lvCol
.fmt
== LVCFMT_CENTER
)
587 item
.m_format
= wxLIST_FORMAT_CENTRE
;
593 // Sets information about this column
594 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
597 wxConvertToMSWListCol(col
, item
, lvCol
);
599 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
602 // Gets the column width
603 int wxListCtrl::GetColumnWidth(int col
) const
605 return ListView_GetColumnWidth(GetHwnd(), col
);
608 // Sets the column width
609 bool wxListCtrl::SetColumnWidth(int col
, int width
)
612 if ( m_windowStyle
& wxLC_LIST
)
616 if ( width2
== wxLIST_AUTOSIZE
)
617 width2
= LVSCW_AUTOSIZE
;
618 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
619 width2
= LVSCW_AUTOSIZE_USEHEADER
;
621 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
624 // Gets the number of items that can fit vertically in the
625 // visible area of the list control (list or report view)
626 // or the total number of items in the list control (icon
627 // or small icon view)
628 int wxListCtrl::GetCountPerPage() const
630 return ListView_GetCountPerPage(GetHwnd());
633 // Gets the edit control for editing labels.
634 wxTextCtrl
* wxListCtrl::GetEditControl() const
639 // Gets information about the item
640 bool wxListCtrl::GetItem(wxListItem
& info
) const
643 wxZeroMemory(lvItem
);
645 lvItem
.iItem
= info
.m_itemId
;
646 lvItem
.iSubItem
= info
.m_col
;
648 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
650 lvItem
.mask
|= LVIF_TEXT
;
651 lvItem
.pszText
= new wxChar
[513];
652 lvItem
.cchTextMax
= 512;
656 lvItem
.pszText
= NULL
;
659 if (info
.m_mask
& wxLIST_MASK_DATA
)
660 lvItem
.mask
|= LVIF_PARAM
;
662 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
663 lvItem
.mask
|= LVIF_IMAGE
;
665 if ( info
.m_mask
& wxLIST_MASK_STATE
)
667 lvItem
.mask
|= LVIF_STATE
;
668 // the other bits are hardly interesting anyhow
669 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
672 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
675 wxLogError(_("Couldn't retrieve information about list control item %d."),
680 // give NULL as hwnd as we already have everything we need
681 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
685 delete[] lvItem
.pszText
;
690 // Sets information about the item
691 bool wxListCtrl::SetItem(wxListItem
& info
)
694 wxConvertToMSWListItem(this, info
, item
);
697 if ( !ListView_SetItem(GetHwnd(), &item
) )
699 wxLogDebug(_T("ListView_SetItem() failed"));
704 // we need to update the item immediately to show the new image
705 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
707 // check whether it has any custom attributes
708 if ( info
.HasAttributes() )
710 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
713 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
715 *attr
= *info
.GetAttributes();
719 // if the colour has changed, we must redraw the item
725 // we need this to make the change visible right now
726 ListView_Update(GetHwnd(), item
.iItem
);
732 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
736 info
.m_mask
= wxLIST_MASK_TEXT
;
737 info
.m_itemId
= index
;
741 info
.m_image
= imageId
;
742 info
.m_mask
|= wxLIST_MASK_IMAGE
;
744 return SetItem(info
);
748 // Gets the item state
749 int wxListCtrl::GetItemState(long item
, long stateMask
) const
753 info
.m_mask
= wxLIST_MASK_STATE
;
754 info
.m_stateMask
= stateMask
;
755 info
.m_itemId
= item
;
763 // Sets the item state
764 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
766 // NB: don't use SetItem() here as it doesn't work with the virtual list
769 wxZeroMemory(lvItem
);
771 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
773 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
774 (WPARAM
)item
, (LPARAM
)&lvItem
) )
776 wxLogLastError(_T("ListView_SetItemState"));
784 // Sets the item image
785 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
789 info
.m_mask
= wxLIST_MASK_IMAGE
;
790 info
.m_image
= image
;
791 info
.m_itemId
= item
;
793 return SetItem(info
);
796 // Gets the item text
797 wxString
wxListCtrl::GetItemText(long item
) const
801 info
.m_mask
= wxLIST_MASK_TEXT
;
802 info
.m_itemId
= item
;
809 // Sets the item text
810 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
814 info
.m_mask
= wxLIST_MASK_TEXT
;
815 info
.m_itemId
= item
;
821 // Gets the item data
822 long wxListCtrl::GetItemData(long item
) const
826 info
.m_mask
= wxLIST_MASK_DATA
;
827 info
.m_itemId
= item
;
834 // Sets the item data
835 bool wxListCtrl::SetItemData(long item
, long data
)
839 info
.m_mask
= wxLIST_MASK_DATA
;
840 info
.m_itemId
= item
;
843 return SetItem(info
);
846 // Gets the item rectangle
847 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
851 int code2
= LVIR_BOUNDS
;
852 if ( code
== wxLIST_RECT_BOUNDS
)
854 else if ( code
== wxLIST_RECT_ICON
)
856 else if ( code
== wxLIST_RECT_LABEL
)
860 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
862 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
867 rect
.width
= rect2
.right
- rect2
.left
;
868 rect
.height
= rect2
.bottom
- rect2
.top
;
872 // Gets the item position
873 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
877 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
879 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
883 // Sets the item position.
884 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
886 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
889 // Gets the number of items in the list control
890 int wxListCtrl::GetItemCount() const
892 return ListView_GetItemCount(GetHwnd());
895 // Retrieves the spacing between icons in pixels.
896 // If small is TRUE, gets the spacing for the small icon
897 // view, otherwise the large icon view.
898 int wxListCtrl::GetItemSpacing(bool isSmall
) const
900 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
903 // Gets the number of selected items in the list control
904 int wxListCtrl::GetSelectedItemCount() const
906 return ListView_GetSelectedCount(GetHwnd());
909 // Gets the text colour of the listview
910 wxColour
wxListCtrl::GetTextColour() const
912 COLORREF ref
= ListView_GetTextColor(GetHwnd());
913 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
917 // Sets the text colour of the listview
918 void wxListCtrl::SetTextColour(const wxColour
& col
)
920 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
923 // Gets the index of the topmost visible item when in
924 // list or report view
925 long wxListCtrl::GetTopItem() const
927 return (long) ListView_GetTopIndex(GetHwnd());
930 // Searches for an item, starting from 'item'.
931 // 'geometry' is one of
932 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
933 // 'state' is a state bit flag, one or more of
934 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
935 // item can be -1 to find the first item that matches the
937 // Returns the item or -1 if unsuccessful.
938 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
942 if ( geom
== wxLIST_NEXT_ABOVE
)
944 if ( geom
== wxLIST_NEXT_ALL
)
946 if ( geom
== wxLIST_NEXT_BELOW
)
948 if ( geom
== wxLIST_NEXT_LEFT
)
949 flags
|= LVNI_TOLEFT
;
950 if ( geom
== wxLIST_NEXT_RIGHT
)
951 flags
|= LVNI_TORIGHT
;
953 if ( state
& wxLIST_STATE_CUT
)
955 if ( state
& wxLIST_STATE_DROPHILITED
)
956 flags
|= LVNI_DROPHILITED
;
957 if ( state
& wxLIST_STATE_FOCUSED
)
958 flags
|= LVNI_FOCUSED
;
959 if ( state
& wxLIST_STATE_SELECTED
)
960 flags
|= LVNI_SELECTED
;
962 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
966 wxImageList
*wxListCtrl::GetImageList(int which
) const
968 if ( which
== wxIMAGE_LIST_NORMAL
)
970 return m_imageListNormal
;
972 else if ( which
== wxIMAGE_LIST_SMALL
)
974 return m_imageListSmall
;
976 else if ( which
== wxIMAGE_LIST_STATE
)
978 return m_imageListState
;
983 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
986 if ( which
== wxIMAGE_LIST_NORMAL
)
988 flags
= LVSIL_NORMAL
;
989 if (m_ownsImageListNormal
) delete m_imageListNormal
;
990 m_imageListNormal
= imageList
;
991 m_ownsImageListNormal
= FALSE
;
993 else if ( which
== wxIMAGE_LIST_SMALL
)
996 if (m_ownsImageListSmall
) delete m_imageListSmall
;
997 m_imageListSmall
= imageList
;
998 m_ownsImageListSmall
= FALSE
;
1000 else if ( which
== wxIMAGE_LIST_STATE
)
1002 flags
= LVSIL_STATE
;
1003 if (m_ownsImageListState
) delete m_imageListState
;
1004 m_imageListState
= imageList
;
1005 m_ownsImageListState
= FALSE
;
1007 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1010 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1012 SetImageList(imageList
, which
);
1013 if ( which
== wxIMAGE_LIST_NORMAL
)
1014 m_ownsImageListNormal
= TRUE
;
1015 else if ( which
== wxIMAGE_LIST_SMALL
)
1016 m_ownsImageListSmall
= TRUE
;
1017 else if ( which
== wxIMAGE_LIST_STATE
)
1018 m_ownsImageListState
= TRUE
;
1021 // ----------------------------------------------------------------------------
1023 // ----------------------------------------------------------------------------
1025 // Arranges the items
1026 bool wxListCtrl::Arrange(int flag
)
1029 if ( flag
== wxLIST_ALIGN_LEFT
)
1030 code
= LVA_ALIGNLEFT
;
1031 else if ( flag
== wxLIST_ALIGN_TOP
)
1032 code
= LVA_ALIGNTOP
;
1033 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1035 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1036 code
= LVA_SNAPTOGRID
;
1038 return (ListView_Arrange(GetHwnd(), code
) != 0);
1042 bool wxListCtrl::DeleteItem(long item
)
1044 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1047 // Deletes all items
1048 bool wxListCtrl::DeleteAllItems()
1050 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1053 // Deletes all items
1054 bool wxListCtrl::DeleteAllColumns()
1056 while ( m_colCount
> 0 )
1058 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1060 wxLogLastError(wxT("ListView_DeleteColumn"));
1068 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1074 bool wxListCtrl::DeleteColumn(int col
)
1076 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1078 if ( success
&& (m_colCount
> 0) )
1083 // Clears items, and columns if there are any.
1084 void wxListCtrl::ClearAll()
1087 if ( m_colCount
> 0 )
1091 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1093 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1095 // VS: ListView_EditLabel requires that the list has focus.
1097 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1101 m_textCtrl
->SetHWND(0);
1102 m_textCtrl
->UnsubclassWin();
1107 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1108 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1109 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1114 // End label editing, optionally cancelling the edit
1115 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1117 wxFAIL_MSG( _T("not implemented") );
1122 // Ensures this item is visible
1123 bool wxListCtrl::EnsureVisible(long item
)
1125 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1128 // Find an item whose label matches this string, starting from the item after 'start'
1129 // or the beginning if 'start' is -1.
1130 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1132 LV_FINDINFO findInfo
;
1134 findInfo
.flags
= LVFI_STRING
;
1136 findInfo
.flags
|= LVFI_PARTIAL
;
1139 // ListView_FindItem() excludes the first item from search and to look
1140 // through all the items you need to start from -1 which is unnatural and
1141 // inconsistent with the generic version - so we adjust the index
1144 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1147 // Find an item whose data matches this data, starting from the item after 'start'
1148 // or the beginning if 'start' is -1.
1149 long wxListCtrl::FindItem(long start
, long data
)
1151 LV_FINDINFO findInfo
;
1153 findInfo
.flags
= LVFI_PARAM
;
1154 findInfo
.lParam
= data
;
1156 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1159 // Find an item nearest this position in the specified direction, starting from
1160 // the item after 'start' or the beginning if 'start' is -1.
1161 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1163 LV_FINDINFO findInfo
;
1165 findInfo
.flags
= LVFI_NEARESTXY
;
1166 findInfo
.pt
.x
= pt
.x
;
1167 findInfo
.pt
.y
= pt
.y
;
1168 findInfo
.vkDirection
= VK_RIGHT
;
1170 if ( direction
== wxLIST_FIND_UP
)
1171 findInfo
.vkDirection
= VK_UP
;
1172 else if ( direction
== wxLIST_FIND_DOWN
)
1173 findInfo
.vkDirection
= VK_DOWN
;
1174 else if ( direction
== wxLIST_FIND_LEFT
)
1175 findInfo
.vkDirection
= VK_LEFT
;
1176 else if ( direction
== wxLIST_FIND_RIGHT
)
1177 findInfo
.vkDirection
= VK_RIGHT
;
1179 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1182 // Determines which item (if any) is at the specified point,
1183 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1184 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1186 LV_HITTESTINFO hitTestInfo
;
1187 hitTestInfo
.pt
.x
= (int) point
.x
;
1188 hitTestInfo
.pt
.y
= (int) point
.y
;
1190 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1193 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1194 flags
|= wxLIST_HITTEST_ABOVE
;
1195 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1196 flags
|= wxLIST_HITTEST_BELOW
;
1197 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1198 flags
|= wxLIST_HITTEST_NOWHERE
;
1199 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1200 flags
|= wxLIST_HITTEST_ONITEMICON
;
1201 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1202 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1203 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1204 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1205 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1206 flags
|= wxLIST_HITTEST_TOLEFT
;
1207 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1208 flags
|= wxLIST_HITTEST_TORIGHT
;
1210 return (long) hitTestInfo
.iItem
;
1213 // Inserts an item, returning the index of the new item if successful,
1215 long wxListCtrl::InsertItem(wxListItem
& info
)
1217 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1220 wxConvertToMSWListItem(this, info
, item
);
1222 // check whether it has any custom attributes
1223 if ( info
.HasAttributes() )
1226 wxListItemAttr
*attr
;
1227 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1231 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1233 else *attr
= *info
.GetAttributes();
1235 m_hasAnyAttr
= TRUE
;
1238 return (long) ListView_InsertItem(GetHwnd(), & item
);
1241 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1244 info
.m_text
= label
;
1245 info
.m_mask
= wxLIST_MASK_TEXT
;
1246 info
.m_itemId
= index
;
1247 return InsertItem(info
);
1250 // Inserts an image item
1251 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1254 info
.m_image
= imageIndex
;
1255 info
.m_mask
= wxLIST_MASK_IMAGE
;
1256 info
.m_itemId
= index
;
1257 return InsertItem(info
);
1260 // Inserts an image/string item
1261 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1264 info
.m_image
= imageIndex
;
1265 info
.m_text
= label
;
1266 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1267 info
.m_itemId
= index
;
1268 return InsertItem(info
);
1271 // For list view mode (only), inserts a column.
1272 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1275 wxConvertToMSWListCol(col
, item
, lvCol
);
1277 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1279 // always give some width to the new column: this one is compatible
1280 // with the generic version
1281 lvCol
.mask
|= LVCF_WIDTH
;
1285 // when we insert a column which can contain an image, we must specify this
1286 // flag right now as doing it later in SetColumn() has no effect
1288 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1289 // way to dynamically set/clear the bitmap as the column without a bitmap
1290 // on the left looks ugly (there is a hole)
1292 // unfortunately with my version of comctl32.dll (5.80), the left column
1293 // image is always on the left and it seems that it's a "feature" - I
1294 // didn't find any way to work around it in any case
1295 if ( lvCol
.mask
& LVCF_IMAGE
)
1297 lvCol
.mask
|= LVCF_FMT
;
1298 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1301 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1308 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1315 long wxListCtrl::InsertColumn(long col
,
1316 const wxString
& heading
,
1321 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1322 item
.m_text
= heading
;
1325 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1326 item
.m_width
= width
;
1328 item
.m_format
= format
;
1330 return InsertColumn(col
, item
);
1333 // Scrolls the list control. If in icon, small icon or report view mode,
1334 // x specifies the number of pixels to scroll. If in list view mode, x
1335 // specifies the number of columns to scroll.
1336 // If in icon, small icon or list view mode, y specifies the number of pixels
1337 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1338 bool wxListCtrl::ScrollList(int dx
, int dy
)
1340 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1345 // fn is a function which takes 3 long arguments: item1, item2, data.
1346 // item1 is the long data associated with a first item (NOT the index).
1347 // item2 is the long data associated with a second item (NOT the index).
1348 // data is the same value as passed to SortItems.
1349 // The return value is a negative number if the first item should precede the second
1350 // item, a positive number of the second item should precede the first,
1351 // or zero if the two items are equivalent.
1353 // data is arbitrary data to be passed to the sort function.
1355 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1356 // time for anything better right now (VZ)
1357 static long gs_sortData
= 0;
1358 static wxListCtrl
*gs_sortCtrl
= NULL
;
1359 static wxListCtrlCompare gs_sortFunction
= NULL
;
1361 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1363 int n1
= *(const int *)arg1
,
1364 n2
= *(const int *)arg2
;
1366 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1367 gs_sortCtrl
->GetItemData(n2
),
1371 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1373 // sort the attributes too
1377 count
= GetItemCount();
1378 int *aItems
= new int[count
];
1379 for ( n
= 0; n
< count
; n
++ )
1386 gs_sortFunction
= fn
;
1388 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1392 gs_sortFunction
= NULL
;
1394 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1395 for ( n
= 0; n
< count
; n
++ )
1397 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1400 attrsNew
.Put(n
, attr
);
1410 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1412 wxLogDebug(_T("ListView_SortItems() failed"));
1422 // ----------------------------------------------------------------------------
1423 // message processing
1424 // ----------------------------------------------------------------------------
1426 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1428 if (cmd
== EN_UPDATE
)
1430 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1431 event
.SetEventObject( this );
1432 ProcessCommand(event
);
1435 else if (cmd
== EN_KILLFOCUS
)
1437 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1438 event
.SetEventObject( this );
1439 ProcessCommand(event
);
1446 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1448 // prepare the event
1449 // -----------------
1451 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1452 wxEventType eventType
= wxEVT_NULL
;
1454 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1456 // check for messages from the header (in report view)
1457 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1459 // is it a message from the header?
1460 if ( nmhdr
->hwndFrom
== hwndHdr
)
1462 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1463 event
.m_itemIndex
= -1;
1465 switch ( nmhdr
->code
)
1467 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1468 // TRACK messages even to ANSI programs: on my system I get
1469 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1471 // work around is to simply catch both versions and hope that it
1472 // works (why should this message exist in ANSI and Unicode is
1473 // beyond me as it doesn't deal with strings at all...)
1474 case HDN_BEGINTRACKA
:
1475 case HDN_BEGINTRACKW
:
1476 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1481 if ( eventType
== wxEVT_NULL
)
1482 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1487 if ( eventType
== wxEVT_NULL
)
1488 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1489 event
.m_col
= nmHDR
->iItem
;
1494 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1497 // find the column clicked: we have to search for it
1498 // ourselves as the notification message doesn't provide
1501 // where did the click occur?
1503 if ( !::GetCursorPos(&ptClick
) )
1505 wxLogLastError(_T("GetCursorPos"));
1508 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1510 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1513 event
.m_pointDrag
.x
= ptClick
.x
;
1514 event
.m_pointDrag
.y
= ptClick
.y
;
1516 int colCount
= Header_GetItemCount(hwndHdr
);
1519 for ( int col
= 0; col
< colCount
; col
++ )
1521 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1523 if ( ::PtInRect(&rect
, ptClick
) )
1534 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1537 else if ( nmhdr
->hwndFrom
== GetHwnd() )
1539 // almost all messages use NM_LISTVIEW
1540 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1542 // this is true for almost all events
1543 event
.m_item
.m_data
= nmLV
->lParam
;
1545 switch ( nmhdr
->code
)
1547 case LVN_BEGINRDRAG
:
1548 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1552 if ( eventType
== wxEVT_NULL
)
1554 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1557 event
.m_itemIndex
= nmLV
->iItem
;
1558 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1559 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1562 case LVN_BEGINLABELEDIT
:
1564 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1565 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1566 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1567 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1571 case LVN_COLUMNCLICK
:
1572 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1573 event
.m_itemIndex
= -1;
1574 event
.m_col
= nmLV
->iSubItem
;
1577 case LVN_DELETEALLITEMS
:
1578 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1579 event
.m_itemIndex
= -1;
1585 case LVN_DELETEITEM
:
1586 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1587 event
.m_itemIndex
= nmLV
->iItem
;
1591 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1595 case LVN_ENDLABELEDIT
:
1597 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1598 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1599 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1600 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1603 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1607 case LVN_SETDISPINFO
:
1609 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1610 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1611 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1615 case LVN_INSERTITEM
:
1616 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1617 event
.m_itemIndex
= nmLV
->iItem
;
1620 case LVN_ITEMCHANGED
:
1621 // This needs to be sent to wxListCtrl as a rather more concrete
1622 // event. For now, just detect a selection or deselection.
1623 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1625 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1626 event
.m_itemIndex
= nmLV
->iItem
;
1628 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1630 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1631 event
.m_itemIndex
= nmLV
->iItem
;
1641 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1642 WORD wVKey
= info
->wVKey
;
1644 // get the current selection
1645 long lItem
= GetNextItem(-1,
1647 wxLIST_STATE_SELECTED
);
1649 // <Enter> or <Space> activate the selected item if any (but
1650 // not with Shift and/or Ctrl as then they have a predefined
1651 // meaning for the list view)
1653 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1654 !(wxIsShiftDown() || wxIsCtrlDown()) )
1656 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1660 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1661 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1665 event
.m_item
.m_itemId
= lItem
;
1669 // fill the other fields too
1670 event
.m_item
.m_text
= GetItemText(lItem
);
1671 event
.m_item
.m_data
= GetItemData(lItem
);
1677 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1679 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1684 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1685 // if it happened on an item (and not on empty place)
1686 if ( nmLV
->iItem
== -1 )
1692 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1693 event
.m_itemIndex
= nmLV
->iItem
;
1694 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1695 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1699 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1700 // don't do anything else
1701 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1706 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1707 LV_HITTESTINFO lvhti
;
1708 wxZeroMemory(lvhti
);
1710 ::GetCursorPos(&(lvhti
.pt
));
1711 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1712 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1714 if ( lvhti
.flags
& LVHT_ONITEM
)
1716 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1717 event
.m_itemIndex
= lvhti
.iItem
;
1718 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1719 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1724 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1725 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1727 *result
= OnCustomDraw(lParam
);
1730 #endif // _WIN32_IE >= 0x300
1732 case LVN_ODCACHEHINT
:
1734 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1736 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1738 // we get some really stupid cache hints like ones for items in
1739 // range 0..0 for an empty control or, after deleting an item,
1740 // for items in invalid range - filter this garbage out
1741 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1743 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1745 long iMax
= GetItemCount();
1746 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1756 case LVN_GETDISPINFO
:
1759 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1761 LV_ITEM
& lvi
= info
->item
;
1762 long item
= lvi
.iItem
;
1764 if ( lvi
.mask
& LVIF_TEXT
)
1766 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1767 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1770 if ( lvi
.mask
& LVIF_IMAGE
)
1772 lvi
.iImage
= OnGetItemImage(item
);
1775 // a little dose of healthy paranoia: as we never use
1776 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1777 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1778 _T("we don't support state callbacks yet!") );
1785 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1790 // where did this one come from?
1794 // process the event
1795 // -----------------
1797 event
.SetEventObject( this );
1798 event
.SetEventType(eventType
);
1800 if ( !GetEventHandler()->ProcessEvent(event
) )
1806 switch ( nmhdr
->code
)
1808 case LVN_DELETEALLITEMS
:
1809 // always return TRUE to suppress all additional LVN_DELETEITEM
1810 // notifications - this makes deleting all items from a list ctrl
1816 case LVN_ENDLABELEDIT
:
1817 // logic here is inversed compared to all the other messages
1818 *result
= event
.IsAllowed();
1823 *result
= !event
.IsAllowed();
1828 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1830 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1832 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1833 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1834 switch ( nmcd
.dwDrawStage
)
1837 // if we've got any items with non standard attributes,
1838 // notify us before painting each item
1840 // for virtual controls, always suppose that we have attributes as
1841 // there is no way to check for this
1842 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1845 case CDDS_ITEMPREPAINT
:
1847 size_t item
= (size_t)nmcd
.dwItemSpec
;
1848 if ( item
>= (size_t)GetItemCount() )
1850 // we get this message with item == 0 for an empty control,
1851 // we must ignore it as calling OnGetItemAttr() would be
1853 return CDRF_DODEFAULT
;
1856 wxListItemAttr
*attr
=
1857 IsVirtual() ? OnGetItemAttr(item
)
1858 : (wxListItemAttr
*)m_attrs
.Get(item
);
1862 // nothing to do for this item
1863 return CDRF_DODEFAULT
;
1867 wxColour colText
, colBack
;
1868 if ( attr
->HasFont() )
1870 wxFont font
= attr
->GetFont();
1871 hFont
= (HFONT
)font
.GetResourceHandle();
1878 if ( attr
->HasTextColour() )
1880 colText
= attr
->GetTextColour();
1884 colText
= GetTextColour();
1887 if ( attr
->HasBackgroundColour() )
1889 colBack
= attr
->GetBackgroundColour();
1893 colBack
= GetBackgroundColour();
1896 lplvcd
->clrText
= wxColourToRGB(colText
);
1897 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1899 // note that if we wanted to set colours for
1900 // individual columns (subitems), we would have
1901 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1904 ::SelectObject(nmcd
.hdc
, hFont
);
1906 return CDRF_NEWFONT
;
1909 // fall through to return CDRF_DODEFAULT
1912 return CDRF_DODEFAULT
;
1916 #endif // NM_CUSTOMDRAW supported
1918 // Necessary for drawing hrules and vrules, if specified
1919 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1923 wxControl::OnPaint(event
);
1925 // Reset the device origin since it may have been set
1926 dc
.SetDeviceOrigin(0, 0);
1928 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1929 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1931 if (!drawHRules
&& !drawVRules
)
1933 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1936 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1938 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1940 wxSize clientSize
= GetClientSize();
1944 int itemCount
= GetItemCount();
1948 long top
= GetTopItem();
1949 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
1951 if (GetItemRect(i
, itemRect
))
1953 cy
= itemRect
.GetTop();
1954 if (i
!= 0) // Don't draw the first one
1956 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1959 if (i
== itemCount
- 1)
1961 cy
= itemRect
.GetBottom();
1962 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1968 if (drawVRules
&& (i
> -1))
1970 wxRect firstItemRect
;
1971 GetItemRect(0, firstItemRect
);
1973 if (GetItemRect(i
, itemRect
))
1976 int x
= itemRect
.GetX();
1977 for (col
= 0; col
< GetColumnCount(); col
++)
1979 int colWidth
= GetColumnWidth(col
);
1981 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1987 // ----------------------------------------------------------------------------
1988 // virtual list controls
1989 // ----------------------------------------------------------------------------
1991 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
1993 // this is a pure virtual function, in fact - which is not really pure
1994 // because the controls which are not virtual don't need to implement it
1995 wxFAIL_MSG( _T("not supposed to be called") );
1997 return wxEmptyString
;
2000 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2003 wxFAIL_MSG( _T("not supposed to be called") );
2008 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2010 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2011 _T("invalid item index in OnGetItemAttr()") );
2013 // no attributes by default
2017 void wxListCtrl::SetItemCount(long count
)
2019 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2021 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2023 wxLogLastError(_T("ListView_SetItemCount"));
2027 void wxListCtrl::RefreshItem(long item
)
2029 if ( !ListView_Update(GetHwnd(), item
) )
2031 wxLogLastError(_T("ListView_Update"));
2035 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2037 wxRect rect1
, rect2
;
2038 GetItemRect(itemFrom
, rect1
);
2039 GetItemRect(itemTo
, rect2
);
2041 wxRect rect
= rect1
;
2042 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2047 // ----------------------------------------------------------------------------
2049 // ----------------------------------------------------------------------------
2051 // List item structure
2052 wxListItem::wxListItem()
2062 m_format
= wxLIST_FORMAT_CENTRE
;
2068 void wxListItem::Clear()
2077 m_format
= wxLIST_FORMAT_CENTRE
;
2079 m_text
= wxEmptyString
;
2081 if (m_attr
) delete m_attr
;
2085 void wxListItem::ClearAttributes()
2087 if (m_attr
) delete m_attr
;
2091 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2095 info
.m_data
= lvItem
.lParam
;
2098 info
.m_stateMask
= 0;
2099 info
.m_itemId
= lvItem
.iItem
;
2101 long oldMask
= lvItem
.mask
;
2103 bool needText
= FALSE
;
2104 if (hwndListCtrl
!= 0)
2106 if ( lvItem
.mask
& LVIF_TEXT
)
2113 lvItem
.pszText
= new wxChar
[513];
2114 lvItem
.cchTextMax
= 512;
2116 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2117 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2120 if ( lvItem
.mask
& LVIF_STATE
)
2122 info
.m_mask
|= wxLIST_MASK_STATE
;
2124 if ( lvItem
.stateMask
& LVIS_CUT
)
2126 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2127 if ( lvItem
.state
& LVIS_CUT
)
2128 info
.m_state
|= wxLIST_STATE_CUT
;
2130 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2132 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2133 if ( lvItem
.state
& LVIS_DROPHILITED
)
2134 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2136 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2138 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2139 if ( lvItem
.state
& LVIS_FOCUSED
)
2140 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2142 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2144 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2145 if ( lvItem
.state
& LVIS_SELECTED
)
2146 info
.m_state
|= wxLIST_STATE_SELECTED
;
2150 if ( lvItem
.mask
& LVIF_TEXT
)
2152 info
.m_mask
|= wxLIST_MASK_TEXT
;
2153 info
.m_text
= lvItem
.pszText
;
2155 if ( lvItem
.mask
& LVIF_IMAGE
)
2157 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2158 info
.m_image
= lvItem
.iImage
;
2160 if ( lvItem
.mask
& LVIF_PARAM
)
2161 info
.m_mask
|= wxLIST_MASK_DATA
;
2162 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2163 info
.m_mask
|= wxLIST_SET_ITEM
;
2164 info
.m_col
= lvItem
.iSubItem
;
2169 delete[] lvItem
.pszText
;
2171 lvItem
.mask
= oldMask
;
2174 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2176 if (stateMask
& wxLIST_STATE_CUT
)
2178 lvItem
.stateMask
|= LVIS_CUT
;
2179 if (state
& wxLIST_STATE_CUT
)
2180 lvItem
.state
|= LVIS_CUT
;
2182 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2184 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2185 if (state
& wxLIST_STATE_DROPHILITED
)
2186 lvItem
.state
|= LVIS_DROPHILITED
;
2188 if (stateMask
& wxLIST_STATE_FOCUSED
)
2190 lvItem
.stateMask
|= LVIS_FOCUSED
;
2191 if (state
& wxLIST_STATE_FOCUSED
)
2192 lvItem
.state
|= LVIS_FOCUSED
;
2194 if (stateMask
& wxLIST_STATE_SELECTED
)
2196 lvItem
.stateMask
|= LVIS_SELECTED
;
2197 if (state
& wxLIST_STATE_SELECTED
)
2198 lvItem
.state
|= LVIS_SELECTED
;
2202 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2203 const wxListItem
& info
,
2206 lvItem
.iItem
= (int) info
.m_itemId
;
2208 lvItem
.iImage
= info
.m_image
;
2209 lvItem
.lParam
= info
.m_data
;
2210 lvItem
.stateMask
= 0;
2213 lvItem
.iSubItem
= info
.m_col
;
2215 if (info
.m_mask
& wxLIST_MASK_STATE
)
2217 lvItem
.mask
|= LVIF_STATE
;
2219 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2222 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2224 lvItem
.mask
|= LVIF_TEXT
;
2225 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2227 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2231 // pszText is not const, hence the cast
2232 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2233 if ( lvItem
.pszText
)
2234 lvItem
.cchTextMax
= info
.m_text
.Length();
2236 lvItem
.cchTextMax
= 0;
2239 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2240 lvItem
.mask
|= LVIF_IMAGE
;
2241 if (info
.m_mask
& wxLIST_MASK_DATA
)
2242 lvItem
.mask
|= LVIF_PARAM
;
2245 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
2248 wxZeroMemory(lvCol
);
2250 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2252 lvCol
.mask
|= LVCF_TEXT
;
2253 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2256 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2258 lvCol
.mask
|= LVCF_FMT
;
2260 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2261 lvCol
.fmt
= LVCFMT_LEFT
;
2262 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2263 lvCol
.fmt
= LVCFMT_RIGHT
;
2264 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2265 lvCol
.fmt
= LVCFMT_CENTER
;
2268 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2270 lvCol
.mask
|= LVCF_WIDTH
;
2271 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2272 lvCol
.cx
= LVSCW_AUTOSIZE
;
2273 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2274 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2276 lvCol
.cx
= item
.m_width
;
2279 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2281 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2283 lvCol
.mask
|= LVCF_IMAGE
;
2284 lvCol
.iImage
= item
.m_image
;
2286 //else: it doesn't support item images anyhow
2290 // ----------------------------------------------------------------------------
2292 // ----------------------------------------------------------------------------
2294 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2296 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2297 : wxNotifyEvent(commandType
, id
)
2303 m_cancelled
= FALSE
;
2306 #endif // wxUSE_LISTCTRL