1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
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"
54 #include "wx/msw/missing.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // convert our state and mask flags to LV_ITEM constants
61 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
63 // convert wxListItem to LV_ITEM
64 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
65 const wxListItem
& info
, LV_ITEM
& lvItem
);
67 // convert LV_ITEM to wxListItem
68 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
70 /* const */ LV_ITEM
& lvItem
);
72 // convert our wxListItem to LV_COLUMN
73 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
76 // ----------------------------------------------------------------------------
77 // private helper classes
78 // ----------------------------------------------------------------------------
80 // We have to handle both fooW and fooA notifications in several cases
81 // because of broken commctl.dll and/or unicows.dll. This class is used to
82 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
83 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
84 // by wxConvertToMSWListItem().
88 ~wxLV_ITEM() { delete m_buf
; }
89 operator LV_ITEM
&() const { return *m_item
; }
92 wxLV_ITEM(LV_ITEMW
&item
) : m_buf(NULL
), m_item(&item
) {}
93 wxLV_ITEM(LV_ITEMA
&item
)
95 m_item
= new LV_ITEM((LV_ITEM
&)item
);
96 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
98 m_buf
= new wxMB2WXbuf(wxConvLocal
.cMB2WX(item
.pszText
));
99 m_item
->pszText
= (wxChar
*)m_buf
->data();
108 wxLV_ITEM(LV_ITEMW
&item
)
110 m_item
= new LV_ITEM((LV_ITEM
&)item
);
111 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
113 m_buf
= new wxWC2WXbuf(wxConvLocal
.cWC2WX(item
.pszText
));
114 m_item
->pszText
= (wxChar
*)m_buf
->data();
119 wxLV_ITEM(LV_ITEMA
&item
) : m_buf(NULL
), m_item(&item
) {}
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
132 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
133 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
134 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
135 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
136 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
137 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
138 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
139 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
140 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
141 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
142 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
143 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
144 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
145 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
146 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
147 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
148 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
149 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
150 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
151 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
152 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
154 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
155 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
156 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
158 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
160 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
161 EVT_PAINT(wxListCtrl::OnPaint
)
164 // ============================================================================
166 // ============================================================================
168 // ----------------------------------------------------------------------------
169 // wxListCtrl construction
170 // ----------------------------------------------------------------------------
172 void wxListCtrl::Init()
174 m_imageListNormal
= NULL
;
175 m_imageListSmall
= NULL
;
176 m_imageListState
= NULL
;
177 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
181 m_hasAnyAttr
= FALSE
;
184 bool wxListCtrl::Create(wxWindow
*parent
,
189 const wxValidator
& validator
,
190 const wxString
& name
)
193 SetValidator(validator
);
194 #endif // wxUSE_VALIDATORS
203 m_windowStyle
= style
;
216 m_windowId
= (id
== -1) ? NewControlId() : id
;
218 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
219 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
221 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
222 wstyle
|= WS_CLIPSIBLINGS
;
224 if ( wxStyleHasBorder(m_windowStyle
) )
226 m_baseStyle
= wstyle
;
228 if ( !DoCreateControl(x
, y
, width
, height
) )
232 parent
->AddChild(this);
237 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
239 DWORD wstyle
= m_baseStyle
;
242 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
244 // Even with extended styles, need to combine with WS_BORDER
245 // for them to look right.
249 long oldStyle
= 0; // Dummy
250 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
252 // Create the ListView control.
253 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
258 GetWinHwnd(GetParent()),
265 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
270 // for comctl32.dll v 4.70+ we want to have this attribute because it's
271 // prettier (and also because wxGTK does it like this)
272 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
274 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
275 0, LVS_EX_FULLROWSELECT
);
278 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
279 SetForegroundColour(GetParent()->GetForegroundColour());
286 void wxListCtrl::UpdateStyle()
290 // The new window view style
292 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
293 dwStyleNew
|= m_baseStyle
;
295 // Get the current window style.
296 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
298 // Only set the window style if the view bits have changed.
299 if ( dwStyleOld
!= dwStyleNew
)
301 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
306 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
310 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
312 delete (wxListItemAttr
*)node
->Data();
318 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
321 m_hasAnyAttr
= FALSE
;
325 wxListCtrl::~wxListCtrl()
327 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
331 m_textCtrl
->SetHWND(0);
332 m_textCtrl
->UnsubclassWin();
337 if (m_ownsImageListNormal
) delete m_imageListNormal
;
338 if (m_ownsImageListSmall
) delete m_imageListSmall
;
339 if (m_ownsImageListState
) delete m_imageListState
;
342 // ----------------------------------------------------------------------------
343 // set/get/change style
344 // ----------------------------------------------------------------------------
346 // Add or remove a single window style
347 void wxListCtrl::SetSingleStyle(long style
, bool add
)
349 long flag
= GetWindowStyleFlag();
351 // Get rid of conflicting styles
354 if ( style
& wxLC_MASK_TYPE
)
355 flag
= flag
& ~wxLC_MASK_TYPE
;
356 if ( style
& wxLC_MASK_ALIGN
)
357 flag
= flag
& ~wxLC_MASK_ALIGN
;
358 if ( style
& wxLC_MASK_SORT
)
359 flag
= flag
& ~wxLC_MASK_SORT
;
375 m_windowStyle
= flag
;
380 // Set the whole window style
381 void wxListCtrl::SetWindowStyleFlag(long flag
)
383 m_windowStyle
= flag
;
388 // Can be just a single style, or a bitlist
389 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
392 if ( style
& wxLC_ICON
)
394 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
395 oldStyle
-= LVS_SMALLICON
;
396 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
397 oldStyle
-= LVS_REPORT
;
398 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
399 oldStyle
-= LVS_LIST
;
403 if ( style
& wxLC_SMALL_ICON
)
405 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
406 oldStyle
-= LVS_ICON
;
407 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
408 oldStyle
-= LVS_REPORT
;
409 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
410 oldStyle
-= LVS_LIST
;
411 wstyle
|= LVS_SMALLICON
;
414 if ( style
& wxLC_LIST
)
416 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
417 oldStyle
-= LVS_ICON
;
418 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
419 oldStyle
-= LVS_REPORT
;
420 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
421 oldStyle
-= LVS_SMALLICON
;
425 if ( style
& wxLC_REPORT
)
427 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
428 oldStyle
-= LVS_ICON
;
429 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
430 oldStyle
-= LVS_LIST
;
431 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
432 oldStyle
-= LVS_SMALLICON
;
434 wstyle
|= LVS_REPORT
;
437 if ( style
& wxLC_ALIGN_LEFT
)
439 if ( oldStyle
& LVS_ALIGNTOP
)
440 oldStyle
-= LVS_ALIGNTOP
;
441 wstyle
|= LVS_ALIGNLEFT
;
444 if ( style
& wxLC_ALIGN_TOP
)
446 if ( oldStyle
& LVS_ALIGNLEFT
)
447 oldStyle
-= LVS_ALIGNLEFT
;
448 wstyle
|= LVS_ALIGNTOP
;
451 if ( style
& wxLC_AUTOARRANGE
)
452 wstyle
|= LVS_AUTOARRANGE
;
454 if ( style
& wxLC_NO_SORT_HEADER
)
455 wstyle
|= LVS_NOSORTHEADER
;
457 if ( style
& wxLC_NO_HEADER
)
458 wstyle
|= LVS_NOCOLUMNHEADER
;
460 if ( style
& wxLC_EDIT_LABELS
)
461 wstyle
|= LVS_EDITLABELS
;
463 if ( style
& wxLC_SINGLE_SEL
)
464 wstyle
|= LVS_SINGLESEL
;
466 if ( style
& wxLC_SORT_ASCENDING
)
468 if ( oldStyle
& LVS_SORTDESCENDING
)
469 oldStyle
-= LVS_SORTDESCENDING
;
470 wstyle
|= LVS_SORTASCENDING
;
473 if ( style
& wxLC_SORT_DESCENDING
)
475 if ( oldStyle
& LVS_SORTASCENDING
)
476 oldStyle
-= LVS_SORTASCENDING
;
477 wstyle
|= LVS_SORTDESCENDING
;
480 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
481 if ( style
& wxLC_VIRTUAL
)
483 int ver
= wxTheApp
->GetComCtl32Version();
486 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."),
487 ver
/ 100, ver
% 100);
490 wstyle
|= LVS_OWNERDATA
;
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 // Sets the foreground, i.e. text, colour
502 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
504 if ( !wxWindow::SetForegroundColour(col
) )
507 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
512 // Sets the background colour
513 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
515 if ( !wxWindow::SetBackgroundColour(col
) )
518 // we set the same colour for both the "empty" background and the items
520 COLORREF color
= wxColourToRGB(col
);
521 ListView_SetBkColor(GetHwnd(), color
);
522 ListView_SetTextBkColor(GetHwnd(), color
);
527 // Gets information about this column
528 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
533 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
535 lvCol
.mask
|= LVCF_TEXT
;
536 lvCol
.pszText
= new wxChar
[513];
537 lvCol
.cchTextMax
= 512;
540 bool success
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0;
542 // item.m_subItem = lvCol.iSubItem;
543 item
.m_width
= lvCol
.cx
;
545 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
547 item
.m_text
= lvCol
.pszText
;
548 delete[] lvCol
.pszText
;
551 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
553 if (lvCol
.fmt
== LVCFMT_LEFT
)
554 item
.m_format
= wxLIST_FORMAT_LEFT
;
555 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
556 item
.m_format
= wxLIST_FORMAT_RIGHT
;
557 else if (lvCol
.fmt
== LVCFMT_CENTER
)
558 item
.m_format
= wxLIST_FORMAT_CENTRE
;
564 // Sets information about this column
565 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
568 wxConvertToMSWListCol(col
, item
, lvCol
);
570 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
573 // Gets the column width
574 int wxListCtrl::GetColumnWidth(int col
) const
576 return ListView_GetColumnWidth(GetHwnd(), col
);
579 // Sets the column width
580 bool wxListCtrl::SetColumnWidth(int col
, int width
)
583 if ( m_windowStyle
& wxLC_LIST
)
587 if ( width2
== wxLIST_AUTOSIZE
)
588 width2
= LVSCW_AUTOSIZE
;
589 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
590 width2
= LVSCW_AUTOSIZE_USEHEADER
;
592 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
595 // Gets the number of items that can fit vertically in the
596 // visible area of the list control (list or report view)
597 // or the total number of items in the list control (icon
598 // or small icon view)
599 int wxListCtrl::GetCountPerPage() const
601 return ListView_GetCountPerPage(GetHwnd());
604 // Gets the edit control for editing labels.
605 wxTextCtrl
* wxListCtrl::GetEditControl() const
610 // Gets information about the item
611 bool wxListCtrl::GetItem(wxListItem
& info
) const
614 wxZeroMemory(lvItem
);
616 lvItem
.iItem
= info
.m_itemId
;
617 lvItem
.iSubItem
= info
.m_col
;
619 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
621 lvItem
.mask
|= LVIF_TEXT
;
622 lvItem
.pszText
= new wxChar
[513];
623 lvItem
.cchTextMax
= 512;
627 lvItem
.pszText
= NULL
;
630 if (info
.m_mask
& wxLIST_MASK_DATA
)
631 lvItem
.mask
|= LVIF_PARAM
;
633 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
634 lvItem
.mask
|= LVIF_IMAGE
;
636 if ( info
.m_mask
& wxLIST_MASK_STATE
)
638 lvItem
.mask
|= LVIF_STATE
;
639 // the other bits are hardly interesting anyhow
640 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
643 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
646 wxLogError(_("Couldn't retrieve information about list control item %d."),
651 // give NULL as hwnd as we already have everything we need
652 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
656 delete[] lvItem
.pszText
;
661 // Sets information about the item
662 bool wxListCtrl::SetItem(wxListItem
& info
)
665 wxConvertToMSWListItem(this, info
, item
);
667 // we could be changing only the attribute in which case we don't need to
668 // call ListView_SetItem() at all
672 if ( !ListView_SetItem(GetHwnd(), &item
) )
674 wxLogDebug(_T("ListView_SetItem() failed"));
680 // we need to update the item immediately to show the new image
681 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
683 // check whether it has any custom attributes
684 if ( info
.HasAttributes() )
686 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
689 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
691 *attr
= *info
.GetAttributes();
695 // if the colour has changed, we must redraw the item
701 // we need this to make the change visible right now
702 RefreshItem(item
.iItem
);
708 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
712 info
.m_mask
= wxLIST_MASK_TEXT
;
713 info
.m_itemId
= index
;
717 info
.m_image
= imageId
;
718 info
.m_mask
|= wxLIST_MASK_IMAGE
;
720 return SetItem(info
);
724 // Gets the item state
725 int wxListCtrl::GetItemState(long item
, long stateMask
) const
729 info
.m_mask
= wxLIST_MASK_STATE
;
730 info
.m_stateMask
= stateMask
;
731 info
.m_itemId
= item
;
739 // Sets the item state
740 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
742 // NB: don't use SetItem() here as it doesn't work with the virtual list
745 wxZeroMemory(lvItem
);
747 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
749 // for the virtual list controls we need to refresh the previously focused
750 // item manually when changing focus without changing selection
751 // programmatically because otherwise it keeps its focus rectangle until
752 // next repaint (yet another comctl32 bug)
755 (stateMask
& wxLIST_STATE_FOCUSED
) &&
756 (state
& wxLIST_STATE_FOCUSED
) )
758 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
765 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
766 (WPARAM
)item
, (LPARAM
)&lvItem
) )
768 wxLogLastError(_T("ListView_SetItemState"));
773 if ( focusOld
!= -1 )
775 // no need to refresh the item if it was previously selected, it would
776 // only result in annoying flicker
777 if ( !(GetItemState(focusOld
,
778 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
780 RefreshItem(focusOld
);
787 // Sets the item image
788 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
792 info
.m_mask
= wxLIST_MASK_IMAGE
;
793 info
.m_image
= image
;
794 info
.m_itemId
= item
;
796 return SetItem(info
);
799 // Gets the item text
800 wxString
wxListCtrl::GetItemText(long item
) const
804 info
.m_mask
= wxLIST_MASK_TEXT
;
805 info
.m_itemId
= item
;
812 // Sets the item text
813 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
817 info
.m_mask
= wxLIST_MASK_TEXT
;
818 info
.m_itemId
= item
;
824 // Gets the item data
825 long wxListCtrl::GetItemData(long item
) const
829 info
.m_mask
= wxLIST_MASK_DATA
;
830 info
.m_itemId
= item
;
837 // Sets the item data
838 bool wxListCtrl::SetItemData(long item
, long data
)
842 info
.m_mask
= wxLIST_MASK_DATA
;
843 info
.m_itemId
= item
;
846 return SetItem(info
);
849 // Gets the item rectangle
850 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
855 if ( code
== wxLIST_RECT_BOUNDS
)
856 codeWin
= LVIR_BOUNDS
;
857 else if ( code
== wxLIST_RECT_ICON
)
859 else if ( code
== wxLIST_RECT_LABEL
)
860 codeWin
= LVIR_LABEL
;
863 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
865 codeWin
= LVIR_BOUNDS
;
869 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
) != 0;
871 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
874 rect
.x
= rectWin
.left
;
875 rect
.y
= rectWin
.top
;
876 rect
.width
= rectWin
.right
- rectWin
.left
;
877 rect
.height
= rectWin
.bottom
- rectWin
.top
;
882 // Gets the item position
883 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
887 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
889 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
893 // Sets the item position.
894 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
896 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
899 // Gets the number of items in the list control
900 int wxListCtrl::GetItemCount() const
902 return ListView_GetItemCount(GetHwnd());
905 // Retrieves the spacing between icons in pixels.
906 // If small is TRUE, gets the spacing for the small icon
907 // view, otherwise the large icon view.
908 int wxListCtrl::GetItemSpacing(bool isSmall
) const
910 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
913 // Gets the number of selected items in the list control
914 int wxListCtrl::GetSelectedItemCount() const
916 return ListView_GetSelectedCount(GetHwnd());
919 // Gets the text colour of the listview
920 wxColour
wxListCtrl::GetTextColour() const
922 COLORREF ref
= ListView_GetTextColor(GetHwnd());
923 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
927 // Sets the text colour of the listview
928 void wxListCtrl::SetTextColour(const wxColour
& col
)
930 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
933 // Gets the index of the topmost visible item when in
934 // list or report view
935 long wxListCtrl::GetTopItem() const
937 return (long) ListView_GetTopIndex(GetHwnd());
940 // Searches for an item, starting from 'item'.
941 // 'geometry' is one of
942 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
943 // 'state' is a state bit flag, one or more of
944 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
945 // item can be -1 to find the first item that matches the
947 // Returns the item or -1 if unsuccessful.
948 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
952 if ( geom
== wxLIST_NEXT_ABOVE
)
954 if ( geom
== wxLIST_NEXT_ALL
)
956 if ( geom
== wxLIST_NEXT_BELOW
)
958 if ( geom
== wxLIST_NEXT_LEFT
)
959 flags
|= LVNI_TOLEFT
;
960 if ( geom
== wxLIST_NEXT_RIGHT
)
961 flags
|= LVNI_TORIGHT
;
963 if ( state
& wxLIST_STATE_CUT
)
965 if ( state
& wxLIST_STATE_DROPHILITED
)
966 flags
|= LVNI_DROPHILITED
;
967 if ( state
& wxLIST_STATE_FOCUSED
)
968 flags
|= LVNI_FOCUSED
;
969 if ( state
& wxLIST_STATE_SELECTED
)
970 flags
|= LVNI_SELECTED
;
972 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
976 wxImageList
*wxListCtrl::GetImageList(int which
) const
978 if ( which
== wxIMAGE_LIST_NORMAL
)
980 return m_imageListNormal
;
982 else if ( which
== wxIMAGE_LIST_SMALL
)
984 return m_imageListSmall
;
986 else if ( which
== wxIMAGE_LIST_STATE
)
988 return m_imageListState
;
993 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
996 if ( which
== wxIMAGE_LIST_NORMAL
)
998 flags
= LVSIL_NORMAL
;
999 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1000 m_imageListNormal
= imageList
;
1001 m_ownsImageListNormal
= FALSE
;
1003 else if ( which
== wxIMAGE_LIST_SMALL
)
1005 flags
= LVSIL_SMALL
;
1006 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1007 m_imageListSmall
= imageList
;
1008 m_ownsImageListSmall
= FALSE
;
1010 else if ( which
== wxIMAGE_LIST_STATE
)
1012 flags
= LVSIL_STATE
;
1013 if (m_ownsImageListState
) delete m_imageListState
;
1014 m_imageListState
= imageList
;
1015 m_ownsImageListState
= FALSE
;
1017 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1020 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1022 SetImageList(imageList
, which
);
1023 if ( which
== wxIMAGE_LIST_NORMAL
)
1024 m_ownsImageListNormal
= TRUE
;
1025 else if ( which
== wxIMAGE_LIST_SMALL
)
1026 m_ownsImageListSmall
= TRUE
;
1027 else if ( which
== wxIMAGE_LIST_STATE
)
1028 m_ownsImageListState
= TRUE
;
1031 // ----------------------------------------------------------------------------
1033 // ----------------------------------------------------------------------------
1035 // Arranges the items
1036 bool wxListCtrl::Arrange(int flag
)
1039 if ( flag
== wxLIST_ALIGN_LEFT
)
1040 code
= LVA_ALIGNLEFT
;
1041 else if ( flag
== wxLIST_ALIGN_TOP
)
1042 code
= LVA_ALIGNTOP
;
1043 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1045 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1046 code
= LVA_SNAPTOGRID
;
1048 return (ListView_Arrange(GetHwnd(), code
) != 0);
1052 bool wxListCtrl::DeleteItem(long item
)
1054 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1056 wxLogLastError(_T("ListView_DeleteItem"));
1060 // the virtual list control doesn't refresh itself correctly, help it
1063 // we need to refresh all the lines below the one which was deleted
1065 if ( item
> 0 && GetItemCount() )
1067 GetItemRect(item
- 1, rectItem
);
1072 rectItem
.height
= 0;
1075 wxRect rectWin
= GetRect();
1076 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1077 rectWin
.y
= rectItem
.GetBottom();
1079 RefreshRect(rectWin
);
1085 // Deletes all items
1086 bool wxListCtrl::DeleteAllItems()
1088 return ListView_DeleteAllItems(GetHwnd()) != 0;
1091 // Deletes all items
1092 bool wxListCtrl::DeleteAllColumns()
1094 while ( m_colCount
> 0 )
1096 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1098 wxLogLastError(wxT("ListView_DeleteColumn"));
1106 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1112 bool wxListCtrl::DeleteColumn(int col
)
1114 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1116 if ( success
&& (m_colCount
> 0) )
1121 // Clears items, and columns if there are any.
1122 void wxListCtrl::ClearAll()
1125 if ( m_colCount
> 0 )
1129 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1131 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1133 // VS: ListView_EditLabel requires that the list has focus.
1135 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1139 m_textCtrl
->SetHWND(0);
1140 m_textCtrl
->UnsubclassWin();
1145 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1146 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1147 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1152 // End label editing, optionally cancelling the edit
1153 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1155 wxFAIL_MSG( _T("not implemented") );
1160 // Ensures this item is visible
1161 bool wxListCtrl::EnsureVisible(long item
)
1163 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1166 // Find an item whose label matches this string, starting from the item after 'start'
1167 // or the beginning if 'start' is -1.
1168 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1170 LV_FINDINFO findInfo
;
1172 findInfo
.flags
= LVFI_STRING
;
1174 findInfo
.flags
|= LVFI_PARTIAL
;
1177 // ListView_FindItem() excludes the first item from search and to look
1178 // through all the items you need to start from -1 which is unnatural and
1179 // inconsistent with the generic version - so we adjust the index
1182 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1185 // Find an item whose data matches this data, starting from the item after 'start'
1186 // or the beginning if 'start' is -1.
1187 long wxListCtrl::FindItem(long start
, long data
)
1189 LV_FINDINFO findInfo
;
1191 findInfo
.flags
= LVFI_PARAM
;
1192 findInfo
.lParam
= data
;
1194 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1197 // Find an item nearest this position in the specified direction, starting from
1198 // the item after 'start' or the beginning if 'start' is -1.
1199 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1201 LV_FINDINFO findInfo
;
1203 findInfo
.flags
= LVFI_NEARESTXY
;
1204 findInfo
.pt
.x
= pt
.x
;
1205 findInfo
.pt
.y
= pt
.y
;
1206 findInfo
.vkDirection
= VK_RIGHT
;
1208 if ( direction
== wxLIST_FIND_UP
)
1209 findInfo
.vkDirection
= VK_UP
;
1210 else if ( direction
== wxLIST_FIND_DOWN
)
1211 findInfo
.vkDirection
= VK_DOWN
;
1212 else if ( direction
== wxLIST_FIND_LEFT
)
1213 findInfo
.vkDirection
= VK_LEFT
;
1214 else if ( direction
== wxLIST_FIND_RIGHT
)
1215 findInfo
.vkDirection
= VK_RIGHT
;
1217 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1220 // Determines which item (if any) is at the specified point,
1221 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1222 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1224 LV_HITTESTINFO hitTestInfo
;
1225 hitTestInfo
.pt
.x
= (int) point
.x
;
1226 hitTestInfo
.pt
.y
= (int) point
.y
;
1228 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1231 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1232 flags
|= wxLIST_HITTEST_ABOVE
;
1233 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1234 flags
|= wxLIST_HITTEST_BELOW
;
1235 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1236 flags
|= wxLIST_HITTEST_NOWHERE
;
1237 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1238 flags
|= wxLIST_HITTEST_ONITEMICON
;
1239 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1240 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1241 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1242 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1243 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1244 flags
|= wxLIST_HITTEST_TOLEFT
;
1245 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1246 flags
|= wxLIST_HITTEST_TORIGHT
;
1248 return (long) hitTestInfo
.iItem
;
1251 // Inserts an item, returning the index of the new item if successful,
1253 long wxListCtrl::InsertItem(wxListItem
& info
)
1255 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1258 wxConvertToMSWListItem(this, info
, item
);
1260 // check whether it has any custom attributes
1261 if ( info
.HasAttributes() )
1264 wxListItemAttr
*attr
;
1265 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1269 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1271 else *attr
= *info
.GetAttributes();
1273 m_hasAnyAttr
= TRUE
;
1276 return (long) ListView_InsertItem(GetHwnd(), & item
);
1279 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1282 info
.m_text
= label
;
1283 info
.m_mask
= wxLIST_MASK_TEXT
;
1284 info
.m_itemId
= index
;
1285 return InsertItem(info
);
1288 // Inserts an image item
1289 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1292 info
.m_image
= imageIndex
;
1293 info
.m_mask
= wxLIST_MASK_IMAGE
;
1294 info
.m_itemId
= index
;
1295 return InsertItem(info
);
1298 // Inserts an image/string item
1299 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1302 info
.m_image
= imageIndex
;
1303 info
.m_text
= label
;
1304 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1305 info
.m_itemId
= index
;
1306 return InsertItem(info
);
1309 // For list view mode (only), inserts a column.
1310 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1313 wxConvertToMSWListCol(col
, item
, lvCol
);
1315 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1317 // always give some width to the new column: this one is compatible
1318 // with the generic version
1319 lvCol
.mask
|= LVCF_WIDTH
;
1323 // when we insert a column which can contain an image, we must specify this
1324 // flag right now as doing it later in SetColumn() has no effect
1326 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1327 // way to dynamically set/clear the bitmap as the column without a bitmap
1328 // on the left looks ugly (there is a hole)
1330 // unfortunately with my version of comctl32.dll (5.80), the left column
1331 // image is always on the left and it seems that it's a "feature" - I
1332 // didn't find any way to work around it in any case
1333 if ( lvCol
.mask
& LVCF_IMAGE
)
1335 lvCol
.mask
|= LVCF_FMT
;
1336 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1339 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1346 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1353 long wxListCtrl::InsertColumn(long col
,
1354 const wxString
& heading
,
1359 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1360 item
.m_text
= heading
;
1363 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1364 item
.m_width
= width
;
1366 item
.m_format
= format
;
1368 return InsertColumn(col
, item
);
1371 // Scrolls the list control. If in icon, small icon or report view mode,
1372 // x specifies the number of pixels to scroll. If in list view mode, x
1373 // specifies the number of columns to scroll.
1374 // If in icon, small icon or list view mode, y specifies the number of pixels
1375 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1376 bool wxListCtrl::ScrollList(int dx
, int dy
)
1378 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1383 // fn is a function which takes 3 long arguments: item1, item2, data.
1384 // item1 is the long data associated with a first item (NOT the index).
1385 // item2 is the long data associated with a second item (NOT the index).
1386 // data is the same value as passed to SortItems.
1387 // The return value is a negative number if the first item should precede the second
1388 // item, a positive number of the second item should precede the first,
1389 // or zero if the two items are equivalent.
1391 // data is arbitrary data to be passed to the sort function.
1393 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1394 // time for anything better right now (VZ)
1395 static long gs_sortData
= 0;
1396 static wxListCtrl
*gs_sortCtrl
= NULL
;
1397 static wxListCtrlCompare gs_sortFunction
= NULL
;
1399 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1401 int n1
= *(const int *)arg1
,
1402 n2
= *(const int *)arg2
;
1404 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1405 gs_sortCtrl
->GetItemData(n2
),
1409 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1411 // sort the attributes too
1415 count
= GetItemCount();
1416 int *aItems
= new int[count
];
1417 for ( n
= 0; n
< count
; n
++ )
1424 gs_sortFunction
= fn
;
1426 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1430 gs_sortFunction
= NULL
;
1432 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1433 for ( n
= 0; n
< count
; n
++ )
1435 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1438 attrsNew
.Put(n
, attr
);
1448 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1450 wxLogDebug(_T("ListView_SortItems() failed"));
1460 // ----------------------------------------------------------------------------
1461 // message processing
1462 // ----------------------------------------------------------------------------
1464 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1466 if (cmd
== EN_UPDATE
)
1468 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1469 event
.SetEventObject( this );
1470 ProcessCommand(event
);
1473 else if (cmd
== EN_KILLFOCUS
)
1475 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1476 event
.SetEventObject( this );
1477 ProcessCommand(event
);
1484 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1486 // prepare the event
1487 // -----------------
1489 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1490 event
.SetEventObject(this);
1492 wxEventType eventType
= wxEVT_NULL
;
1494 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1496 // if your compiler is as broken as this, you should really change it: this
1497 // code is needed for normal operation! #ifdef below is only useful for
1498 // automatic rebuilds which are done with a very old compiler version
1499 #ifdef HDN_BEGINTRACKA
1501 // check for messages from the header (in report view)
1502 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1504 // is it a message from the header?
1505 if ( nmhdr
->hwndFrom
== hwndHdr
)
1507 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1509 event
.m_itemIndex
= -1;
1511 switch ( nmhdr
->code
)
1513 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1514 // TRACK messages even to ANSI programs: on my system I get
1515 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1517 // work around is to simply catch both versions and hope that it
1518 // works (why should this message exist in ANSI and Unicode is
1519 // beyond me as it doesn't deal with strings at all...)
1520 case HDN_BEGINTRACKA
:
1521 case HDN_BEGINTRACKW
:
1522 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1527 if ( eventType
== wxEVT_NULL
)
1528 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1533 if ( eventType
== wxEVT_NULL
)
1534 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1535 event
.m_col
= nmHDR
->iItem
;
1540 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1543 // find the column clicked: we have to search for it
1544 // ourselves as the notification message doesn't provide
1547 // where did the click occur?
1549 if ( !::GetCursorPos(&ptClick
) )
1551 wxLogLastError(_T("GetCursorPos"));
1554 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1556 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1559 event
.m_pointDrag
.x
= ptClick
.x
;
1560 event
.m_pointDrag
.y
= ptClick
.y
;
1562 int colCount
= Header_GetItemCount(hwndHdr
);
1565 for ( int col
= 0; col
< colCount
; col
++ )
1567 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1569 if ( ::PtInRect(&rect
, ptClick
) )
1580 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1584 #endif // defined(HDN_BEGINTRACKA)
1585 if ( nmhdr
->hwndFrom
== GetHwnd() )
1587 // almost all messages use NM_LISTVIEW
1588 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1590 // this is true for almost all events
1591 event
.m_item
.m_data
= nmLV
->lParam
;
1593 switch ( nmhdr
->code
)
1595 case LVN_BEGINRDRAG
:
1596 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1600 if ( eventType
== wxEVT_NULL
)
1602 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1605 event
.m_itemIndex
= nmLV
->iItem
;
1606 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1607 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1610 // NB: we have to handle both *A and *W versions here because some
1611 // versions of comctl32.dll send ANSI message to an Unicode app
1612 case LVN_BEGINLABELEDITA
:
1614 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1615 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1616 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1617 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1620 case LVN_BEGINLABELEDITW
:
1622 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1623 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1624 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1625 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1629 case LVN_ENDLABELEDITA
:
1631 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1632 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1633 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1634 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1635 ((LV_ITEM
)item
).iItem
== -1 )
1638 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1641 case LVN_ENDLABELEDITW
:
1643 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1644 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1645 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1646 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1647 ((LV_ITEM
)item
).iItem
== -1 )
1650 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1654 case LVN_COLUMNCLICK
:
1655 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1656 event
.m_itemIndex
= -1;
1657 event
.m_col
= nmLV
->iSubItem
;
1660 case LVN_DELETEALLITEMS
:
1661 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1662 event
.m_itemIndex
= -1;
1668 case LVN_DELETEITEM
:
1669 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1670 event
.m_itemIndex
= nmLV
->iItem
;
1674 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1678 case LVN_SETDISPINFO
:
1680 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1681 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1682 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1686 case LVN_INSERTITEM
:
1687 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1688 event
.m_itemIndex
= nmLV
->iItem
;
1691 case LVN_ITEMCHANGED
:
1692 // we translate this catch all message into more interesting
1693 // (and more easy to process) wxWindows events
1695 // first of all, we deal with the state change events only
1696 if ( nmLV
->uChanged
& LVIF_STATE
)
1698 // temp vars for readability
1699 const UINT stOld
= nmLV
->uOldState
;
1700 const UINT stNew
= nmLV
->uNewState
;
1702 // has the focus changed?
1703 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1705 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1706 event
.m_itemIndex
= nmLV
->iItem
;
1709 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1711 if ( eventType
!= wxEVT_NULL
)
1713 // focus and selection have both changed: send the
1714 // focus event from here and the selection one
1716 event
.SetEventType(eventType
);
1717 (void)GetEventHandler()->ProcessEvent(event
);
1719 else // no focus event to send
1721 // then need to set m_itemIndex as it wasn't done
1723 event
.m_itemIndex
= nmLV
->iItem
;
1726 eventType
= stNew
& LVIS_SELECTED
1727 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1728 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1732 if ( eventType
== wxEVT_NULL
)
1734 // not an interesting event for us
1742 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1743 WORD wVKey
= info
->wVKey
;
1745 // get the current selection
1746 long lItem
= GetNextItem(-1,
1748 wxLIST_STATE_SELECTED
);
1750 // <Enter> or <Space> activate the selected item if any (but
1751 // not with Shift and/or Ctrl as then they have a predefined
1752 // meaning for the list view)
1754 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1755 !(wxIsShiftDown() || wxIsCtrlDown()) )
1757 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1761 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1763 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1764 // value which should be used as is
1765 int code
= wxCharCodeMSWToWX(wVKey
);
1766 event
.m_code
= code
? code
: wVKey
;
1770 event
.m_item
.m_itemId
= lItem
;
1774 // fill the other fields too
1775 event
.m_item
.m_text
= GetItemText(lItem
);
1776 event
.m_item
.m_data
= GetItemData(lItem
);
1782 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1784 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1789 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1790 // if it happened on an item (and not on empty place)
1791 if ( nmLV
->iItem
== -1 )
1797 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1798 event
.m_itemIndex
= nmLV
->iItem
;
1799 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1800 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1804 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1805 // don't do anything else
1806 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1811 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1812 LV_HITTESTINFO lvhti
;
1813 wxZeroMemory(lvhti
);
1815 ::GetCursorPos(&(lvhti
.pt
));
1816 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1817 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1819 if ( lvhti
.flags
& LVHT_ONITEM
)
1821 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1822 event
.m_itemIndex
= lvhti
.iItem
;
1823 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1824 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1829 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1830 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1832 *result
= OnCustomDraw(lParam
);
1835 #endif // _WIN32_IE >= 0x300
1837 case LVN_ODCACHEHINT
:
1839 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1841 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1843 // we get some really stupid cache hints like ones for items in
1844 // range 0..0 for an empty control or, after deleting an item,
1845 // for items in invalid range - filter this garbage out
1846 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1848 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1850 long iMax
= GetItemCount();
1851 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1861 case LVN_GETDISPINFO
:
1864 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1866 LV_ITEM
& lvi
= info
->item
;
1867 long item
= lvi
.iItem
;
1869 if ( lvi
.mask
& LVIF_TEXT
)
1871 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1872 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1875 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1876 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
1877 if ( lvi
.mask
& LVIF_IMAGE
)
1879 lvi
.iImage
= OnGetItemImage(item
);
1883 // a little dose of healthy paranoia: as we never use
1884 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1885 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1886 _T("we don't support state callbacks yet!") );
1893 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1898 // where did this one come from?
1902 // process the event
1903 // -----------------
1905 event
.SetEventType(eventType
);
1907 if ( !GetEventHandler()->ProcessEvent(event
) )
1913 switch ( nmhdr
->code
)
1915 case LVN_DELETEALLITEMS
:
1916 // always return TRUE to suppress all additional LVN_DELETEITEM
1917 // notifications - this makes deleting all items from a list ctrl
1923 case LVN_ENDLABELEDITA
:
1924 case LVN_ENDLABELEDITW
:
1925 // logic here is inversed compared to all the other messages
1926 *result
= event
.IsAllowed();
1931 *result
= !event
.IsAllowed();
1936 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1938 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1940 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1941 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1942 switch ( nmcd
.dwDrawStage
)
1945 // if we've got any items with non standard attributes,
1946 // notify us before painting each item
1948 // for virtual controls, always suppose that we have attributes as
1949 // there is no way to check for this
1950 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1953 case CDDS_ITEMPREPAINT
:
1955 size_t item
= (size_t)nmcd
.dwItemSpec
;
1956 if ( item
>= (size_t)GetItemCount() )
1958 // we get this message with item == 0 for an empty control,
1959 // we must ignore it as calling OnGetItemAttr() would be
1961 return CDRF_DODEFAULT
;
1964 wxListItemAttr
*attr
=
1965 IsVirtual() ? OnGetItemAttr(item
)
1966 : (wxListItemAttr
*)m_attrs
.Get(item
);
1970 // nothing to do for this item
1971 return CDRF_DODEFAULT
;
1975 wxColour colText
, colBack
;
1976 if ( attr
->HasFont() )
1978 wxFont font
= attr
->GetFont();
1979 hFont
= (HFONT
)font
.GetResourceHandle();
1986 if ( attr
->HasTextColour() )
1988 colText
= attr
->GetTextColour();
1992 colText
= GetTextColour();
1995 if ( attr
->HasBackgroundColour() )
1997 colBack
= attr
->GetBackgroundColour();
2001 colBack
= GetBackgroundColour();
2004 lplvcd
->clrText
= wxColourToRGB(colText
);
2005 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2007 // note that if we wanted to set colours for
2008 // individual columns (subitems), we would have
2009 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2012 ::SelectObject(nmcd
.hdc
, hFont
);
2014 return CDRF_NEWFONT
;
2017 // fall through to return CDRF_DODEFAULT
2020 return CDRF_DODEFAULT
;
2024 #endif // NM_CUSTOMDRAW supported
2026 // Necessary for drawing hrules and vrules, if specified
2027 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2031 wxControl::OnPaint(event
);
2033 // Reset the device origin since it may have been set
2034 dc
.SetDeviceOrigin(0, 0);
2036 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2037 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2039 if (!drawHRules
&& !drawVRules
)
2041 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2044 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2046 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2048 wxSize clientSize
= GetClientSize();
2052 int itemCount
= GetItemCount();
2056 long top
= GetTopItem();
2057 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2059 if (GetItemRect(i
, itemRect
))
2061 cy
= itemRect
.GetTop();
2062 if (i
!= 0) // Don't draw the first one
2064 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2067 if (i
== itemCount
- 1)
2069 cy
= itemRect
.GetBottom();
2070 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2076 if (drawVRules
&& (i
> -1))
2078 wxRect firstItemRect
;
2079 GetItemRect(0, firstItemRect
);
2081 if (GetItemRect(i
, itemRect
))
2084 int x
= itemRect
.GetX();
2085 for (col
= 0; col
< GetColumnCount(); col
++)
2087 int colWidth
= GetColumnWidth(col
);
2089 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
2095 // ----------------------------------------------------------------------------
2096 // virtual list controls
2097 // ----------------------------------------------------------------------------
2099 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2101 // this is a pure virtual function, in fact - which is not really pure
2102 // because the controls which are not virtual don't need to implement it
2103 wxFAIL_MSG( _T("not supposed to be called") );
2105 return wxEmptyString
;
2108 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2111 wxFAIL_MSG( _T("not supposed to be called") );
2116 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2118 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2119 _T("invalid item index in OnGetItemAttr()") );
2121 // no attributes by default
2125 void wxListCtrl::SetItemCount(long count
)
2127 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2129 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2131 wxLogLastError(_T("ListView_SetItemCount"));
2135 void wxListCtrl::RefreshItem(long item
)
2137 // strangely enough, ListView_Update() results in much more flicker here
2138 // than a dumb Refresh() -- why?
2140 if ( !ListView_Update(GetHwnd(), item
) )
2142 wxLogLastError(_T("ListView_Update"));
2146 GetItemRect(item
, rect
);
2151 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2153 wxRect rect1
, rect2
;
2154 GetItemRect(itemFrom
, rect1
);
2155 GetItemRect(itemTo
, rect2
);
2157 wxRect rect
= rect1
;
2158 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2163 // ----------------------------------------------------------------------------
2165 // ----------------------------------------------------------------------------
2167 // List item structure
2168 wxListItem::wxListItem()
2178 m_format
= wxLIST_FORMAT_CENTRE
;
2184 void wxListItem::Clear()
2193 m_format
= wxLIST_FORMAT_CENTRE
;
2195 m_text
= wxEmptyString
;
2197 if (m_attr
) delete m_attr
;
2201 void wxListItem::ClearAttributes()
2203 if (m_attr
) delete m_attr
;
2207 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2211 info
.m_data
= lvItem
.lParam
;
2214 info
.m_stateMask
= 0;
2215 info
.m_itemId
= lvItem
.iItem
;
2217 long oldMask
= lvItem
.mask
;
2219 bool needText
= FALSE
;
2220 if (hwndListCtrl
!= 0)
2222 if ( lvItem
.mask
& LVIF_TEXT
)
2229 lvItem
.pszText
= new wxChar
[513];
2230 lvItem
.cchTextMax
= 512;
2232 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2233 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2236 if ( lvItem
.mask
& LVIF_STATE
)
2238 info
.m_mask
|= wxLIST_MASK_STATE
;
2240 if ( lvItem
.stateMask
& LVIS_CUT
)
2242 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2243 if ( lvItem
.state
& LVIS_CUT
)
2244 info
.m_state
|= wxLIST_STATE_CUT
;
2246 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2248 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2249 if ( lvItem
.state
& LVIS_DROPHILITED
)
2250 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2252 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2254 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2255 if ( lvItem
.state
& LVIS_FOCUSED
)
2256 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2258 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2260 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2261 if ( lvItem
.state
& LVIS_SELECTED
)
2262 info
.m_state
|= wxLIST_STATE_SELECTED
;
2266 if ( lvItem
.mask
& LVIF_TEXT
)
2268 info
.m_mask
|= wxLIST_MASK_TEXT
;
2269 info
.m_text
= lvItem
.pszText
;
2271 if ( lvItem
.mask
& LVIF_IMAGE
)
2273 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2274 info
.m_image
= lvItem
.iImage
;
2276 if ( lvItem
.mask
& LVIF_PARAM
)
2277 info
.m_mask
|= wxLIST_MASK_DATA
;
2278 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2279 info
.m_mask
|= wxLIST_SET_ITEM
;
2280 info
.m_col
= lvItem
.iSubItem
;
2285 delete[] lvItem
.pszText
;
2287 lvItem
.mask
= oldMask
;
2290 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2292 if (stateMask
& wxLIST_STATE_CUT
)
2294 lvItem
.stateMask
|= LVIS_CUT
;
2295 if (state
& wxLIST_STATE_CUT
)
2296 lvItem
.state
|= LVIS_CUT
;
2298 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2300 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2301 if (state
& wxLIST_STATE_DROPHILITED
)
2302 lvItem
.state
|= LVIS_DROPHILITED
;
2304 if (stateMask
& wxLIST_STATE_FOCUSED
)
2306 lvItem
.stateMask
|= LVIS_FOCUSED
;
2307 if (state
& wxLIST_STATE_FOCUSED
)
2308 lvItem
.state
|= LVIS_FOCUSED
;
2310 if (stateMask
& wxLIST_STATE_SELECTED
)
2312 lvItem
.stateMask
|= LVIS_SELECTED
;
2313 if (state
& wxLIST_STATE_SELECTED
)
2314 lvItem
.state
|= LVIS_SELECTED
;
2318 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2319 const wxListItem
& info
,
2322 lvItem
.iItem
= (int) info
.m_itemId
;
2324 lvItem
.iImage
= info
.m_image
;
2325 lvItem
.lParam
= info
.m_data
;
2326 lvItem
.stateMask
= 0;
2329 lvItem
.iSubItem
= info
.m_col
;
2331 if (info
.m_mask
& wxLIST_MASK_STATE
)
2333 lvItem
.mask
|= LVIF_STATE
;
2335 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2338 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2340 lvItem
.mask
|= LVIF_TEXT
;
2341 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2343 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2347 // pszText is not const, hence the cast
2348 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2349 if ( lvItem
.pszText
)
2350 lvItem
.cchTextMax
= info
.m_text
.Length();
2352 lvItem
.cchTextMax
= 0;
2355 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2356 lvItem
.mask
|= LVIF_IMAGE
;
2357 if (info
.m_mask
& wxLIST_MASK_DATA
)
2358 lvItem
.mask
|= LVIF_PARAM
;
2361 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2364 wxZeroMemory(lvCol
);
2366 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2368 lvCol
.mask
|= LVCF_TEXT
;
2369 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2372 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2374 lvCol
.mask
|= LVCF_FMT
;
2376 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2377 lvCol
.fmt
= LVCFMT_LEFT
;
2378 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2379 lvCol
.fmt
= LVCFMT_RIGHT
;
2380 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2381 lvCol
.fmt
= LVCFMT_CENTER
;
2384 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2386 lvCol
.mask
|= LVCF_WIDTH
;
2387 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2388 lvCol
.cx
= LVSCW_AUTOSIZE
;
2389 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2390 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2392 lvCol
.cx
= item
.m_width
;
2395 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2396 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2397 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2399 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2401 lvCol
.mask
|= LVCF_IMAGE
;
2402 lvCol
.iImage
= item
.m_image
;
2404 //else: it doesn't support item images anyhow
2409 #endif // wxUSE_LISTCTRL