1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
38 #include "wx/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
46 #include "wx/msw/private.h"
48 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
49 #include "wx/msw/gnuwin32/extra.h"
56 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
60 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
63 #ifndef LVS_EX_FULLROWSELECT
64 #define LVS_EX_FULLROWSELECT 0x00000020
68 #define LVS_OWNERDATA 0x1000
71 // mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff
73 typedef struct tagNMLVCACHEHINT
80 #define NM_CACHEHINT NMLVCACHEHINT
83 #ifndef LVN_ODCACHEHINT
84 #define LVN_ODCACHEHINT (-113)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // convert our state and mask flags to LV_ITEM constants
92 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
94 // convert wxListItem to LV_ITEM
95 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
96 const wxListItem
& info
, LV_ITEM
& lvItem
);
98 // convert LV_ITEM to wxListItem
99 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
101 /* const */ LV_ITEM
& lvItem
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
112 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
113 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
114 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
115 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
116 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
117 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
118 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
119 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
120 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
121 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
122 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
123 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
124 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
125 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
126 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
127 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
129 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
130 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
131 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
133 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
134 EVT_PAINT(wxListCtrl::OnPaint
)
137 // ============================================================================
139 // ============================================================================
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 void wxListEvent::CopyObject(wxObject
& object_dest
) const
147 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
149 wxNotifyEvent::CopyObject(object_dest
);
151 obj
->m_code
= m_code
;
152 obj
->m_itemIndex
= m_itemIndex
;
153 obj
->m_oldItemIndex
= m_oldItemIndex
;
155 obj
->m_cancelled
= m_cancelled
;
156 obj
->m_pointDrag
= m_pointDrag
;
157 obj
->m_item
.m_mask
= m_item
.m_mask
;
158 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
159 obj
->m_item
.m_col
= m_item
.m_col
;
160 obj
->m_item
.m_state
= m_item
.m_state
;
161 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
162 obj
->m_item
.m_text
= m_item
.m_text
;
163 obj
->m_item
.m_image
= m_item
.m_image
;
164 obj
->m_item
.m_data
= m_item
.m_data
;
165 obj
->m_item
.m_format
= m_item
.m_format
;
166 obj
->m_item
.m_width
= m_item
.m_width
;
168 if ( m_item
.HasAttributes() )
170 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
171 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
172 obj
->m_item
.SetFont(m_item
.GetFont());
176 // ----------------------------------------------------------------------------
177 // wxListCtrl construction
178 // ----------------------------------------------------------------------------
180 void wxListCtrl::Init()
182 m_imageListNormal
= NULL
;
183 m_imageListSmall
= NULL
;
184 m_imageListState
= NULL
;
185 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
189 m_hasAnyAttr
= FALSE
;
192 bool wxListCtrl::Create(wxWindow
*parent
,
197 const wxValidator
& validator
,
198 const wxString
& name
)
201 SetValidator(validator
);
202 #endif // wxUSE_VALIDATORS
211 m_windowStyle
= style
;
224 m_windowId
= (id
== -1) ? NewControlId() : id
;
226 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
227 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
229 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
230 wstyle
|= WS_CLIPSIBLINGS
;
232 if ( wxStyleHasBorder(m_windowStyle
) )
234 m_baseStyle
= wstyle
;
236 if ( !DoCreateControl(x
, y
, width
, height
) )
240 parent
->AddChild(this);
245 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
247 DWORD wstyle
= m_baseStyle
;
250 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
252 // Even with extended styles, need to combine with WS_BORDER
253 // for them to look right.
257 long oldStyle
= 0; // Dummy
258 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
260 // Create the ListView control.
261 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
266 GetWinHwnd(GetParent()),
273 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
278 // for comctl32.dll v 4.70+ we want to have this attribute because it's
279 // prettier (and also because wxGTK does it like this)
280 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
282 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
283 0, LVS_EX_FULLROWSELECT
);
286 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
287 SetForegroundColour(GetParent()->GetForegroundColour());
294 void wxListCtrl::UpdateStyle()
298 // The new window view style
300 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
301 dwStyleNew
|= m_baseStyle
;
303 // Get the current window style.
304 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
306 // Only set the window style if the view bits have changed.
307 if ( dwStyleOld
!= dwStyleNew
)
309 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
314 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
318 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
320 delete (wxListItemAttr
*)node
->Data();
326 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
329 m_hasAnyAttr
= FALSE
;
333 wxListCtrl::~wxListCtrl()
335 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
339 m_textCtrl
->SetHWND(0);
340 m_textCtrl
->UnsubclassWin();
345 if (m_ownsImageListNormal
) delete m_imageListNormal
;
346 if (m_ownsImageListSmall
) delete m_imageListSmall
;
347 if (m_ownsImageListState
) delete m_imageListState
;
350 // ----------------------------------------------------------------------------
351 // set/get/change style
352 // ----------------------------------------------------------------------------
354 // Add or remove a single window style
355 void wxListCtrl::SetSingleStyle(long style
, bool add
)
357 long flag
= GetWindowStyleFlag();
359 // Get rid of conflicting styles
362 if ( style
& wxLC_MASK_TYPE
)
363 flag
= flag
& ~wxLC_MASK_TYPE
;
364 if ( style
& wxLC_MASK_ALIGN
)
365 flag
= flag
& ~wxLC_MASK_ALIGN
;
366 if ( style
& wxLC_MASK_SORT
)
367 flag
= flag
& ~wxLC_MASK_SORT
;
383 m_windowStyle
= flag
;
388 // Set the whole window style
389 void wxListCtrl::SetWindowStyleFlag(long flag
)
391 m_windowStyle
= flag
;
396 // Can be just a single style, or a bitlist
397 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
400 if ( style
& wxLC_ICON
)
402 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
403 oldStyle
-= LVS_SMALLICON
;
404 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
405 oldStyle
-= LVS_REPORT
;
406 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
407 oldStyle
-= LVS_LIST
;
411 if ( style
& wxLC_SMALL_ICON
)
413 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
414 oldStyle
-= LVS_ICON
;
415 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
416 oldStyle
-= LVS_REPORT
;
417 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
418 oldStyle
-= LVS_LIST
;
419 wstyle
|= LVS_SMALLICON
;
422 if ( style
& wxLC_LIST
)
424 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
425 oldStyle
-= LVS_ICON
;
426 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
427 oldStyle
-= LVS_REPORT
;
428 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
429 oldStyle
-= LVS_SMALLICON
;
433 if ( style
& wxLC_REPORT
)
435 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
436 oldStyle
-= LVS_ICON
;
437 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
438 oldStyle
-= LVS_LIST
;
439 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
440 oldStyle
-= LVS_SMALLICON
;
442 wstyle
|= LVS_REPORT
;
445 if ( style
& wxLC_ALIGN_LEFT
)
447 if ( oldStyle
& LVS_ALIGNTOP
)
448 oldStyle
-= LVS_ALIGNTOP
;
449 wstyle
|= LVS_ALIGNLEFT
;
452 if ( style
& wxLC_ALIGN_TOP
)
454 if ( oldStyle
& LVS_ALIGNLEFT
)
455 oldStyle
-= LVS_ALIGNLEFT
;
456 wstyle
|= LVS_ALIGNTOP
;
459 if ( style
& wxLC_AUTOARRANGE
)
460 wstyle
|= LVS_AUTOARRANGE
;
462 if ( style
& wxLC_NO_SORT_HEADER
)
463 wstyle
|= LVS_NOSORTHEADER
;
465 if ( style
& wxLC_NO_HEADER
)
466 wstyle
|= LVS_NOCOLUMNHEADER
;
468 if ( style
& wxLC_EDIT_LABELS
)
469 wstyle
|= LVS_EDITLABELS
;
471 if ( style
& wxLC_SINGLE_SEL
)
472 wstyle
|= LVS_SINGLESEL
;
474 if ( style
& wxLC_SORT_ASCENDING
)
476 if ( oldStyle
& LVS_SORTDESCENDING
)
477 oldStyle
-= LVS_SORTDESCENDING
;
478 wstyle
|= LVS_SORTASCENDING
;
481 if ( style
& wxLC_SORT_DESCENDING
)
483 if ( oldStyle
& LVS_SORTASCENDING
)
484 oldStyle
-= LVS_SORTASCENDING
;
485 wstyle
|= LVS_SORTDESCENDING
;
488 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
489 if ( style
& wxLC_VIRTUAL
)
491 int ver
= wxTheApp
->GetComCtl32Version();
494 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."),
495 ver
/ 100, ver
% 100);
498 wstyle
|= LVS_OWNERDATA
;
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
509 // Sets the foreground, i.e. text, colour
510 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
512 if ( !wxWindow::SetForegroundColour(col
) )
515 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
520 // Sets the background colour
521 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
523 if ( !wxWindow::SetBackgroundColour(col
) )
526 // we set the same colour for both the "empty" background and the items
528 COLORREF color
= wxColourToRGB(col
);
529 ListView_SetBkColor(GetHwnd(), color
);
530 ListView_SetTextBkColor(GetHwnd(), color
);
535 // Gets information about this column
536 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
541 lvCol
.pszText
= NULL
;
543 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
545 lvCol
.mask
|= LVCF_TEXT
;
546 lvCol
.pszText
= new wxChar
[513];
547 lvCol
.cchTextMax
= 512;
550 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
552 // item.m_subItem = lvCol.iSubItem;
553 item
.m_width
= lvCol
.cx
;
555 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
557 item
.m_text
= lvCol
.pszText
;
558 delete[] lvCol
.pszText
;
561 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
563 if (lvCol
.fmt
== LVCFMT_LEFT
)
564 item
.m_format
= wxLIST_FORMAT_LEFT
;
565 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
566 item
.m_format
= wxLIST_FORMAT_RIGHT
;
567 else if (lvCol
.fmt
== LVCFMT_CENTER
)
568 item
.m_format
= wxLIST_FORMAT_CENTRE
;
574 // Sets information about this column
575 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
580 lvCol
.pszText
= NULL
;
582 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
584 lvCol
.mask
|= LVCF_TEXT
;
585 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
586 lvCol
.cchTextMax
= 0; // Ignored
588 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
590 lvCol
.mask
|= LVCF_FMT
;
592 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
593 lvCol
.fmt
= LVCFMT_LEFT
;
594 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
595 lvCol
.fmt
= LVCFMT_RIGHT
;
596 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
597 lvCol
.fmt
= LVCFMT_CENTER
;
600 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
602 lvCol
.mask
|= LVCF_WIDTH
;
603 lvCol
.cx
= item
.m_width
;
605 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
606 lvCol
.cx
= LVSCW_AUTOSIZE
;
607 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
608 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
610 lvCol
.mask
|= LVCF_SUBITEM
;
611 lvCol
.iSubItem
= col
;
612 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
615 // Gets the column width
616 int wxListCtrl::GetColumnWidth(int col
) const
618 return ListView_GetColumnWidth(GetHwnd(), col
);
621 // Sets the column width
622 bool wxListCtrl::SetColumnWidth(int col
, int width
)
625 if ( m_windowStyle
& wxLC_LIST
)
629 if ( width2
== wxLIST_AUTOSIZE
)
630 width2
= LVSCW_AUTOSIZE
;
631 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
632 width2
= LVSCW_AUTOSIZE_USEHEADER
;
634 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
637 // Gets the number of items that can fit vertically in the
638 // visible area of the list control (list or report view)
639 // or the total number of items in the list control (icon
640 // or small icon view)
641 int wxListCtrl::GetCountPerPage() const
643 return ListView_GetCountPerPage(GetHwnd());
646 // Gets the edit control for editing labels.
647 wxTextCtrl
* wxListCtrl::GetEditControl() const
652 // Gets information about the item
653 bool wxListCtrl::GetItem(wxListItem
& info
) const
656 wxZeroMemory(lvItem
);
658 lvItem
.iItem
= info
.m_itemId
;
659 lvItem
.iSubItem
= info
.m_col
;
661 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
663 lvItem
.mask
|= LVIF_TEXT
;
664 lvItem
.pszText
= new wxChar
[513];
665 lvItem
.cchTextMax
= 512;
669 lvItem
.pszText
= NULL
;
672 if (info
.m_mask
& wxLIST_MASK_DATA
)
673 lvItem
.mask
|= LVIF_PARAM
;
675 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
676 lvItem
.mask
|= LVIF_IMAGE
;
678 if ( info
.m_mask
& wxLIST_MASK_STATE
)
680 lvItem
.mask
|= LVIF_STATE
;
681 // the other bits are hardly interesting anyhow
682 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
685 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
688 wxLogError(_("Couldn't retrieve information about list control item %d."),
693 // give NULL as hwnd as we already have everything we need
694 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
698 delete[] lvItem
.pszText
;
703 // Sets information about the item
704 bool wxListCtrl::SetItem(wxListItem
& info
)
707 wxConvertToMSWListItem(this, info
, item
);
710 if ( !ListView_SetItem(GetHwnd(), &item
) )
712 wxLogDebug(_T("ListView_SetItem() failed"));
717 // we need to update the item immediately to show the new image
718 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
720 // check whether it has any custom attributes
721 if ( info
.HasAttributes() )
723 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
726 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
728 *attr
= *info
.GetAttributes();
732 // if the colour has changed, we must redraw the item
738 // we need this to make the change visible right now
739 ListView_Update(GetHwnd(), item
.iItem
);
745 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
749 info
.m_mask
= wxLIST_MASK_TEXT
;
750 info
.m_itemId
= index
;
754 info
.m_image
= imageId
;
755 info
.m_mask
|= wxLIST_MASK_IMAGE
;
757 return SetItem(info
);
761 // Gets the item state
762 int wxListCtrl::GetItemState(long item
, long stateMask
) const
766 info
.m_mask
= wxLIST_MASK_STATE
;
767 info
.m_stateMask
= stateMask
;
768 info
.m_itemId
= item
;
776 // Sets the item state
777 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
779 // NB: don't use SetItem() here as it doesn't work with the virtual list
782 wxZeroMemory(lvItem
);
784 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
786 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
787 (WPARAM
)item
, (LPARAM
)&lvItem
) )
789 wxLogLastError(_T("ListView_SetItemState"));
797 // Sets the item image
798 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
802 info
.m_mask
= wxLIST_MASK_IMAGE
;
803 info
.m_image
= image
;
804 info
.m_itemId
= item
;
806 return SetItem(info
);
809 // Gets the item text
810 wxString
wxListCtrl::GetItemText(long item
) const
814 info
.m_mask
= wxLIST_MASK_TEXT
;
815 info
.m_itemId
= item
;
822 // Sets the item text
823 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
827 info
.m_mask
= wxLIST_MASK_TEXT
;
828 info
.m_itemId
= item
;
834 // Gets the item data
835 long wxListCtrl::GetItemData(long item
) const
839 info
.m_mask
= wxLIST_MASK_DATA
;
840 info
.m_itemId
= item
;
847 // Sets the item data
848 bool wxListCtrl::SetItemData(long item
, long data
)
852 info
.m_mask
= wxLIST_MASK_DATA
;
853 info
.m_itemId
= item
;
856 return SetItem(info
);
859 // Gets the item rectangle
860 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
864 int code2
= LVIR_BOUNDS
;
865 if ( code
== wxLIST_RECT_BOUNDS
)
867 else if ( code
== wxLIST_RECT_ICON
)
869 else if ( code
== wxLIST_RECT_LABEL
)
873 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
875 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
880 rect
.width
= rect2
.right
- rect2
.left
;
881 rect
.height
= rect2
.bottom
- rect2
.top
;
885 // Gets the item position
886 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
890 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
892 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
896 // Sets the item position.
897 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
899 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
902 // Gets the number of items in the list control
903 int wxListCtrl::GetItemCount() const
905 return ListView_GetItemCount(GetHwnd());
908 // Retrieves the spacing between icons in pixels.
909 // If small is TRUE, gets the spacing for the small icon
910 // view, otherwise the large icon view.
911 int wxListCtrl::GetItemSpacing(bool isSmall
) const
913 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
916 // Gets the number of selected items in the list control
917 int wxListCtrl::GetSelectedItemCount() const
919 return ListView_GetSelectedCount(GetHwnd());
922 // Gets the text colour of the listview
923 wxColour
wxListCtrl::GetTextColour() const
925 COLORREF ref
= ListView_GetTextColor(GetHwnd());
926 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
930 // Sets the text colour of the listview
931 void wxListCtrl::SetTextColour(const wxColour
& col
)
933 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
936 // Gets the index of the topmost visible item when in
937 // list or report view
938 long wxListCtrl::GetTopItem() const
940 return (long) ListView_GetTopIndex(GetHwnd());
943 // Searches for an item, starting from 'item'.
944 // 'geometry' is one of
945 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
946 // 'state' is a state bit flag, one or more of
947 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
948 // item can be -1 to find the first item that matches the
950 // Returns the item or -1 if unsuccessful.
951 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
955 if ( geom
== wxLIST_NEXT_ABOVE
)
957 if ( geom
== wxLIST_NEXT_ALL
)
959 if ( geom
== wxLIST_NEXT_BELOW
)
961 if ( geom
== wxLIST_NEXT_LEFT
)
962 flags
|= LVNI_TOLEFT
;
963 if ( geom
== wxLIST_NEXT_RIGHT
)
964 flags
|= LVNI_TORIGHT
;
966 if ( state
& wxLIST_STATE_CUT
)
968 if ( state
& wxLIST_STATE_DROPHILITED
)
969 flags
|= LVNI_DROPHILITED
;
970 if ( state
& wxLIST_STATE_FOCUSED
)
971 flags
|= LVNI_FOCUSED
;
972 if ( state
& wxLIST_STATE_SELECTED
)
973 flags
|= LVNI_SELECTED
;
975 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
979 wxImageList
*wxListCtrl::GetImageList(int which
) const
981 if ( which
== wxIMAGE_LIST_NORMAL
)
983 return m_imageListNormal
;
985 else if ( which
== wxIMAGE_LIST_SMALL
)
987 return m_imageListSmall
;
989 else if ( which
== wxIMAGE_LIST_STATE
)
991 return m_imageListState
;
996 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
999 if ( which
== wxIMAGE_LIST_NORMAL
)
1001 flags
= LVSIL_NORMAL
;
1002 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1003 m_imageListNormal
= imageList
;
1004 m_ownsImageListNormal
= FALSE
;
1006 else if ( which
== wxIMAGE_LIST_SMALL
)
1008 flags
= LVSIL_SMALL
;
1009 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1010 m_imageListSmall
= imageList
;
1011 m_ownsImageListSmall
= FALSE
;
1013 else if ( which
== wxIMAGE_LIST_STATE
)
1015 flags
= LVSIL_STATE
;
1016 if (m_ownsImageListState
) delete m_imageListState
;
1017 m_imageListState
= imageList
;
1018 m_ownsImageListState
= FALSE
;
1020 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1023 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1025 SetImageList(imageList
, which
);
1026 if ( which
== wxIMAGE_LIST_NORMAL
)
1027 m_ownsImageListNormal
= TRUE
;
1028 else if ( which
== wxIMAGE_LIST_SMALL
)
1029 m_ownsImageListSmall
= TRUE
;
1030 else if ( which
== wxIMAGE_LIST_STATE
)
1031 m_ownsImageListState
= TRUE
;
1034 // ----------------------------------------------------------------------------
1036 // ----------------------------------------------------------------------------
1038 // Arranges the items
1039 bool wxListCtrl::Arrange(int flag
)
1042 if ( flag
== wxLIST_ALIGN_LEFT
)
1043 code
= LVA_ALIGNLEFT
;
1044 else if ( flag
== wxLIST_ALIGN_TOP
)
1045 code
= LVA_ALIGNTOP
;
1046 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1048 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1049 code
= LVA_SNAPTOGRID
;
1051 return (ListView_Arrange(GetHwnd(), code
) != 0);
1055 bool wxListCtrl::DeleteItem(long item
)
1057 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1060 // Deletes all items
1061 bool wxListCtrl::DeleteAllItems()
1063 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1066 // Deletes all items
1067 bool wxListCtrl::DeleteAllColumns()
1069 while ( m_colCount
> 0 )
1071 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1073 wxLogLastError(wxT("ListView_DeleteColumn"));
1081 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1087 bool wxListCtrl::DeleteColumn(int col
)
1089 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1091 if ( success
&& (m_colCount
> 0) )
1096 // Clears items, and columns if there are any.
1097 void wxListCtrl::ClearAll()
1100 if ( m_colCount
> 0 )
1104 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1106 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1108 // VS: ListView_EditLabel requires that the list has focus.
1110 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1114 m_textCtrl
->SetHWND(0);
1115 m_textCtrl
->UnsubclassWin();
1120 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1121 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1122 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1127 // End label editing, optionally cancelling the edit
1128 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1130 wxFAIL_MSG( _T("not implemented") );
1135 // Ensures this item is visible
1136 bool wxListCtrl::EnsureVisible(long item
)
1138 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1141 // Find an item whose label matches this string, starting from the item after 'start'
1142 // or the beginning if 'start' is -1.
1143 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1145 LV_FINDINFO findInfo
;
1147 findInfo
.flags
= LVFI_STRING
;
1149 findInfo
.flags
|= LVFI_PARTIAL
;
1152 // ListView_FindItem() excludes the first item from search and to look
1153 // through all the items you need to start from -1 which is unnatural and
1154 // inconsistent with the generic version - so we adjust the index
1157 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1160 // Find an item whose data matches this data, starting from the item after 'start'
1161 // or the beginning if 'start' is -1.
1162 long wxListCtrl::FindItem(long start
, long data
)
1164 LV_FINDINFO findInfo
;
1166 findInfo
.flags
= LVFI_PARAM
;
1167 findInfo
.lParam
= data
;
1169 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1172 // Find an item nearest this position in the specified direction, starting from
1173 // the item after 'start' or the beginning if 'start' is -1.
1174 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1176 LV_FINDINFO findInfo
;
1178 findInfo
.flags
= LVFI_NEARESTXY
;
1179 findInfo
.pt
.x
= pt
.x
;
1180 findInfo
.pt
.y
= pt
.y
;
1181 findInfo
.vkDirection
= VK_RIGHT
;
1183 if ( direction
== wxLIST_FIND_UP
)
1184 findInfo
.vkDirection
= VK_UP
;
1185 else if ( direction
== wxLIST_FIND_DOWN
)
1186 findInfo
.vkDirection
= VK_DOWN
;
1187 else if ( direction
== wxLIST_FIND_LEFT
)
1188 findInfo
.vkDirection
= VK_LEFT
;
1189 else if ( direction
== wxLIST_FIND_RIGHT
)
1190 findInfo
.vkDirection
= VK_RIGHT
;
1192 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1195 // Determines which item (if any) is at the specified point,
1196 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1197 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1199 LV_HITTESTINFO hitTestInfo
;
1200 hitTestInfo
.pt
.x
= (int) point
.x
;
1201 hitTestInfo
.pt
.y
= (int) point
.y
;
1203 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1206 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1207 flags
|= wxLIST_HITTEST_ABOVE
;
1208 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1209 flags
|= wxLIST_HITTEST_BELOW
;
1210 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1211 flags
|= wxLIST_HITTEST_NOWHERE
;
1212 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1213 flags
|= wxLIST_HITTEST_ONITEMICON
;
1214 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1215 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1216 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1217 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1218 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1219 flags
|= wxLIST_HITTEST_TOLEFT
;
1220 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1221 flags
|= wxLIST_HITTEST_TORIGHT
;
1223 return (long) hitTestInfo
.iItem
;
1226 // Inserts an item, returning the index of the new item if successful,
1228 long wxListCtrl::InsertItem(wxListItem
& info
)
1230 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1233 wxConvertToMSWListItem(this, info
, item
);
1235 // check whether it has any custom attributes
1236 if ( info
.HasAttributes() )
1239 wxListItemAttr
*attr
;
1240 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1244 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1246 else *attr
= *info
.GetAttributes();
1248 m_hasAnyAttr
= TRUE
;
1251 return (long) ListView_InsertItem(GetHwnd(), & item
);
1254 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1257 info
.m_text
= label
;
1258 info
.m_mask
= wxLIST_MASK_TEXT
;
1259 info
.m_itemId
= index
;
1260 return InsertItem(info
);
1263 // Inserts an image item
1264 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1267 info
.m_image
= imageIndex
;
1268 info
.m_mask
= wxLIST_MASK_IMAGE
;
1269 info
.m_itemId
= index
;
1270 return InsertItem(info
);
1273 // Inserts an image/string item
1274 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1277 info
.m_image
= imageIndex
;
1278 info
.m_text
= label
;
1279 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1280 info
.m_itemId
= index
;
1281 return InsertItem(info
);
1284 // For list view mode (only), inserts a column.
1285 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1290 lvCol
.pszText
= NULL
;
1292 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1294 lvCol
.mask
|= LVCF_TEXT
;
1295 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1296 lvCol
.cchTextMax
= 0; // Ignored
1298 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1300 lvCol
.mask
|= LVCF_FMT
;
1302 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1303 lvCol
.fmt
= LVCFMT_LEFT
;
1304 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1305 lvCol
.fmt
= LVCFMT_RIGHT
;
1306 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1307 lvCol
.fmt
= LVCFMT_CENTER
;
1310 lvCol
.mask
|= LVCF_WIDTH
;
1311 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1313 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1314 lvCol
.cx
= LVSCW_AUTOSIZE
;
1315 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1316 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1318 lvCol
.cx
= item
.m_width
;
1322 // always give some width to the new column: this one is compatible
1327 lvCol
.mask
|= LVCF_SUBITEM
;
1328 lvCol
.iSubItem
= col
;
1330 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1337 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1344 long wxListCtrl::InsertColumn(long col
,
1345 const wxString
& heading
,
1350 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1351 item
.m_text
= heading
;
1354 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1355 item
.m_width
= width
;
1357 item
.m_format
= format
;
1359 return InsertColumn(col
, item
);
1362 // Scrolls the list control. If in icon, small icon or report view mode,
1363 // x specifies the number of pixels to scroll. If in list view mode, x
1364 // specifies the number of columns to scroll.
1365 // If in icon, small icon or list view mode, y specifies the number of pixels
1366 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1367 bool wxListCtrl::ScrollList(int dx
, int dy
)
1369 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1374 // fn is a function which takes 3 long arguments: item1, item2, data.
1375 // item1 is the long data associated with a first item (NOT the index).
1376 // item2 is the long data associated with a second item (NOT the index).
1377 // data is the same value as passed to SortItems.
1378 // The return value is a negative number if the first item should precede the second
1379 // item, a positive number of the second item should precede the first,
1380 // or zero if the two items are equivalent.
1382 // data is arbitrary data to be passed to the sort function.
1384 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1385 // time for anything better right now (VZ)
1386 static long gs_sortData
= 0;
1387 static wxListCtrl
*gs_sortCtrl
= NULL
;
1388 static wxListCtrlCompare gs_sortFunction
= NULL
;
1390 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1392 int n1
= *(const int *)arg1
,
1393 n2
= *(const int *)arg2
;
1395 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1396 gs_sortCtrl
->GetItemData(n2
),
1400 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1402 // sort the attributes too
1406 count
= GetItemCount();
1407 int *aItems
= new int[count
];
1408 for ( n
= 0; n
< count
; n
++ )
1415 gs_sortFunction
= fn
;
1417 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1421 gs_sortFunction
= NULL
;
1423 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1424 for ( n
= 0; n
< count
; n
++ )
1426 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1429 attrsNew
.Put(n
, attr
);
1439 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1441 wxLogDebug(_T("ListView_SortItems() failed"));
1451 // ----------------------------------------------------------------------------
1452 // message processing
1453 // ----------------------------------------------------------------------------
1455 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1457 if (cmd
== EN_UPDATE
)
1459 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1460 event
.SetEventObject( this );
1461 ProcessCommand(event
);
1464 else if (cmd
== EN_KILLFOCUS
)
1466 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1467 event
.SetEventObject( this );
1468 ProcessCommand(event
);
1475 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1477 // prepare the event
1478 // -----------------
1480 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1481 wxEventType eventType
= wxEVT_NULL
;
1483 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1485 // check for messages from the header (in report view)
1486 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1488 // is it a message from the header?
1489 if ( nmhdr
->hwndFrom
== hwndHdr
)
1491 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1492 event
.m_itemIndex
= -1;
1494 switch ( nmhdr
->code
)
1496 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1497 // TRACK messages even to ANSI programs: on my system I get
1498 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1500 // work around is to simply catch both versions and hope that it
1501 // works (why should this message exist in ANSI and Unicode is
1502 // beyond me as it doesn't deal with strings at all...)
1503 case HDN_BEGINTRACKA
:
1504 case HDN_BEGINTRACKW
:
1505 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1510 if ( eventType
== wxEVT_NULL
)
1511 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1516 if ( eventType
== wxEVT_NULL
)
1517 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1518 event
.m_col
= nmHDR
->iItem
;
1523 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1526 // find the column clicked: we have to search for it
1527 // ourselves as the notification message doesn't provide
1530 // where did the click occur?
1532 if ( !::GetCursorPos(&ptClick
) )
1534 wxLogLastError(_T("GetCursorPos"));
1537 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1539 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1542 event
.m_pointDrag
.x
= ptClick
.x
;
1543 event
.m_pointDrag
.y
= ptClick
.y
;
1545 int colCount
= Header_GetItemCount(hwndHdr
);
1548 for ( int col
= 0; col
< colCount
; col
++ )
1550 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1552 if ( ::PtInRect(&rect
, ptClick
) )
1563 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1566 else if ( nmhdr
->hwndFrom
== GetHwnd() )
1568 // almost all messages use NM_LISTVIEW
1569 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1571 // this is true for almost all events
1572 event
.m_item
.m_data
= nmLV
->lParam
;
1574 switch ( nmhdr
->code
)
1576 case LVN_BEGINRDRAG
:
1577 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1581 if ( eventType
== wxEVT_NULL
)
1583 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1586 event
.m_itemIndex
= nmLV
->iItem
;
1587 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1588 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1591 case LVN_BEGINLABELEDIT
:
1593 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1594 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1595 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1596 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1600 case LVN_COLUMNCLICK
:
1601 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1602 event
.m_itemIndex
= -1;
1603 event
.m_col
= nmLV
->iSubItem
;
1606 case LVN_DELETEALLITEMS
:
1607 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1608 event
.m_itemIndex
= -1;
1614 case LVN_DELETEITEM
:
1615 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1616 event
.m_itemIndex
= nmLV
->iItem
;
1620 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1624 case LVN_ENDLABELEDIT
:
1626 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1627 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1628 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1629 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1632 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1636 case LVN_SETDISPINFO
:
1638 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1639 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1640 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1644 case LVN_INSERTITEM
:
1645 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1646 event
.m_itemIndex
= nmLV
->iItem
;
1649 case LVN_ITEMCHANGED
:
1650 // This needs to be sent to wxListCtrl as a rather more concrete
1651 // event. For now, just detect a selection or deselection.
1652 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1654 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1655 event
.m_itemIndex
= nmLV
->iItem
;
1657 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1659 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1660 event
.m_itemIndex
= nmLV
->iItem
;
1670 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1671 WORD wVKey
= info
->wVKey
;
1673 // get the current selection
1674 long lItem
= GetNextItem(-1,
1676 wxLIST_STATE_SELECTED
);
1678 // <Enter> or <Space> activate the selected item if any (but
1679 // not with Shift and/or Ctrl as then they have a predefined
1680 // meaning for the list view)
1682 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1683 !(wxIsShiftDown() || wxIsCtrlDown()) )
1685 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1689 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1690 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1694 event
.m_item
.m_itemId
= lItem
;
1698 // fill the other fields too
1699 event
.m_item
.m_text
= GetItemText(lItem
);
1700 event
.m_item
.m_data
= GetItemData(lItem
);
1706 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1708 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1713 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1714 // if it happened on an item (and not on empty place)
1715 if ( nmLV
->iItem
== -1 )
1721 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1722 event
.m_itemIndex
= nmLV
->iItem
;
1723 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1724 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1728 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1729 // don't do anything else
1730 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1735 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1736 LV_HITTESTINFO lvhti
;
1737 wxZeroMemory(lvhti
);
1739 ::GetCursorPos(&(lvhti
.pt
));
1740 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1741 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1743 if ( lvhti
.flags
& LVHT_ONITEM
)
1745 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1746 event
.m_itemIndex
= lvhti
.iItem
;
1747 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1748 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1753 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1754 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1756 *result
= OnCustomDraw(lParam
);
1759 #endif // _WIN32_IE >= 0x300
1761 case LVN_ODCACHEHINT
:
1763 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1765 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1767 // we get some really stupid cache hints like ones for items in
1768 // range 0..0 for an empty control or, after deleting an item,
1769 // for items in invalid range - filter this garbage out
1770 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1772 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1774 long iMax
= GetItemCount();
1775 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1785 case LVN_GETDISPINFO
:
1788 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1790 LV_ITEM
& lvi
= info
->item
;
1791 long item
= lvi
.iItem
;
1793 if ( lvi
.mask
& LVIF_TEXT
)
1795 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1796 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1799 if ( lvi
.mask
& LVIF_IMAGE
)
1801 lvi
.iImage
= OnGetItemImage(item
);
1804 // a little dose of healthy paranoia: as we never use
1805 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1806 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1807 _T("we don't support state callbacks yet!") );
1814 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1819 // where did this one come from?
1823 // process the event
1824 // -----------------
1826 event
.SetEventObject( this );
1827 event
.SetEventType(eventType
);
1829 if ( !GetEventHandler()->ProcessEvent(event
) )
1835 switch ( nmhdr
->code
)
1837 case LVN_DELETEALLITEMS
:
1838 // always return TRUE to suppress all additional LVN_DELETEITEM
1839 // notifications - this makes deleting all items from a list ctrl
1845 case LVN_ENDLABELEDIT
:
1846 // logic here is inversed compared to all the other messages
1847 *result
= event
.IsAllowed();
1852 *result
= !event
.IsAllowed();
1857 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1859 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1861 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1862 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1863 switch ( nmcd
.dwDrawStage
)
1866 // if we've got any items with non standard attributes,
1867 // notify us before painting each item
1869 // for virtual controls, always suppose that we have attributes as
1870 // there is no way to check for this
1871 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1874 case CDDS_ITEMPREPAINT
:
1876 size_t item
= (size_t)nmcd
.dwItemSpec
;
1877 if ( item
>= (size_t)GetItemCount() )
1879 // we get this message with item == 0 for an empty control,
1880 // we must ignore it as calling OnGetItemAttr() would be
1882 return CDRF_DODEFAULT
;
1885 wxListItemAttr
*attr
=
1886 IsVirtual() ? OnGetItemAttr(item
)
1887 : (wxListItemAttr
*)m_attrs
.Get(item
);
1891 // nothing to do for this item
1892 return CDRF_DODEFAULT
;
1896 wxColour colText
, colBack
;
1897 if ( attr
->HasFont() )
1899 wxFont font
= attr
->GetFont();
1900 hFont
= (HFONT
)font
.GetResourceHandle();
1907 if ( attr
->HasTextColour() )
1909 colText
= attr
->GetTextColour();
1913 colText
= GetTextColour();
1916 if ( attr
->HasBackgroundColour() )
1918 colBack
= attr
->GetBackgroundColour();
1922 colBack
= GetBackgroundColour();
1925 lplvcd
->clrText
= wxColourToRGB(colText
);
1926 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1928 // note that if we wanted to set colours for
1929 // individual columns (subitems), we would have
1930 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1933 ::SelectObject(nmcd
.hdc
, hFont
);
1935 return CDRF_NEWFONT
;
1938 // fall through to return CDRF_DODEFAULT
1941 return CDRF_DODEFAULT
;
1945 #endif // NM_CUSTOMDRAW supported
1947 // Necessary for drawing hrules and vrules, if specified
1948 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1952 wxControl::OnPaint(event
);
1954 // Reset the device origin since it may have been set
1955 dc
.SetDeviceOrigin(0, 0);
1957 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1958 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1960 if (!drawHRules
&& !drawVRules
)
1962 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1965 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1967 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1969 wxSize clientSize
= GetClientSize();
1973 int itemCount
= GetItemCount();
1977 long top
= GetTopItem();
1978 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
1980 if (GetItemRect(i
, itemRect
))
1982 cy
= itemRect
.GetTop();
1983 if (i
!= 0) // Don't draw the first one
1985 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1988 if (i
== itemCount
- 1)
1990 cy
= itemRect
.GetBottom();
1991 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1997 if (drawVRules
&& (i
> -1))
1999 wxRect firstItemRect
;
2000 GetItemRect(0, firstItemRect
);
2002 if (GetItemRect(i
, itemRect
))
2005 int x
= itemRect
.GetX();
2006 for (col
= 0; col
< GetColumnCount(); col
++)
2008 int colWidth
= GetColumnWidth(col
);
2010 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
2016 // ----------------------------------------------------------------------------
2017 // virtual list controls
2018 // ----------------------------------------------------------------------------
2020 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2022 // this is a pure virtual function, in fact - which is not really pure
2023 // because the controls which are not virtual don't need to implement it
2024 wxFAIL_MSG( _T("not supposed to be called") );
2026 return wxEmptyString
;
2029 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2032 wxFAIL_MSG( _T("not supposed to be called") );
2037 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2039 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2040 _T("invalid item index in OnGetItemAttr()") );
2042 // no attributes by default
2046 void wxListCtrl::SetItemCount(long count
)
2048 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2050 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2052 wxLogLastError(_T("ListView_SetItemCount"));
2056 void wxListCtrl::RefreshItem(long item
)
2058 if ( !ListView_Update(GetHwnd(), item
) )
2060 wxLogLastError(_T("ListView_Update"));
2064 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2066 wxRect rect1
, rect2
;
2067 GetItemRect(itemFrom
, rect1
);
2068 GetItemRect(itemTo
, rect2
);
2070 wxRect rect
= rect1
;
2071 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2076 // ----------------------------------------------------------------------------
2078 // ----------------------------------------------------------------------------
2080 // List item structure
2081 wxListItem::wxListItem()
2091 m_format
= wxLIST_FORMAT_CENTRE
;
2097 void wxListItem::Clear()
2106 m_format
= wxLIST_FORMAT_CENTRE
;
2108 m_text
= wxEmptyString
;
2110 if (m_attr
) delete m_attr
;
2114 void wxListItem::ClearAttributes()
2116 if (m_attr
) delete m_attr
;
2120 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2124 info
.m_data
= lvItem
.lParam
;
2127 info
.m_stateMask
= 0;
2128 info
.m_itemId
= lvItem
.iItem
;
2130 long oldMask
= lvItem
.mask
;
2132 bool needText
= FALSE
;
2133 if (hwndListCtrl
!= 0)
2135 if ( lvItem
.mask
& LVIF_TEXT
)
2142 lvItem
.pszText
= new wxChar
[513];
2143 lvItem
.cchTextMax
= 512;
2145 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2146 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2149 if ( lvItem
.mask
& LVIF_STATE
)
2151 info
.m_mask
|= wxLIST_MASK_STATE
;
2153 if ( lvItem
.stateMask
& LVIS_CUT
)
2155 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2156 if ( lvItem
.state
& LVIS_CUT
)
2157 info
.m_state
|= wxLIST_STATE_CUT
;
2159 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2161 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2162 if ( lvItem
.state
& LVIS_DROPHILITED
)
2163 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2165 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2167 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2168 if ( lvItem
.state
& LVIS_FOCUSED
)
2169 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2171 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2173 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2174 if ( lvItem
.state
& LVIS_SELECTED
)
2175 info
.m_state
|= wxLIST_STATE_SELECTED
;
2179 if ( lvItem
.mask
& LVIF_TEXT
)
2181 info
.m_mask
|= wxLIST_MASK_TEXT
;
2182 info
.m_text
= lvItem
.pszText
;
2184 if ( lvItem
.mask
& LVIF_IMAGE
)
2186 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2187 info
.m_image
= lvItem
.iImage
;
2189 if ( lvItem
.mask
& LVIF_PARAM
)
2190 info
.m_mask
|= wxLIST_MASK_DATA
;
2191 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2192 info
.m_mask
|= wxLIST_SET_ITEM
;
2193 info
.m_col
= lvItem
.iSubItem
;
2198 delete[] lvItem
.pszText
;
2200 lvItem
.mask
= oldMask
;
2203 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2205 if (stateMask
& wxLIST_STATE_CUT
)
2207 lvItem
.stateMask
|= LVIS_CUT
;
2208 if (state
& wxLIST_STATE_CUT
)
2209 lvItem
.state
|= LVIS_CUT
;
2211 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2213 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2214 if (state
& wxLIST_STATE_DROPHILITED
)
2215 lvItem
.state
|= LVIS_DROPHILITED
;
2217 if (stateMask
& wxLIST_STATE_FOCUSED
)
2219 lvItem
.stateMask
|= LVIS_FOCUSED
;
2220 if (state
& wxLIST_STATE_FOCUSED
)
2221 lvItem
.state
|= LVIS_FOCUSED
;
2223 if (stateMask
& wxLIST_STATE_SELECTED
)
2225 lvItem
.stateMask
|= LVIS_SELECTED
;
2226 if (state
& wxLIST_STATE_SELECTED
)
2227 lvItem
.state
|= LVIS_SELECTED
;
2231 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2232 const wxListItem
& info
,
2235 lvItem
.iItem
= (int) info
.m_itemId
;
2237 lvItem
.iImage
= info
.m_image
;
2238 lvItem
.lParam
= info
.m_data
;
2239 lvItem
.stateMask
= 0;
2242 lvItem
.iSubItem
= info
.m_col
;
2244 if (info
.m_mask
& wxLIST_MASK_STATE
)
2246 lvItem
.mask
|= LVIF_STATE
;
2248 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2251 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2253 lvItem
.mask
|= LVIF_TEXT
;
2254 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2256 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2260 // pszText is not const, hence the cast
2261 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2262 if ( lvItem
.pszText
)
2263 lvItem
.cchTextMax
= info
.m_text
.Length();
2265 lvItem
.cchTextMax
= 0;
2268 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2269 lvItem
.mask
|= LVIF_IMAGE
;
2270 if (info
.m_mask
& wxLIST_MASK_DATA
)
2271 lvItem
.mask
|= LVIF_PARAM
;
2274 // ----------------------------------------------------------------------------
2276 // ----------------------------------------------------------------------------
2278 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2280 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2281 : wxNotifyEvent(commandType
, id
)
2287 m_cancelled
= FALSE
;
2290 #endif // wxUSE_LISTCTRL