1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
38 #include "wx/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
46 #include "wx/msw/private.h"
48 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
49 #include "wx/msw/gnuwin32/extra.h"
56 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
60 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
63 #ifndef LVS_EX_FULLROWSELECT
64 #define LVS_EX_FULLROWSELECT 0x00000020
68 #define LVS_OWNERDATA 0x1000
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // convert our state and mask flags to LV_ITEM constants
76 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
78 // convert wxListItem to LV_ITEM
79 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
80 const wxListItem
& info
, LV_ITEM
& lvItem
);
82 // convert LV_ITEM to wxListItem
83 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
85 /* const */ LV_ITEM
& lvItem
);
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
104 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
105 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
108 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
109 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
111 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
112 EVT_PAINT(wxListCtrl::OnPaint
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxListEvent::CopyObject(wxObject
& object_dest
) const
125 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
127 wxNotifyEvent::CopyObject(object_dest
);
129 obj
->m_code
= m_code
;
130 obj
->m_itemIndex
= m_itemIndex
;
131 obj
->m_oldItemIndex
= m_oldItemIndex
;
133 obj
->m_cancelled
= m_cancelled
;
134 obj
->m_pointDrag
= m_pointDrag
;
135 obj
->m_item
.m_mask
= m_item
.m_mask
;
136 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
137 obj
->m_item
.m_col
= m_item
.m_col
;
138 obj
->m_item
.m_state
= m_item
.m_state
;
139 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
140 obj
->m_item
.m_text
= m_item
.m_text
;
141 obj
->m_item
.m_image
= m_item
.m_image
;
142 obj
->m_item
.m_data
= m_item
.m_data
;
143 obj
->m_item
.m_format
= m_item
.m_format
;
144 obj
->m_item
.m_width
= m_item
.m_width
;
146 if ( m_item
.HasAttributes() )
148 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
149 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
150 obj
->m_item
.SetFont(m_item
.GetFont());
154 // ----------------------------------------------------------------------------
155 // wxListCtrl construction
156 // ----------------------------------------------------------------------------
158 void wxListCtrl::Init()
160 m_imageListNormal
= NULL
;
161 m_imageListSmall
= NULL
;
162 m_imageListState
= NULL
;
163 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
167 m_hasAnyAttr
= FALSE
;
170 bool wxListCtrl::Create(wxWindow
*parent
,
175 const wxValidator
& validator
,
176 const wxString
& name
)
179 SetValidator(validator
);
180 #endif // wxUSE_VALIDATORS
189 m_windowStyle
= style
;
202 m_windowId
= (id
== -1) ? NewControlId() : id
;
204 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
205 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
207 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
208 wstyle
|= WS_CLIPSIBLINGS
;
210 if ( wxStyleHasBorder(m_windowStyle
) )
212 m_baseStyle
= wstyle
;
214 if ( !DoCreateControl(x
, y
, width
, height
) )
218 parent
->AddChild(this);
223 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
225 DWORD wstyle
= m_baseStyle
;
228 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
230 // Even with extended styles, need to combine with WS_BORDER
231 // for them to look right.
235 long oldStyle
= 0; // Dummy
236 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
238 // Create the ListView control.
239 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
244 GetWinHwnd(GetParent()),
251 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
256 // for comctl32.dll v 4.70+ we want to have this attribute because it's
257 // prettier (and also because wxGTK does it like this)
258 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
260 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
261 0, LVS_EX_FULLROWSELECT
);
264 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
265 SetForegroundColour(GetParent()->GetForegroundColour());
272 void wxListCtrl::UpdateStyle()
276 // The new window view style
278 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
279 dwStyleNew
|= m_baseStyle
;
281 // Get the current window style.
282 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
284 // Only set the window style if the view bits have changed.
285 if ( dwStyleOld
!= dwStyleNew
)
287 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
292 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
296 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
298 delete (wxListItemAttr
*)node
->Data();
304 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
307 m_hasAnyAttr
= FALSE
;
311 wxListCtrl::~wxListCtrl()
313 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
317 m_textCtrl
->SetHWND(0);
318 m_textCtrl
->UnsubclassWin();
323 if (m_ownsImageListNormal
) delete m_imageListNormal
;
324 if (m_ownsImageListSmall
) delete m_imageListSmall
;
325 if (m_ownsImageListState
) delete m_imageListState
;
328 // ----------------------------------------------------------------------------
329 // set/get/change style
330 // ----------------------------------------------------------------------------
332 // Add or remove a single window style
333 void wxListCtrl::SetSingleStyle(long style
, bool add
)
335 long flag
= GetWindowStyleFlag();
337 // Get rid of conflicting styles
340 if ( style
& wxLC_MASK_TYPE
)
341 flag
= flag
& ~wxLC_MASK_TYPE
;
342 if ( style
& wxLC_MASK_ALIGN
)
343 flag
= flag
& ~wxLC_MASK_ALIGN
;
344 if ( style
& wxLC_MASK_SORT
)
345 flag
= flag
& ~wxLC_MASK_SORT
;
361 m_windowStyle
= flag
;
366 // Set the whole window style
367 void wxListCtrl::SetWindowStyleFlag(long flag
)
369 m_windowStyle
= flag
;
374 // Can be just a single style, or a bitlist
375 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
378 if ( style
& wxLC_ICON
)
380 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
381 oldStyle
-= LVS_SMALLICON
;
382 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
383 oldStyle
-= LVS_REPORT
;
384 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
385 oldStyle
-= LVS_LIST
;
389 if ( style
& wxLC_SMALL_ICON
)
391 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
392 oldStyle
-= LVS_ICON
;
393 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
394 oldStyle
-= LVS_REPORT
;
395 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
396 oldStyle
-= LVS_LIST
;
397 wstyle
|= LVS_SMALLICON
;
400 if ( style
& wxLC_LIST
)
402 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
403 oldStyle
-= LVS_ICON
;
404 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
405 oldStyle
-= LVS_REPORT
;
406 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
407 oldStyle
-= LVS_SMALLICON
;
411 if ( style
& wxLC_REPORT
)
413 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
414 oldStyle
-= LVS_ICON
;
415 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
416 oldStyle
-= LVS_LIST
;
417 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
418 oldStyle
-= LVS_SMALLICON
;
420 wstyle
|= LVS_REPORT
;
423 if ( style
& wxLC_ALIGN_LEFT
)
425 if ( oldStyle
& LVS_ALIGNTOP
)
426 oldStyle
-= LVS_ALIGNTOP
;
427 wstyle
|= LVS_ALIGNLEFT
;
430 if ( style
& wxLC_ALIGN_TOP
)
432 if ( oldStyle
& LVS_ALIGNLEFT
)
433 oldStyle
-= LVS_ALIGNLEFT
;
434 wstyle
|= LVS_ALIGNTOP
;
437 if ( style
& wxLC_AUTOARRANGE
)
438 wstyle
|= LVS_AUTOARRANGE
;
440 if ( style
& wxLC_NO_SORT_HEADER
)
441 wstyle
|= LVS_NOSORTHEADER
;
443 if ( style
& wxLC_NO_HEADER
)
444 wstyle
|= LVS_NOCOLUMNHEADER
;
446 if ( style
& wxLC_EDIT_LABELS
)
447 wstyle
|= LVS_EDITLABELS
;
449 if ( style
& wxLC_SINGLE_SEL
)
450 wstyle
|= LVS_SINGLESEL
;
452 if ( style
& wxLC_SORT_ASCENDING
)
454 if ( oldStyle
& LVS_SORTDESCENDING
)
455 oldStyle
-= LVS_SORTDESCENDING
;
456 wstyle
|= LVS_SORTASCENDING
;
459 if ( style
& wxLC_SORT_DESCENDING
)
461 if ( oldStyle
& LVS_SORTASCENDING
)
462 oldStyle
-= LVS_SORTASCENDING
;
463 wstyle
|= LVS_SORTDESCENDING
;
466 if ( style
& wxLC_VIRTUAL
)
468 int ver
= wxTheApp
->GetComCtl32Version();
471 wxLogWarning(_("Please install a newer version of comctl32.dll\n"
472 "(at least version 4.70 is required but you have "
474 "or this program won't operate correctly."),
475 ver
/ 100, ver
% 100);
478 wstyle
|= LVS_OWNERDATA
;
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 // Sets the foreground, i.e. text, colour
489 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
491 if ( !wxWindow::SetForegroundColour(col
) )
494 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
499 // Sets the background colour
500 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
502 if ( !wxWindow::SetBackgroundColour(col
) )
505 // we set the same colour for both the "empty" background and the items
507 COLORREF color
= wxColourToRGB(col
);
508 ListView_SetBkColor(GetHwnd(), color
);
509 ListView_SetTextBkColor(GetHwnd(), color
);
514 // Gets information about this column
515 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
520 lvCol
.pszText
= NULL
;
522 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
524 lvCol
.mask
|= LVCF_TEXT
;
525 lvCol
.pszText
= new wxChar
[513];
526 lvCol
.cchTextMax
= 512;
529 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
531 // item.m_subItem = lvCol.iSubItem;
532 item
.m_width
= lvCol
.cx
;
534 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
536 item
.m_text
= lvCol
.pszText
;
537 delete[] lvCol
.pszText
;
540 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
542 if (lvCol
.fmt
== LVCFMT_LEFT
)
543 item
.m_format
= wxLIST_FORMAT_LEFT
;
544 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
545 item
.m_format
= wxLIST_FORMAT_RIGHT
;
546 else if (lvCol
.fmt
== LVCFMT_CENTER
)
547 item
.m_format
= wxLIST_FORMAT_CENTRE
;
553 // Sets information about this column
554 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
559 lvCol
.pszText
= NULL
;
561 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
563 lvCol
.mask
|= LVCF_TEXT
;
564 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
565 lvCol
.cchTextMax
= 0; // Ignored
567 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
569 lvCol
.mask
|= LVCF_FMT
;
571 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
572 lvCol
.fmt
= LVCFMT_LEFT
;
573 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
574 lvCol
.fmt
= LVCFMT_RIGHT
;
575 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
576 lvCol
.fmt
= LVCFMT_CENTER
;
579 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
581 lvCol
.mask
|= LVCF_WIDTH
;
582 lvCol
.cx
= item
.m_width
;
584 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
585 lvCol
.cx
= LVSCW_AUTOSIZE
;
586 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
587 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
589 lvCol
.mask
|= LVCF_SUBITEM
;
590 lvCol
.iSubItem
= col
;
591 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
594 // Gets the column width
595 int wxListCtrl::GetColumnWidth(int col
) const
597 return ListView_GetColumnWidth(GetHwnd(), col
);
600 // Sets the column width
601 bool wxListCtrl::SetColumnWidth(int col
, int width
)
604 if ( m_windowStyle
& wxLC_LIST
)
608 if ( width2
== wxLIST_AUTOSIZE
)
609 width2
= LVSCW_AUTOSIZE
;
610 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
611 width2
= LVSCW_AUTOSIZE_USEHEADER
;
613 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
616 // Gets the number of items that can fit vertically in the
617 // visible area of the list control (list or report view)
618 // or the total number of items in the list control (icon
619 // or small icon view)
620 int wxListCtrl::GetCountPerPage() const
622 return ListView_GetCountPerPage(GetHwnd());
625 // Gets the edit control for editing labels.
626 wxTextCtrl
* wxListCtrl::GetEditControl() const
631 // Gets information about the item
632 bool wxListCtrl::GetItem(wxListItem
& info
) const
635 wxZeroMemory(lvItem
);
637 lvItem
.iItem
= info
.m_itemId
;
638 lvItem
.iSubItem
= info
.m_col
;
640 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
642 lvItem
.mask
|= LVIF_TEXT
;
643 lvItem
.pszText
= new wxChar
[513];
644 lvItem
.cchTextMax
= 512;
648 lvItem
.pszText
= NULL
;
651 if (info
.m_mask
& wxLIST_MASK_DATA
)
652 lvItem
.mask
|= LVIF_PARAM
;
654 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
655 lvItem
.mask
|= LVIF_IMAGE
;
657 if ( info
.m_mask
& wxLIST_MASK_STATE
)
659 lvItem
.mask
|= LVIF_STATE
;
660 // the other bits are hardly interesting anyhow
661 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
664 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
667 wxLogError(_("Couldn't retrieve information about list control item %d."),
672 wxConvertFromMSWListItem(GetHwnd(), info
, lvItem
);
676 delete[] lvItem
.pszText
;
681 // Sets information about the item
682 bool wxListCtrl::SetItem(wxListItem
& info
)
685 wxConvertToMSWListItem(this, info
, item
);
688 if ( !ListView_SetItem(GetHwnd(), &item
) )
690 wxLogDebug(_T("ListView_SetItem() failed"));
695 // we need to update the item immediately to show the new image
696 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
698 // check whether it has any custom attributes
699 if ( info
.HasAttributes() )
701 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
704 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
706 *attr
= *info
.GetAttributes();
710 // if the colour has changed, we must redraw the item
716 // we need this to make the change visible right now
717 ListView_Update(GetHwnd(), item
.iItem
);
723 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
727 info
.m_mask
= wxLIST_MASK_TEXT
;
728 info
.m_itemId
= index
;
732 info
.m_image
= imageId
;
733 info
.m_mask
|= wxLIST_MASK_IMAGE
;
735 return SetItem(info
);
739 // Gets the item state
740 int wxListCtrl::GetItemState(long item
, long stateMask
) const
744 info
.m_mask
= wxLIST_MASK_STATE
;
745 info
.m_stateMask
= stateMask
;
746 info
.m_itemId
= item
;
754 // Sets the item state
755 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
757 // NB: don't use SetItem() here as it doesn't work with the virtual list
760 wxZeroMemory(lvItem
);
762 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
764 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
765 (WPARAM
)item
, (LPARAM
)&lvItem
) )
767 wxLogLastError(_T("ListView_SetItemState"));
775 // Sets the item image
776 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
780 info
.m_mask
= wxLIST_MASK_IMAGE
;
781 info
.m_image
= image
;
782 info
.m_itemId
= item
;
784 return SetItem(info
);
787 // Gets the item text
788 wxString
wxListCtrl::GetItemText(long item
) const
792 info
.m_mask
= wxLIST_MASK_TEXT
;
793 info
.m_itemId
= item
;
800 // Sets the item text
801 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
805 info
.m_mask
= wxLIST_MASK_TEXT
;
806 info
.m_itemId
= item
;
812 // Gets the item data
813 long wxListCtrl::GetItemData(long item
) const
817 info
.m_mask
= wxLIST_MASK_DATA
;
818 info
.m_itemId
= item
;
825 // Sets the item data
826 bool wxListCtrl::SetItemData(long item
, long data
)
830 info
.m_mask
= wxLIST_MASK_DATA
;
831 info
.m_itemId
= item
;
834 return SetItem(info
);
837 // Gets the item rectangle
838 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
842 int code2
= LVIR_BOUNDS
;
843 if ( code
== wxLIST_RECT_BOUNDS
)
845 else if ( code
== wxLIST_RECT_ICON
)
847 else if ( code
== wxLIST_RECT_LABEL
)
851 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
853 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
858 rect
.width
= rect2
.right
- rect2
.left
;
859 rect
.height
= rect2
.bottom
- rect2
.top
;
863 // Gets the item position
864 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
868 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
870 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
874 // Sets the item position.
875 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
877 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
880 // Gets the number of items in the list control
881 int wxListCtrl::GetItemCount() const
883 return ListView_GetItemCount(GetHwnd());
886 // Retrieves the spacing between icons in pixels.
887 // If small is TRUE, gets the spacing for the small icon
888 // view, otherwise the large icon view.
889 int wxListCtrl::GetItemSpacing(bool isSmall
) const
891 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
894 // Gets the number of selected items in the list control
895 int wxListCtrl::GetSelectedItemCount() const
897 return ListView_GetSelectedCount(GetHwnd());
900 // Gets the text colour of the listview
901 wxColour
wxListCtrl::GetTextColour() const
903 COLORREF ref
= ListView_GetTextColor(GetHwnd());
904 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
908 // Sets the text colour of the listview
909 void wxListCtrl::SetTextColour(const wxColour
& col
)
911 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
914 // Gets the index of the topmost visible item when in
915 // list or report view
916 long wxListCtrl::GetTopItem() const
918 return (long) ListView_GetTopIndex(GetHwnd());
921 // Searches for an item, starting from 'item'.
922 // 'geometry' is one of
923 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
924 // 'state' is a state bit flag, one or more of
925 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
926 // item can be -1 to find the first item that matches the
928 // Returns the item or -1 if unsuccessful.
929 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
933 if ( geom
== wxLIST_NEXT_ABOVE
)
935 if ( geom
== wxLIST_NEXT_ALL
)
937 if ( geom
== wxLIST_NEXT_BELOW
)
939 if ( geom
== wxLIST_NEXT_LEFT
)
940 flags
|= LVNI_TOLEFT
;
941 if ( geom
== wxLIST_NEXT_RIGHT
)
942 flags
|= LVNI_TORIGHT
;
944 if ( state
& wxLIST_STATE_CUT
)
946 if ( state
& wxLIST_STATE_DROPHILITED
)
947 flags
|= LVNI_DROPHILITED
;
948 if ( state
& wxLIST_STATE_FOCUSED
)
949 flags
|= LVNI_FOCUSED
;
950 if ( state
& wxLIST_STATE_SELECTED
)
951 flags
|= LVNI_SELECTED
;
953 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
957 wxImageList
*wxListCtrl::GetImageList(int which
) const
959 if ( which
== wxIMAGE_LIST_NORMAL
)
961 return m_imageListNormal
;
963 else if ( which
== wxIMAGE_LIST_SMALL
)
965 return m_imageListSmall
;
967 else if ( which
== wxIMAGE_LIST_STATE
)
969 return m_imageListState
;
974 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
977 if ( which
== wxIMAGE_LIST_NORMAL
)
979 flags
= LVSIL_NORMAL
;
980 if (m_ownsImageListNormal
) delete m_imageListNormal
;
981 m_imageListNormal
= imageList
;
982 m_ownsImageListNormal
= FALSE
;
984 else if ( which
== wxIMAGE_LIST_SMALL
)
987 if (m_ownsImageListSmall
) delete m_imageListSmall
;
988 m_imageListSmall
= imageList
;
989 m_ownsImageListSmall
= FALSE
;
991 else if ( which
== wxIMAGE_LIST_STATE
)
994 if (m_ownsImageListState
) delete m_imageListState
;
995 m_imageListState
= imageList
;
996 m_ownsImageListState
= FALSE
;
998 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1001 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1003 SetImageList(imageList
, which
);
1004 if ( which
== wxIMAGE_LIST_NORMAL
)
1005 m_ownsImageListNormal
= TRUE
;
1006 else if ( which
== wxIMAGE_LIST_SMALL
)
1007 m_ownsImageListSmall
= TRUE
;
1008 else if ( which
== wxIMAGE_LIST_STATE
)
1009 m_ownsImageListState
= TRUE
;
1012 // ----------------------------------------------------------------------------
1014 // ----------------------------------------------------------------------------
1016 // Arranges the items
1017 bool wxListCtrl::Arrange(int flag
)
1020 if ( flag
== wxLIST_ALIGN_LEFT
)
1021 code
= LVA_ALIGNLEFT
;
1022 else if ( flag
== wxLIST_ALIGN_TOP
)
1023 code
= LVA_ALIGNTOP
;
1024 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1026 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1027 code
= LVA_SNAPTOGRID
;
1029 return (ListView_Arrange(GetHwnd(), code
) != 0);
1033 bool wxListCtrl::DeleteItem(long item
)
1035 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1038 // Deletes all items
1039 bool wxListCtrl::DeleteAllItems()
1041 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1044 // Deletes all items
1045 bool wxListCtrl::DeleteAllColumns()
1047 while ( m_colCount
> 0 )
1049 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1051 wxLogLastError(wxT("ListView_DeleteColumn"));
1059 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1065 bool wxListCtrl::DeleteColumn(int col
)
1067 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1069 if ( success
&& (m_colCount
> 0) )
1074 // Clears items, and columns if there are any.
1075 void wxListCtrl::ClearAll()
1078 if ( m_colCount
> 0 )
1082 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1084 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1086 // VS: ListView_EditLabel requires that the list has focus.
1088 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1092 m_textCtrl
->SetHWND(0);
1093 m_textCtrl
->UnsubclassWin();
1098 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1099 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1100 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1105 // End label editing, optionally cancelling the edit
1106 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1108 wxFAIL_MSG( _T("not implemented") );
1113 // Ensures this item is visible
1114 bool wxListCtrl::EnsureVisible(long item
)
1116 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1119 // Find an item whose label matches this string, starting from the item after 'start'
1120 // or the beginning if 'start' is -1.
1121 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1123 LV_FINDINFO findInfo
;
1125 findInfo
.flags
= LVFI_STRING
;
1127 findInfo
.flags
|= LVFI_PARTIAL
;
1130 // ListView_FindItem() excludes the first item from search and to look
1131 // through all the items you need to start from -1 which is unnatural and
1132 // inconsistent with the generic version - so we adjust the index
1135 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1138 // Find an item whose data matches this data, starting from the item after 'start'
1139 // or the beginning if 'start' is -1.
1140 long wxListCtrl::FindItem(long start
, long data
)
1142 LV_FINDINFO findInfo
;
1144 findInfo
.flags
= LVFI_PARAM
;
1145 findInfo
.lParam
= data
;
1147 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1150 // Find an item nearest this position in the specified direction, starting from
1151 // the item after 'start' or the beginning if 'start' is -1.
1152 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1154 LV_FINDINFO findInfo
;
1156 findInfo
.flags
= LVFI_NEARESTXY
;
1157 findInfo
.pt
.x
= pt
.x
;
1158 findInfo
.pt
.y
= pt
.y
;
1159 findInfo
.vkDirection
= VK_RIGHT
;
1161 if ( direction
== wxLIST_FIND_UP
)
1162 findInfo
.vkDirection
= VK_UP
;
1163 else if ( direction
== wxLIST_FIND_DOWN
)
1164 findInfo
.vkDirection
= VK_DOWN
;
1165 else if ( direction
== wxLIST_FIND_LEFT
)
1166 findInfo
.vkDirection
= VK_LEFT
;
1167 else if ( direction
== wxLIST_FIND_RIGHT
)
1168 findInfo
.vkDirection
= VK_RIGHT
;
1170 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1173 // Determines which item (if any) is at the specified point,
1174 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1175 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1177 LV_HITTESTINFO hitTestInfo
;
1178 hitTestInfo
.pt
.x
= (int) point
.x
;
1179 hitTestInfo
.pt
.y
= (int) point
.y
;
1181 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1184 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1185 flags
|= wxLIST_HITTEST_ABOVE
;
1186 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1187 flags
|= wxLIST_HITTEST_BELOW
;
1188 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1189 flags
|= wxLIST_HITTEST_NOWHERE
;
1190 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1191 flags
|= wxLIST_HITTEST_ONITEMICON
;
1192 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1193 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1194 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1195 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1196 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1197 flags
|= wxLIST_HITTEST_TOLEFT
;
1198 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1199 flags
|= wxLIST_HITTEST_TORIGHT
;
1201 return (long) hitTestInfo
.iItem
;
1204 // Inserts an item, returning the index of the new item if successful,
1206 long wxListCtrl::InsertItem(wxListItem
& info
)
1208 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1211 wxConvertToMSWListItem(this, info
, item
);
1213 // check whether it has any custom attributes
1214 if ( info
.HasAttributes() )
1217 wxListItemAttr
*attr
;
1218 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1222 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1224 else *attr
= *info
.GetAttributes();
1226 m_hasAnyAttr
= TRUE
;
1229 return (long) ListView_InsertItem(GetHwnd(), & item
);
1232 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1235 info
.m_text
= label
;
1236 info
.m_mask
= wxLIST_MASK_TEXT
;
1237 info
.m_itemId
= index
;
1238 return InsertItem(info
);
1241 // Inserts an image item
1242 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1245 info
.m_image
= imageIndex
;
1246 info
.m_mask
= wxLIST_MASK_IMAGE
;
1247 info
.m_itemId
= index
;
1248 return InsertItem(info
);
1251 // Inserts an image/string item
1252 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1255 info
.m_image
= imageIndex
;
1256 info
.m_text
= label
;
1257 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1258 info
.m_itemId
= index
;
1259 return InsertItem(info
);
1262 // For list view mode (only), inserts a column.
1263 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1268 lvCol
.pszText
= NULL
;
1270 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1272 lvCol
.mask
|= LVCF_TEXT
;
1273 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1274 lvCol
.cchTextMax
= 0; // Ignored
1276 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1278 lvCol
.mask
|= LVCF_FMT
;
1280 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1281 lvCol
.fmt
= LVCFMT_LEFT
;
1282 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1283 lvCol
.fmt
= LVCFMT_RIGHT
;
1284 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1285 lvCol
.fmt
= LVCFMT_CENTER
;
1288 lvCol
.mask
|= LVCF_WIDTH
;
1289 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1291 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1292 lvCol
.cx
= LVSCW_AUTOSIZE
;
1293 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1294 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1296 lvCol
.cx
= item
.m_width
;
1300 // always give some width to the new column: this one is compatible
1305 lvCol
.mask
|= LVCF_SUBITEM
;
1306 lvCol
.iSubItem
= col
;
1308 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1315 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1322 long wxListCtrl::InsertColumn(long col
,
1323 const wxString
& heading
,
1328 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1329 item
.m_text
= heading
;
1332 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1333 item
.m_width
= width
;
1335 item
.m_format
= format
;
1337 return InsertColumn(col
, item
);
1340 // Scrolls the list control. If in icon, small icon or report view mode,
1341 // x specifies the number of pixels to scroll. If in list view mode, x
1342 // specifies the number of columns to scroll.
1343 // If in icon, small icon or list view mode, y specifies the number of pixels
1344 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1345 bool wxListCtrl::ScrollList(int dx
, int dy
)
1347 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1352 // fn is a function which takes 3 long arguments: item1, item2, data.
1353 // item1 is the long data associated with a first item (NOT the index).
1354 // item2 is the long data associated with a second item (NOT the index).
1355 // data is the same value as passed to SortItems.
1356 // The return value is a negative number if the first item should precede the second
1357 // item, a positive number of the second item should precede the first,
1358 // or zero if the two items are equivalent.
1360 // data is arbitrary data to be passed to the sort function.
1361 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1363 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1366 // ----------------------------------------------------------------------------
1367 // message processing
1368 // ----------------------------------------------------------------------------
1370 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1372 if (cmd
== EN_UPDATE
)
1374 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1375 event
.SetEventObject( this );
1376 ProcessCommand(event
);
1379 else if (cmd
== EN_KILLFOCUS
)
1381 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1382 event
.SetEventObject( this );
1383 ProcessCommand(event
);
1390 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1392 // prepare the event
1393 // -----------------
1395 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1396 wxEventType eventType
= wxEVT_NULL
;
1398 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1400 // almost all messages use NM_LISTVIEW
1401 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1403 // this is true for almost all events
1404 event
.m_item
.m_data
= nmLV
->lParam
;
1406 switch ( nmhdr
->code
)
1408 case LVN_BEGINRDRAG
:
1409 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1413 if ( eventType
== wxEVT_NULL
)
1415 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1418 event
.m_itemIndex
= nmLV
->iItem
;
1419 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1420 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1423 case LVN_BEGINLABELEDIT
:
1425 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1426 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1427 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1428 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1432 case LVN_COLUMNCLICK
:
1433 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1434 event
.m_itemIndex
= -1;
1435 event
.m_col
= nmLV
->iSubItem
;
1438 case LVN_DELETEALLITEMS
:
1439 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1440 event
.m_itemIndex
= -1;
1446 case LVN_DELETEITEM
:
1447 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1448 event
.m_itemIndex
= nmLV
->iItem
;
1452 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1456 case LVN_ENDLABELEDIT
:
1458 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1459 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1460 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1461 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1464 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1468 case LVN_SETDISPINFO
:
1470 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1471 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1472 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1476 case LVN_INSERTITEM
:
1477 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1478 event
.m_itemIndex
= nmLV
->iItem
;
1481 case LVN_ITEMCHANGED
:
1482 // This needs to be sent to wxListCtrl as a rather more concrete
1483 // event. For now, just detect a selection or deselection.
1484 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1486 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1487 event
.m_itemIndex
= nmLV
->iItem
;
1489 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1491 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1492 event
.m_itemIndex
= nmLV
->iItem
;
1502 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1503 WORD wVKey
= info
->wVKey
;
1505 // get the current selection
1506 long lItem
= GetNextItem(-1,
1508 wxLIST_STATE_SELECTED
);
1510 // <Enter> or <Space> activate the selected item if any (but
1511 // not with Shift and/or Ctrl as then they have a predefined
1512 // meaning for the list view)
1514 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1515 !(wxIsShiftDown() || wxIsCtrlDown()) )
1517 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1521 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1522 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1526 event
.m_item
.m_itemId
= lItem
;
1530 // fill the other fields too
1531 event
.m_item
.m_text
= GetItemText(lItem
);
1532 event
.m_item
.m_data
= GetItemData(lItem
);
1538 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1540 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1545 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1546 // if it happened on an item (and not on empty place)
1547 if ( nmLV
->iItem
== -1 )
1553 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1554 event
.m_itemIndex
= nmLV
->iItem
;
1555 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1556 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1560 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1561 subclass and check for the actual WM_RBUTTONDOWN message,
1562 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1563 before firing off. We want to have notify events for both down
1566 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1567 // don't do anything else
1568 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1573 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1574 LV_HITTESTINFO lvhti
;
1575 wxZeroMemory(lvhti
);
1577 ::GetCursorPos(&(lvhti
.pt
));
1578 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1579 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1581 if ( lvhti
.flags
& LVHT_ONITEM
)
1583 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1584 event
.m_itemIndex
= lvhti
.iItem
;
1585 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1586 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1593 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1595 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1597 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1602 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1603 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1604 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1605 event
.m_itemIndex
= hdr
->iItem
;
1610 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1612 *result
= OnCustomDraw(lParam
);
1615 #endif // _WIN32_IE >= 0x300
1617 case LVN_GETDISPINFO
:
1620 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1622 LV_ITEM
& lvi
= info
->item
;
1623 long item
= lvi
.iItem
;
1625 if ( lvi
.mask
& LVIF_TEXT
)
1627 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1628 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1631 if ( lvi
.mask
& LVIF_IMAGE
)
1633 lvi
.iImage
= OnGetItemImage(item
);
1636 // a little dose of healthy paranoia: as we never use
1637 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1638 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1639 _T("we don't support state callbacks yet!") );
1646 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1649 // process the event
1650 // -----------------
1652 event
.SetEventObject( this );
1653 event
.SetEventType(eventType
);
1655 if ( !GetEventHandler()->ProcessEvent(event
) )
1661 switch ( nmhdr
->code
)
1663 case LVN_DELETEALLITEMS
:
1664 // always return TRUE to suppress all additional LVN_DELETEITEM
1665 // notifications - this makes deleting all items from a list ctrl
1671 case LVN_ENDLABELEDIT
:
1673 *result
= event
.IsAllowed();
1678 *result
= !event
.IsAllowed();
1683 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1685 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1687 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1688 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1689 switch ( nmcd
.dwDrawStage
)
1692 // if we've got any items with non standard attributes,
1693 // notify us before painting each item
1695 // for virtual controls, always suppose that we have attributes as
1696 // there is no way to check for this
1697 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1700 case CDDS_ITEMPREPAINT
:
1702 size_t item
= (size_t)nmcd
.dwItemSpec
;
1703 if ( item
>= (size_t)GetItemCount() )
1705 // we get this message with item == 0 for an empty control,
1706 // we must ignore it as calling OnGetItemAttr() would be
1708 return CDRF_DODEFAULT
;
1711 wxListItemAttr
*attr
=
1712 IsVirtual() ? OnGetItemAttr(item
)
1713 : (wxListItemAttr
*)m_attrs
.Get(item
);
1717 // nothing to do for this item
1718 return CDRF_DODEFAULT
;
1722 wxColour colText
, colBack
;
1723 if ( attr
->HasFont() )
1725 wxFont font
= attr
->GetFont();
1726 hFont
= (HFONT
)font
.GetResourceHandle();
1733 if ( attr
->HasTextColour() )
1735 colText
= attr
->GetTextColour();
1739 colText
= GetTextColour();
1742 if ( attr
->HasBackgroundColour() )
1744 colBack
= attr
->GetBackgroundColour();
1748 colBack
= GetBackgroundColour();
1751 lplvcd
->clrText
= wxColourToRGB(colText
);
1752 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1754 // note that if we wanted to set colours for
1755 // individual columns (subitems), we would have
1756 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1759 ::SelectObject(nmcd
.hdc
, hFont
);
1761 return CDRF_NEWFONT
;
1764 // fall through to return CDRF_DODEFAULT
1767 return CDRF_DODEFAULT
;
1771 #endif // NM_CUSTOMDRAW supported
1773 // Necessary for drawing hrules and vrules, if specified
1774 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1778 wxControl::OnPaint(event
);
1780 // Reset the device origin since it may have been set
1781 dc
.SetDeviceOrigin(0, 0);
1783 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1784 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1786 if (!drawHRules
&& !drawVRules
)
1788 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1791 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1793 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1795 wxSize clientSize
= GetClientSize();
1799 int itemCount
= GetItemCount();
1801 for (i
= 0; i
< itemCount
; i
++)
1803 if (GetItemRect(i
, itemRect
))
1805 cy
= itemRect
.GetTop();
1806 if (i
!= 0) // Don't draw the first one
1808 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1811 if (i
== (GetItemCount() - 1))
1813 cy
= itemRect
.GetBottom();
1814 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1818 i
= (GetItemCount() - 1);
1819 if (drawVRules
&& (i
> -1))
1821 wxRect firstItemRect
;
1822 GetItemRect(0, firstItemRect
);
1824 if (GetItemRect(i
, itemRect
))
1827 int x
= itemRect
.GetX();
1828 for (col
= 0; col
< GetColumnCount(); col
++)
1830 int colWidth
= GetColumnWidth(col
);
1832 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1838 // ----------------------------------------------------------------------------
1839 // virtual list controls
1840 // ----------------------------------------------------------------------------
1842 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
1844 // this is a pure virtual function, in fact - which is not really pure
1845 // because the controls which are not virtual don't need to implement it
1846 wxFAIL_MSG( _T("not supposed to be called") );
1848 return wxEmptyString
;
1851 int wxListCtrl::OnGetItemImage(long item
) const
1854 wxFAIL_MSG( _T("not supposed to be called") );
1859 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
1861 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
1862 _T("invalid item index in OnGetItemAttr()") );
1864 // no attributes by default
1868 void wxListCtrl::SetItemCount(long count
)
1870 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1872 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
1874 wxLogLastError(_T("ListView_SetItemCount"));
1878 void wxListCtrl::RefreshItem(long item
)
1880 if ( !ListView_Update(GetHwnd(), item
) )
1882 wxLogLastError(_T("ListView_Update"));
1886 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
1888 for ( long item
= itemFrom
; item
<= itemTo
; item
++ )
1894 // ----------------------------------------------------------------------------
1896 // ----------------------------------------------------------------------------
1898 // List item structure
1899 wxListItem::wxListItem()
1909 m_format
= wxLIST_FORMAT_CENTRE
;
1915 void wxListItem::Clear()
1924 m_format
= wxLIST_FORMAT_CENTRE
;
1926 m_text
= wxEmptyString
;
1928 if (m_attr
) delete m_attr
;
1932 void wxListItem::ClearAttributes()
1934 if (m_attr
) delete m_attr
;
1938 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
1942 info
.m_data
= lvItem
.lParam
;
1945 info
.m_stateMask
= 0;
1946 info
.m_itemId
= lvItem
.iItem
;
1948 long oldMask
= lvItem
.mask
;
1950 bool needText
= FALSE
;
1951 if (hwndListCtrl
!= 0)
1953 if ( lvItem
.mask
& LVIF_TEXT
)
1960 lvItem
.pszText
= new wxChar
[513];
1961 lvItem
.cchTextMax
= 512;
1963 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1964 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1967 if ( lvItem
.mask
& LVIF_STATE
)
1969 info
.m_mask
|= wxLIST_MASK_STATE
;
1971 if ( lvItem
.stateMask
& LVIS_CUT
)
1973 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1974 if ( lvItem
.state
& LVIS_CUT
)
1975 info
.m_state
|= wxLIST_STATE_CUT
;
1977 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1979 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1980 if ( lvItem
.state
& LVIS_DROPHILITED
)
1981 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1983 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1985 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1986 if ( lvItem
.state
& LVIS_FOCUSED
)
1987 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1989 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1991 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1992 if ( lvItem
.state
& LVIS_SELECTED
)
1993 info
.m_state
|= wxLIST_STATE_SELECTED
;
1997 if ( lvItem
.mask
& LVIF_TEXT
)
1999 info
.m_mask
|= wxLIST_MASK_TEXT
;
2000 info
.m_text
= lvItem
.pszText
;
2002 if ( lvItem
.mask
& LVIF_IMAGE
)
2004 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2005 info
.m_image
= lvItem
.iImage
;
2007 if ( lvItem
.mask
& LVIF_PARAM
)
2008 info
.m_mask
|= wxLIST_MASK_DATA
;
2009 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2010 info
.m_mask
|= wxLIST_SET_ITEM
;
2011 info
.m_col
= lvItem
.iSubItem
;
2016 delete[] lvItem
.pszText
;
2018 lvItem
.mask
= oldMask
;
2021 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2023 if (stateMask
& wxLIST_STATE_CUT
)
2025 lvItem
.stateMask
|= LVIS_CUT
;
2026 if (state
& wxLIST_STATE_CUT
)
2027 lvItem
.state
|= LVIS_CUT
;
2029 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2031 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2032 if (state
& wxLIST_STATE_DROPHILITED
)
2033 lvItem
.state
|= LVIS_DROPHILITED
;
2035 if (stateMask
& wxLIST_STATE_FOCUSED
)
2037 lvItem
.stateMask
|= LVIS_FOCUSED
;
2038 if (state
& wxLIST_STATE_FOCUSED
)
2039 lvItem
.state
|= LVIS_FOCUSED
;
2041 if (stateMask
& wxLIST_STATE_SELECTED
)
2043 lvItem
.stateMask
|= LVIS_SELECTED
;
2044 if (state
& wxLIST_STATE_SELECTED
)
2045 lvItem
.state
|= LVIS_SELECTED
;
2049 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2050 const wxListItem
& info
,
2053 lvItem
.iItem
= (int) info
.m_itemId
;
2055 lvItem
.iImage
= info
.m_image
;
2056 lvItem
.lParam
= info
.m_data
;
2057 lvItem
.stateMask
= 0;
2060 lvItem
.iSubItem
= info
.m_col
;
2062 if (info
.m_mask
& wxLIST_MASK_STATE
)
2064 lvItem
.mask
|= LVIF_STATE
;
2066 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2069 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2071 lvItem
.mask
|= LVIF_TEXT
;
2072 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2074 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2078 // pszText is not const, hence the cast
2079 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2080 if ( lvItem
.pszText
)
2081 lvItem
.cchTextMax
= info
.m_text
.Length();
2083 lvItem
.cchTextMax
= 0;
2086 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2087 lvItem
.mask
|= LVIF_IMAGE
;
2088 if (info
.m_mask
& wxLIST_MASK_DATA
)
2089 lvItem
.mask
|= LVIF_PARAM
;
2092 // ----------------------------------------------------------------------------
2094 // ----------------------------------------------------------------------------
2096 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2098 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2099 : wxNotifyEvent(commandType
, id
)
2105 m_cancelled
= FALSE
;
2108 #endif // wxUSE_LISTCTRL