1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
38 #include "wx/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
46 #include "wx/msw/private.h"
48 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
49 #include "wx/msw/gnuwin32/extra.h"
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
72 #define LVM_FIRST 0x1000
76 #define HDM_FIRST 0x1200
79 // mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff
81 typedef struct tagNMLVCACHEHINT
88 #define NM_CACHEHINT NMLVCACHEHINT
91 #ifndef LVN_ODCACHEHINT
92 #define LVN_ODCACHEHINT (-113)
95 #ifndef ListView_GetHeader
96 #define ListView_GetHeader(w) (HWND)SendMessage((w),LVM_GETHEADER,0,0)
100 #define LVM_GETHEADER (LVM_FIRST+31)
103 #ifndef Header_GetItemRect
104 #define Header_GetItemRect(w,i,r) \
105 (BOOL)SendMessage((w),HDM_GETITEMRECT,(WPARAM)(i),(LPARAM)(r))
108 #ifndef HDM_GETITEMRECT
109 #define HDM_GETITEMRECT (HDM_FIRST+7)
113 #define LVCF_IMAGE 0x0010
116 #ifndef LVCFMT_BITMAP_ON_RIGHT
117 #define LVCFMT_BITMAP_ON_RIGHT 0x1000
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // convert our state and mask flags to LV_ITEM constants
125 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
127 // convert wxListItem to LV_ITEM
128 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
129 const wxListItem
& info
, LV_ITEM
& lvItem
);
131 // convert LV_ITEM to wxListItem
132 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
134 /* const */ LV_ITEM
& lvItem
);
136 // convert our wxListItem to LV_COLUMN
137 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
145 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
146 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
147 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
148 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
149 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
150 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
151 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
152 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
153 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
154 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
155 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
156 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
157 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
158 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
159 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
160 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
161 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
162 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
163 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
164 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
165 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
167 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
168 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
169 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
171 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
173 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
174 EVT_PAINT(wxListCtrl::OnPaint
)
177 // ============================================================================
179 // ============================================================================
181 // ----------------------------------------------------------------------------
182 // wxListCtrl construction
183 // ----------------------------------------------------------------------------
185 void wxListCtrl::Init()
187 m_imageListNormal
= NULL
;
188 m_imageListSmall
= NULL
;
189 m_imageListState
= NULL
;
190 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
194 m_hasAnyAttr
= FALSE
;
197 bool wxListCtrl::Create(wxWindow
*parent
,
202 const wxValidator
& validator
,
203 const wxString
& name
)
206 SetValidator(validator
);
207 #endif // wxUSE_VALIDATORS
216 m_windowStyle
= style
;
229 m_windowId
= (id
== -1) ? NewControlId() : id
;
231 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
232 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
234 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
235 wstyle
|= WS_CLIPSIBLINGS
;
237 if ( wxStyleHasBorder(m_windowStyle
) )
239 m_baseStyle
= wstyle
;
241 if ( !DoCreateControl(x
, y
, width
, height
) )
245 parent
->AddChild(this);
250 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
252 DWORD wstyle
= m_baseStyle
;
255 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
257 // Even with extended styles, need to combine with WS_BORDER
258 // for them to look right.
262 long oldStyle
= 0; // Dummy
263 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
265 // Create the ListView control.
266 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
271 GetWinHwnd(GetParent()),
278 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
283 // for comctl32.dll v 4.70+ we want to have this attribute because it's
284 // prettier (and also because wxGTK does it like this)
285 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
287 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
288 0, LVS_EX_FULLROWSELECT
);
291 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
292 SetForegroundColour(GetParent()->GetForegroundColour());
299 void wxListCtrl::UpdateStyle()
303 // The new window view style
305 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
306 dwStyleNew
|= m_baseStyle
;
308 // Get the current window style.
309 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
311 // Only set the window style if the view bits have changed.
312 if ( dwStyleOld
!= dwStyleNew
)
314 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
319 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
323 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
325 delete (wxListItemAttr
*)node
->Data();
331 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
334 m_hasAnyAttr
= FALSE
;
338 wxListCtrl::~wxListCtrl()
340 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
344 m_textCtrl
->SetHWND(0);
345 m_textCtrl
->UnsubclassWin();
350 if (m_ownsImageListNormal
) delete m_imageListNormal
;
351 if (m_ownsImageListSmall
) delete m_imageListSmall
;
352 if (m_ownsImageListState
) delete m_imageListState
;
355 // ----------------------------------------------------------------------------
356 // set/get/change style
357 // ----------------------------------------------------------------------------
359 // Add or remove a single window style
360 void wxListCtrl::SetSingleStyle(long style
, bool add
)
362 long flag
= GetWindowStyleFlag();
364 // Get rid of conflicting styles
367 if ( style
& wxLC_MASK_TYPE
)
368 flag
= flag
& ~wxLC_MASK_TYPE
;
369 if ( style
& wxLC_MASK_ALIGN
)
370 flag
= flag
& ~wxLC_MASK_ALIGN
;
371 if ( style
& wxLC_MASK_SORT
)
372 flag
= flag
& ~wxLC_MASK_SORT
;
388 m_windowStyle
= flag
;
393 // Set the whole window style
394 void wxListCtrl::SetWindowStyleFlag(long flag
)
396 m_windowStyle
= flag
;
401 // Can be just a single style, or a bitlist
402 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
405 if ( style
& wxLC_ICON
)
407 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
408 oldStyle
-= LVS_SMALLICON
;
409 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
410 oldStyle
-= LVS_REPORT
;
411 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
412 oldStyle
-= LVS_LIST
;
416 if ( style
& wxLC_SMALL_ICON
)
418 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
419 oldStyle
-= LVS_ICON
;
420 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
421 oldStyle
-= LVS_REPORT
;
422 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
423 oldStyle
-= LVS_LIST
;
424 wstyle
|= LVS_SMALLICON
;
427 if ( style
& wxLC_LIST
)
429 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
430 oldStyle
-= LVS_ICON
;
431 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
432 oldStyle
-= LVS_REPORT
;
433 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
434 oldStyle
-= LVS_SMALLICON
;
438 if ( style
& wxLC_REPORT
)
440 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
441 oldStyle
-= LVS_ICON
;
442 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
443 oldStyle
-= LVS_LIST
;
444 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
445 oldStyle
-= LVS_SMALLICON
;
447 wstyle
|= LVS_REPORT
;
450 if ( style
& wxLC_ALIGN_LEFT
)
452 if ( oldStyle
& LVS_ALIGNTOP
)
453 oldStyle
-= LVS_ALIGNTOP
;
454 wstyle
|= LVS_ALIGNLEFT
;
457 if ( style
& wxLC_ALIGN_TOP
)
459 if ( oldStyle
& LVS_ALIGNLEFT
)
460 oldStyle
-= LVS_ALIGNLEFT
;
461 wstyle
|= LVS_ALIGNTOP
;
464 if ( style
& wxLC_AUTOARRANGE
)
465 wstyle
|= LVS_AUTOARRANGE
;
467 if ( style
& wxLC_NO_SORT_HEADER
)
468 wstyle
|= LVS_NOSORTHEADER
;
470 if ( style
& wxLC_NO_HEADER
)
471 wstyle
|= LVS_NOCOLUMNHEADER
;
473 if ( style
& wxLC_EDIT_LABELS
)
474 wstyle
|= LVS_EDITLABELS
;
476 if ( style
& wxLC_SINGLE_SEL
)
477 wstyle
|= LVS_SINGLESEL
;
479 if ( style
& wxLC_SORT_ASCENDING
)
481 if ( oldStyle
& LVS_SORTDESCENDING
)
482 oldStyle
-= LVS_SORTDESCENDING
;
483 wstyle
|= LVS_SORTASCENDING
;
486 if ( style
& wxLC_SORT_DESCENDING
)
488 if ( oldStyle
& LVS_SORTASCENDING
)
489 oldStyle
-= LVS_SORTASCENDING
;
490 wstyle
|= LVS_SORTDESCENDING
;
493 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
494 if ( style
& wxLC_VIRTUAL
)
496 int ver
= wxTheApp
->GetComCtl32Version();
499 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."),
500 ver
/ 100, ver
% 100);
503 wstyle
|= LVS_OWNERDATA
;
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 // Sets the foreground, i.e. text, colour
515 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
517 if ( !wxWindow::SetForegroundColour(col
) )
520 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
525 // Sets the background colour
526 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
528 if ( !wxWindow::SetBackgroundColour(col
) )
531 // we set the same colour for both the "empty" background and the items
533 COLORREF color
= wxColourToRGB(col
);
534 ListView_SetBkColor(GetHwnd(), color
);
535 ListView_SetTextBkColor(GetHwnd(), color
);
540 // Gets information about this column
541 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
546 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
548 lvCol
.mask
|= LVCF_TEXT
;
549 lvCol
.pszText
= new wxChar
[513];
550 lvCol
.cchTextMax
= 512;
553 bool success
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0;
555 // item.m_subItem = lvCol.iSubItem;
556 item
.m_width
= lvCol
.cx
;
558 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
560 item
.m_text
= lvCol
.pszText
;
561 delete[] lvCol
.pszText
;
564 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
566 if (lvCol
.fmt
== LVCFMT_LEFT
)
567 item
.m_format
= wxLIST_FORMAT_LEFT
;
568 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
569 item
.m_format
= wxLIST_FORMAT_RIGHT
;
570 else if (lvCol
.fmt
== LVCFMT_CENTER
)
571 item
.m_format
= wxLIST_FORMAT_CENTRE
;
577 // Sets information about this column
578 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
581 wxConvertToMSWListCol(col
, item
, lvCol
);
583 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
586 // Gets the column width
587 int wxListCtrl::GetColumnWidth(int col
) const
589 return ListView_GetColumnWidth(GetHwnd(), col
);
592 // Sets the column width
593 bool wxListCtrl::SetColumnWidth(int col
, int width
)
596 if ( m_windowStyle
& wxLC_LIST
)
600 if ( width2
== wxLIST_AUTOSIZE
)
601 width2
= LVSCW_AUTOSIZE
;
602 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
603 width2
= LVSCW_AUTOSIZE_USEHEADER
;
605 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
608 // Gets the number of items that can fit vertically in the
609 // visible area of the list control (list or report view)
610 // or the total number of items in the list control (icon
611 // or small icon view)
612 int wxListCtrl::GetCountPerPage() const
614 return ListView_GetCountPerPage(GetHwnd());
617 // Gets the edit control for editing labels.
618 wxTextCtrl
* wxListCtrl::GetEditControl() const
623 // Gets information about the item
624 bool wxListCtrl::GetItem(wxListItem
& info
) const
627 wxZeroMemory(lvItem
);
629 lvItem
.iItem
= info
.m_itemId
;
630 lvItem
.iSubItem
= info
.m_col
;
632 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
634 lvItem
.mask
|= LVIF_TEXT
;
635 lvItem
.pszText
= new wxChar
[513];
636 lvItem
.cchTextMax
= 512;
640 lvItem
.pszText
= NULL
;
643 if (info
.m_mask
& wxLIST_MASK_DATA
)
644 lvItem
.mask
|= LVIF_PARAM
;
646 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
647 lvItem
.mask
|= LVIF_IMAGE
;
649 if ( info
.m_mask
& wxLIST_MASK_STATE
)
651 lvItem
.mask
|= LVIF_STATE
;
652 // the other bits are hardly interesting anyhow
653 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
656 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
659 wxLogError(_("Couldn't retrieve information about list control item %d."),
664 // give NULL as hwnd as we already have everything we need
665 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
669 delete[] lvItem
.pszText
;
674 // Sets information about the item
675 bool wxListCtrl::SetItem(wxListItem
& info
)
678 wxConvertToMSWListItem(this, info
, item
);
680 // we could be changing only the attribute in which case we don't need to
681 // call ListView_SetItem() at all
685 if ( !ListView_SetItem(GetHwnd(), &item
) )
687 wxLogDebug(_T("ListView_SetItem() failed"));
693 // we need to update the item immediately to show the new image
694 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
696 // check whether it has any custom attributes
697 if ( info
.HasAttributes() )
699 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
702 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
704 *attr
= *info
.GetAttributes();
708 // if the colour has changed, we must redraw the item
714 // we need this to make the change visible right now
715 RefreshItem(item
.iItem
);
721 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
725 info
.m_mask
= wxLIST_MASK_TEXT
;
726 info
.m_itemId
= index
;
730 info
.m_image
= imageId
;
731 info
.m_mask
|= wxLIST_MASK_IMAGE
;
733 return SetItem(info
);
737 // Gets the item state
738 int wxListCtrl::GetItemState(long item
, long stateMask
) const
742 info
.m_mask
= wxLIST_MASK_STATE
;
743 info
.m_stateMask
= stateMask
;
744 info
.m_itemId
= item
;
752 // Sets the item state
753 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
755 // NB: don't use SetItem() here as it doesn't work with the virtual list
758 wxZeroMemory(lvItem
);
760 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
762 // for the virtual list controls we need to refresh the previously focused
763 // item manually when changing focus without changing selection
764 // programmatically because otherwise it keeps its focus rectangle until
765 // next repaint (yet another comctl32 bug)
768 (stateMask
& wxLIST_STATE_FOCUSED
) &&
769 (state
& wxLIST_STATE_FOCUSED
) )
771 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
778 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
779 (WPARAM
)item
, (LPARAM
)&lvItem
) )
781 wxLogLastError(_T("ListView_SetItemState"));
786 if ( focusOld
!= -1 )
788 // no need to refresh the item if it was previously selected, it would
789 // only result in annoying flicker
790 if ( !(GetItemState(focusOld
,
791 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
793 RefreshItem(focusOld
);
800 // Sets the item image
801 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
805 info
.m_mask
= wxLIST_MASK_IMAGE
;
806 info
.m_image
= image
;
807 info
.m_itemId
= item
;
809 return SetItem(info
);
812 // Gets the item text
813 wxString
wxListCtrl::GetItemText(long item
) const
817 info
.m_mask
= wxLIST_MASK_TEXT
;
818 info
.m_itemId
= item
;
825 // Sets the item text
826 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
830 info
.m_mask
= wxLIST_MASK_TEXT
;
831 info
.m_itemId
= item
;
837 // Gets the item data
838 long wxListCtrl::GetItemData(long item
) const
842 info
.m_mask
= wxLIST_MASK_DATA
;
843 info
.m_itemId
= item
;
850 // Sets the item data
851 bool wxListCtrl::SetItemData(long item
, long data
)
855 info
.m_mask
= wxLIST_MASK_DATA
;
856 info
.m_itemId
= item
;
859 return SetItem(info
);
862 // Gets the item rectangle
863 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
868 if ( code
== wxLIST_RECT_BOUNDS
)
869 codeWin
= LVIR_BOUNDS
;
870 else if ( code
== wxLIST_RECT_ICON
)
872 else if ( code
== wxLIST_RECT_LABEL
)
873 codeWin
= LVIR_LABEL
;
876 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
878 codeWin
= LVIR_BOUNDS
;
882 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
) != 0;
884 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
887 rect
.x
= rectWin
.left
;
888 rect
.y
= rectWin
.top
;
889 rect
.width
= rectWin
.right
- rectWin
.left
;
890 rect
.height
= rectWin
.bottom
- rectWin
.top
;
895 // Gets the item position
896 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
900 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
902 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
906 // Sets the item position.
907 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
909 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
912 // Gets the number of items in the list control
913 int wxListCtrl::GetItemCount() const
915 return ListView_GetItemCount(GetHwnd());
918 // Retrieves the spacing between icons in pixels.
919 // If small is TRUE, gets the spacing for the small icon
920 // view, otherwise the large icon view.
921 int wxListCtrl::GetItemSpacing(bool isSmall
) const
923 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
926 // Gets the number of selected items in the list control
927 int wxListCtrl::GetSelectedItemCount() const
929 return ListView_GetSelectedCount(GetHwnd());
932 // Gets the text colour of the listview
933 wxColour
wxListCtrl::GetTextColour() const
935 COLORREF ref
= ListView_GetTextColor(GetHwnd());
936 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
940 // Sets the text colour of the listview
941 void wxListCtrl::SetTextColour(const wxColour
& col
)
943 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
946 // Gets the index of the topmost visible item when in
947 // list or report view
948 long wxListCtrl::GetTopItem() const
950 return (long) ListView_GetTopIndex(GetHwnd());
953 // Searches for an item, starting from 'item'.
954 // 'geometry' is one of
955 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
956 // 'state' is a state bit flag, one or more of
957 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
958 // item can be -1 to find the first item that matches the
960 // Returns the item or -1 if unsuccessful.
961 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
965 if ( geom
== wxLIST_NEXT_ABOVE
)
967 if ( geom
== wxLIST_NEXT_ALL
)
969 if ( geom
== wxLIST_NEXT_BELOW
)
971 if ( geom
== wxLIST_NEXT_LEFT
)
972 flags
|= LVNI_TOLEFT
;
973 if ( geom
== wxLIST_NEXT_RIGHT
)
974 flags
|= LVNI_TORIGHT
;
976 if ( state
& wxLIST_STATE_CUT
)
978 if ( state
& wxLIST_STATE_DROPHILITED
)
979 flags
|= LVNI_DROPHILITED
;
980 if ( state
& wxLIST_STATE_FOCUSED
)
981 flags
|= LVNI_FOCUSED
;
982 if ( state
& wxLIST_STATE_SELECTED
)
983 flags
|= LVNI_SELECTED
;
985 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
989 wxImageList
*wxListCtrl::GetImageList(int which
) const
991 if ( which
== wxIMAGE_LIST_NORMAL
)
993 return m_imageListNormal
;
995 else if ( which
== wxIMAGE_LIST_SMALL
)
997 return m_imageListSmall
;
999 else if ( which
== wxIMAGE_LIST_STATE
)
1001 return m_imageListState
;
1006 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1009 if ( which
== wxIMAGE_LIST_NORMAL
)
1011 flags
= LVSIL_NORMAL
;
1012 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1013 m_imageListNormal
= imageList
;
1014 m_ownsImageListNormal
= FALSE
;
1016 else if ( which
== wxIMAGE_LIST_SMALL
)
1018 flags
= LVSIL_SMALL
;
1019 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1020 m_imageListSmall
= imageList
;
1021 m_ownsImageListSmall
= FALSE
;
1023 else if ( which
== wxIMAGE_LIST_STATE
)
1025 flags
= LVSIL_STATE
;
1026 if (m_ownsImageListState
) delete m_imageListState
;
1027 m_imageListState
= imageList
;
1028 m_ownsImageListState
= FALSE
;
1030 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1033 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1035 SetImageList(imageList
, which
);
1036 if ( which
== wxIMAGE_LIST_NORMAL
)
1037 m_ownsImageListNormal
= TRUE
;
1038 else if ( which
== wxIMAGE_LIST_SMALL
)
1039 m_ownsImageListSmall
= TRUE
;
1040 else if ( which
== wxIMAGE_LIST_STATE
)
1041 m_ownsImageListState
= TRUE
;
1044 // ----------------------------------------------------------------------------
1046 // ----------------------------------------------------------------------------
1048 // Arranges the items
1049 bool wxListCtrl::Arrange(int flag
)
1052 if ( flag
== wxLIST_ALIGN_LEFT
)
1053 code
= LVA_ALIGNLEFT
;
1054 else if ( flag
== wxLIST_ALIGN_TOP
)
1055 code
= LVA_ALIGNTOP
;
1056 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1058 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1059 code
= LVA_SNAPTOGRID
;
1061 return (ListView_Arrange(GetHwnd(), code
) != 0);
1065 bool wxListCtrl::DeleteItem(long item
)
1067 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1069 wxLogLastError(_T("ListView_DeleteItem"));
1073 // the virtual list control doesn't refresh itself correctly, help it
1076 // we need to refresh all the lines below the one which was deleted
1078 if ( item
> 0 && GetItemCount() )
1080 GetItemRect(item
- 1, rectItem
);
1085 rectItem
.height
= 0;
1088 wxRect rectWin
= GetRect();
1089 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1090 rectWin
.y
= rectItem
.GetBottom();
1092 RefreshRect(rectWin
);
1098 // Deletes all items
1099 bool wxListCtrl::DeleteAllItems()
1101 return ListView_DeleteAllItems(GetHwnd()) != 0;
1104 // Deletes all items
1105 bool wxListCtrl::DeleteAllColumns()
1107 while ( m_colCount
> 0 )
1109 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1111 wxLogLastError(wxT("ListView_DeleteColumn"));
1119 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1125 bool wxListCtrl::DeleteColumn(int col
)
1127 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1129 if ( success
&& (m_colCount
> 0) )
1134 // Clears items, and columns if there are any.
1135 void wxListCtrl::ClearAll()
1138 if ( m_colCount
> 0 )
1142 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1144 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1146 // VS: ListView_EditLabel requires that the list has focus.
1148 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1152 m_textCtrl
->SetHWND(0);
1153 m_textCtrl
->UnsubclassWin();
1158 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1159 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1160 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1165 // End label editing, optionally cancelling the edit
1166 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1168 wxFAIL_MSG( _T("not implemented") );
1173 // Ensures this item is visible
1174 bool wxListCtrl::EnsureVisible(long item
)
1176 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1179 // Find an item whose label matches this string, starting from the item after 'start'
1180 // or the beginning if 'start' is -1.
1181 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1183 LV_FINDINFO findInfo
;
1185 findInfo
.flags
= LVFI_STRING
;
1187 findInfo
.flags
|= LVFI_PARTIAL
;
1190 // ListView_FindItem() excludes the first item from search and to look
1191 // through all the items you need to start from -1 which is unnatural and
1192 // inconsistent with the generic version - so we adjust the index
1195 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1198 // Find an item whose data matches this data, starting from the item after 'start'
1199 // or the beginning if 'start' is -1.
1200 long wxListCtrl::FindItem(long start
, long data
)
1202 LV_FINDINFO findInfo
;
1204 findInfo
.flags
= LVFI_PARAM
;
1205 findInfo
.lParam
= data
;
1207 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1210 // Find an item nearest this position in the specified direction, starting from
1211 // the item after 'start' or the beginning if 'start' is -1.
1212 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1214 LV_FINDINFO findInfo
;
1216 findInfo
.flags
= LVFI_NEARESTXY
;
1217 findInfo
.pt
.x
= pt
.x
;
1218 findInfo
.pt
.y
= pt
.y
;
1219 findInfo
.vkDirection
= VK_RIGHT
;
1221 if ( direction
== wxLIST_FIND_UP
)
1222 findInfo
.vkDirection
= VK_UP
;
1223 else if ( direction
== wxLIST_FIND_DOWN
)
1224 findInfo
.vkDirection
= VK_DOWN
;
1225 else if ( direction
== wxLIST_FIND_LEFT
)
1226 findInfo
.vkDirection
= VK_LEFT
;
1227 else if ( direction
== wxLIST_FIND_RIGHT
)
1228 findInfo
.vkDirection
= VK_RIGHT
;
1230 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1233 // Determines which item (if any) is at the specified point,
1234 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1235 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1237 LV_HITTESTINFO hitTestInfo
;
1238 hitTestInfo
.pt
.x
= (int) point
.x
;
1239 hitTestInfo
.pt
.y
= (int) point
.y
;
1241 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1244 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1245 flags
|= wxLIST_HITTEST_ABOVE
;
1246 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1247 flags
|= wxLIST_HITTEST_BELOW
;
1248 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1249 flags
|= wxLIST_HITTEST_NOWHERE
;
1250 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1251 flags
|= wxLIST_HITTEST_ONITEMICON
;
1252 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1253 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1254 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1255 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1256 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1257 flags
|= wxLIST_HITTEST_TOLEFT
;
1258 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1259 flags
|= wxLIST_HITTEST_TORIGHT
;
1261 return (long) hitTestInfo
.iItem
;
1264 // Inserts an item, returning the index of the new item if successful,
1266 long wxListCtrl::InsertItem(wxListItem
& info
)
1268 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1271 wxConvertToMSWListItem(this, info
, item
);
1273 // check whether it has any custom attributes
1274 if ( info
.HasAttributes() )
1277 wxListItemAttr
*attr
;
1278 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1282 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1284 else *attr
= *info
.GetAttributes();
1286 m_hasAnyAttr
= TRUE
;
1289 return (long) ListView_InsertItem(GetHwnd(), & item
);
1292 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1295 info
.m_text
= label
;
1296 info
.m_mask
= wxLIST_MASK_TEXT
;
1297 info
.m_itemId
= index
;
1298 return InsertItem(info
);
1301 // Inserts an image item
1302 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1305 info
.m_image
= imageIndex
;
1306 info
.m_mask
= wxLIST_MASK_IMAGE
;
1307 info
.m_itemId
= index
;
1308 return InsertItem(info
);
1311 // Inserts an image/string item
1312 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1315 info
.m_image
= imageIndex
;
1316 info
.m_text
= label
;
1317 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1318 info
.m_itemId
= index
;
1319 return InsertItem(info
);
1322 // For list view mode (only), inserts a column.
1323 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1326 wxConvertToMSWListCol(col
, item
, lvCol
);
1328 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1330 // always give some width to the new column: this one is compatible
1331 // with the generic version
1332 lvCol
.mask
|= LVCF_WIDTH
;
1336 // when we insert a column which can contain an image, we must specify this
1337 // flag right now as doing it later in SetColumn() has no effect
1339 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1340 // way to dynamically set/clear the bitmap as the column without a bitmap
1341 // on the left looks ugly (there is a hole)
1343 // unfortunately with my version of comctl32.dll (5.80), the left column
1344 // image is always on the left and it seems that it's a "feature" - I
1345 // didn't find any way to work around it in any case
1346 if ( lvCol
.mask
& LVCF_IMAGE
)
1348 lvCol
.mask
|= LVCF_FMT
;
1349 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1352 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1359 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1366 long wxListCtrl::InsertColumn(long col
,
1367 const wxString
& heading
,
1372 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1373 item
.m_text
= heading
;
1376 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1377 item
.m_width
= width
;
1379 item
.m_format
= format
;
1381 return InsertColumn(col
, item
);
1384 // Scrolls the list control. If in icon, small icon or report view mode,
1385 // x specifies the number of pixels to scroll. If in list view mode, x
1386 // specifies the number of columns to scroll.
1387 // If in icon, small icon or list view mode, y specifies the number of pixels
1388 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1389 bool wxListCtrl::ScrollList(int dx
, int dy
)
1391 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1396 // fn is a function which takes 3 long arguments: item1, item2, data.
1397 // item1 is the long data associated with a first item (NOT the index).
1398 // item2 is the long data associated with a second item (NOT the index).
1399 // data is the same value as passed to SortItems.
1400 // The return value is a negative number if the first item should precede the second
1401 // item, a positive number of the second item should precede the first,
1402 // or zero if the two items are equivalent.
1404 // data is arbitrary data to be passed to the sort function.
1406 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1407 // time for anything better right now (VZ)
1408 static long gs_sortData
= 0;
1409 static wxListCtrl
*gs_sortCtrl
= NULL
;
1410 static wxListCtrlCompare gs_sortFunction
= NULL
;
1412 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1414 int n1
= *(const int *)arg1
,
1415 n2
= *(const int *)arg2
;
1417 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1418 gs_sortCtrl
->GetItemData(n2
),
1422 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1424 // sort the attributes too
1428 count
= GetItemCount();
1429 int *aItems
= new int[count
];
1430 for ( n
= 0; n
< count
; n
++ )
1437 gs_sortFunction
= fn
;
1439 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1443 gs_sortFunction
= NULL
;
1445 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1446 for ( n
= 0; n
< count
; n
++ )
1448 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1451 attrsNew
.Put(n
, attr
);
1461 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1463 wxLogDebug(_T("ListView_SortItems() failed"));
1473 // ----------------------------------------------------------------------------
1474 // message processing
1475 // ----------------------------------------------------------------------------
1477 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1479 if (cmd
== EN_UPDATE
)
1481 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1482 event
.SetEventObject( this );
1483 ProcessCommand(event
);
1486 else if (cmd
== EN_KILLFOCUS
)
1488 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1489 event
.SetEventObject( this );
1490 ProcessCommand(event
);
1497 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1499 // prepare the event
1500 // -----------------
1502 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1503 event
.SetEventObject(this);
1505 wxEventType eventType
= wxEVT_NULL
;
1507 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1509 // if your compiler is as broken as this, you should really change it: this
1510 // code is needed for normal operation! #ifdef below is only useful for
1511 // automatic rebuilds which are done with a very old compiler version
1512 #ifdef HDN_BEGINTRACKA
1514 // check for messages from the header (in report view)
1515 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1517 // is it a message from the header?
1518 if ( nmhdr
->hwndFrom
== hwndHdr
)
1520 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1522 event
.m_itemIndex
= -1;
1524 switch ( nmhdr
->code
)
1526 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1527 // TRACK messages even to ANSI programs: on my system I get
1528 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1530 // work around is to simply catch both versions and hope that it
1531 // works (why should this message exist in ANSI and Unicode is
1532 // beyond me as it doesn't deal with strings at all...)
1533 case HDN_BEGINTRACKA
:
1534 case HDN_BEGINTRACKW
:
1535 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1540 if ( eventType
== wxEVT_NULL
)
1541 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1546 if ( eventType
== wxEVT_NULL
)
1547 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1548 event
.m_col
= nmHDR
->iItem
;
1553 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1556 // find the column clicked: we have to search for it
1557 // ourselves as the notification message doesn't provide
1560 // where did the click occur?
1562 if ( !::GetCursorPos(&ptClick
) )
1564 wxLogLastError(_T("GetCursorPos"));
1567 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1569 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1572 event
.m_pointDrag
.x
= ptClick
.x
;
1573 event
.m_pointDrag
.y
= ptClick
.y
;
1575 int colCount
= Header_GetItemCount(hwndHdr
);
1578 for ( int col
= 0; col
< colCount
; col
++ )
1580 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1582 if ( ::PtInRect(&rect
, ptClick
) )
1593 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1597 #endif // defined(HDN_BEGINTRACKA)
1598 if ( nmhdr
->hwndFrom
== GetHwnd() )
1600 // almost all messages use NM_LISTVIEW
1601 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1603 // this is true for almost all events
1604 event
.m_item
.m_data
= nmLV
->lParam
;
1606 switch ( nmhdr
->code
)
1608 case LVN_BEGINRDRAG
:
1609 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1613 if ( eventType
== wxEVT_NULL
)
1615 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1618 event
.m_itemIndex
= nmLV
->iItem
;
1619 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1620 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1623 case LVN_BEGINLABELEDIT
:
1625 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1626 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1627 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1628 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1632 case LVN_COLUMNCLICK
:
1633 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1634 event
.m_itemIndex
= -1;
1635 event
.m_col
= nmLV
->iSubItem
;
1638 case LVN_DELETEALLITEMS
:
1639 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1640 event
.m_itemIndex
= -1;
1646 case LVN_DELETEITEM
:
1647 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1648 event
.m_itemIndex
= nmLV
->iItem
;
1652 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1656 case LVN_ENDLABELEDIT
:
1658 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1659 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1660 wxConvertFromMSWListItem(NULL
, event
.m_item
, info
->item
);
1661 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1664 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1668 case LVN_SETDISPINFO
:
1670 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1671 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1672 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1676 case LVN_INSERTITEM
:
1677 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1678 event
.m_itemIndex
= nmLV
->iItem
;
1681 case LVN_ITEMCHANGED
:
1682 // we translate this catch all message into more interesting
1683 // (and more easy to process) wxWindows events
1685 // first of all, we deal with the state change events only
1686 if ( nmLV
->uChanged
& LVIF_STATE
)
1688 // temp vars for readability
1689 const UINT stOld
= nmLV
->uOldState
;
1690 const UINT stNew
= nmLV
->uNewState
;
1692 // has the focus changed?
1693 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1695 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1696 event
.m_itemIndex
= nmLV
->iItem
;
1699 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1701 if ( eventType
!= wxEVT_NULL
)
1703 // focus and selection have both changed: send the
1704 // focus event from here and the selection one
1706 event
.SetEventType(eventType
);
1707 (void)GetEventHandler()->ProcessEvent(event
);
1709 else // no focus event to send
1711 // then need to set m_itemIndex as it wasn't done
1713 event
.m_itemIndex
= nmLV
->iItem
;
1716 eventType
= stNew
& LVIS_SELECTED
1717 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1718 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1722 if ( eventType
== wxEVT_NULL
)
1724 // not an interesting event for us
1732 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1733 WORD wVKey
= info
->wVKey
;
1735 // get the current selection
1736 long lItem
= GetNextItem(-1,
1738 wxLIST_STATE_SELECTED
);
1740 // <Enter> or <Space> activate the selected item if any (but
1741 // not with Shift and/or Ctrl as then they have a predefined
1742 // meaning for the list view)
1744 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1745 !(wxIsShiftDown() || wxIsCtrlDown()) )
1747 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1751 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1753 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1754 // value which should be used as is
1755 int code
= wxCharCodeMSWToWX(wVKey
);
1756 event
.m_code
= code
? code
: wVKey
;
1760 event
.m_item
.m_itemId
= lItem
;
1764 // fill the other fields too
1765 event
.m_item
.m_text
= GetItemText(lItem
);
1766 event
.m_item
.m_data
= GetItemData(lItem
);
1772 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1774 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1779 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1780 // if it happened on an item (and not on empty place)
1781 if ( nmLV
->iItem
== -1 )
1787 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1788 event
.m_itemIndex
= nmLV
->iItem
;
1789 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1790 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1794 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1795 // don't do anything else
1796 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1801 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1802 LV_HITTESTINFO lvhti
;
1803 wxZeroMemory(lvhti
);
1805 ::GetCursorPos(&(lvhti
.pt
));
1806 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1807 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1809 if ( lvhti
.flags
& LVHT_ONITEM
)
1811 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1812 event
.m_itemIndex
= lvhti
.iItem
;
1813 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1814 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1819 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1820 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1822 *result
= OnCustomDraw(lParam
);
1825 #endif // _WIN32_IE >= 0x300
1827 case LVN_ODCACHEHINT
:
1829 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1831 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1833 // we get some really stupid cache hints like ones for items in
1834 // range 0..0 for an empty control or, after deleting an item,
1835 // for items in invalid range - filter this garbage out
1836 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1838 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1840 long iMax
= GetItemCount();
1841 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1851 case LVN_GETDISPINFO
:
1854 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1856 LV_ITEM
& lvi
= info
->item
;
1857 long item
= lvi
.iItem
;
1859 if ( lvi
.mask
& LVIF_TEXT
)
1861 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1862 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1865 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1866 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
1867 if ( lvi
.mask
& LVIF_IMAGE
)
1869 lvi
.iImage
= OnGetItemImage(item
);
1873 // a little dose of healthy paranoia: as we never use
1874 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1875 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1876 _T("we don't support state callbacks yet!") );
1883 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1888 // where did this one come from?
1892 // process the event
1893 // -----------------
1895 event
.SetEventType(eventType
);
1897 if ( !GetEventHandler()->ProcessEvent(event
) )
1903 switch ( nmhdr
->code
)
1905 case LVN_DELETEALLITEMS
:
1906 // always return TRUE to suppress all additional LVN_DELETEITEM
1907 // notifications - this makes deleting all items from a list ctrl
1913 case LVN_ENDLABELEDIT
:
1914 // logic here is inversed compared to all the other messages
1915 *result
= event
.IsAllowed();
1920 *result
= !event
.IsAllowed();
1925 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1927 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1929 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1930 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1931 switch ( nmcd
.dwDrawStage
)
1934 // if we've got any items with non standard attributes,
1935 // notify us before painting each item
1937 // for virtual controls, always suppose that we have attributes as
1938 // there is no way to check for this
1939 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1942 case CDDS_ITEMPREPAINT
:
1944 size_t item
= (size_t)nmcd
.dwItemSpec
;
1945 if ( item
>= (size_t)GetItemCount() )
1947 // we get this message with item == 0 for an empty control,
1948 // we must ignore it as calling OnGetItemAttr() would be
1950 return CDRF_DODEFAULT
;
1953 wxListItemAttr
*attr
=
1954 IsVirtual() ? OnGetItemAttr(item
)
1955 : (wxListItemAttr
*)m_attrs
.Get(item
);
1959 // nothing to do for this item
1960 return CDRF_DODEFAULT
;
1964 wxColour colText
, colBack
;
1965 if ( attr
->HasFont() )
1967 wxFont font
= attr
->GetFont();
1968 hFont
= (HFONT
)font
.GetResourceHandle();
1975 if ( attr
->HasTextColour() )
1977 colText
= attr
->GetTextColour();
1981 colText
= GetTextColour();
1984 if ( attr
->HasBackgroundColour() )
1986 colBack
= attr
->GetBackgroundColour();
1990 colBack
= GetBackgroundColour();
1993 lplvcd
->clrText
= wxColourToRGB(colText
);
1994 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1996 // note that if we wanted to set colours for
1997 // individual columns (subitems), we would have
1998 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2001 ::SelectObject(nmcd
.hdc
, hFont
);
2003 return CDRF_NEWFONT
;
2006 // fall through to return CDRF_DODEFAULT
2009 return CDRF_DODEFAULT
;
2013 #endif // NM_CUSTOMDRAW supported
2015 // Necessary for drawing hrules and vrules, if specified
2016 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2020 wxControl::OnPaint(event
);
2022 // Reset the device origin since it may have been set
2023 dc
.SetDeviceOrigin(0, 0);
2025 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2026 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2028 if (!drawHRules
&& !drawVRules
)
2030 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2033 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2035 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2037 wxSize clientSize
= GetClientSize();
2041 int itemCount
= GetItemCount();
2045 long top
= GetTopItem();
2046 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2048 if (GetItemRect(i
, itemRect
))
2050 cy
= itemRect
.GetTop();
2051 if (i
!= 0) // Don't draw the first one
2053 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2056 if (i
== itemCount
- 1)
2058 cy
= itemRect
.GetBottom();
2059 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2065 if (drawVRules
&& (i
> -1))
2067 wxRect firstItemRect
;
2068 GetItemRect(0, firstItemRect
);
2070 if (GetItemRect(i
, itemRect
))
2073 int x
= itemRect
.GetX();
2074 for (col
= 0; col
< GetColumnCount(); col
++)
2076 int colWidth
= GetColumnWidth(col
);
2078 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
2084 // ----------------------------------------------------------------------------
2085 // virtual list controls
2086 // ----------------------------------------------------------------------------
2088 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2090 // this is a pure virtual function, in fact - which is not really pure
2091 // because the controls which are not virtual don't need to implement it
2092 wxFAIL_MSG( _T("not supposed to be called") );
2094 return wxEmptyString
;
2097 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2100 wxFAIL_MSG( _T("not supposed to be called") );
2105 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2107 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2108 _T("invalid item index in OnGetItemAttr()") );
2110 // no attributes by default
2114 void wxListCtrl::SetItemCount(long count
)
2116 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2118 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2120 wxLogLastError(_T("ListView_SetItemCount"));
2124 void wxListCtrl::RefreshItem(long item
)
2126 // strangely enough, ListView_Update() results in much more flicker here
2127 // than a dumb Refresh() -- why?
2129 if ( !ListView_Update(GetHwnd(), item
) )
2131 wxLogLastError(_T("ListView_Update"));
2135 GetItemRect(item
, rect
);
2140 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2142 wxRect rect1
, rect2
;
2143 GetItemRect(itemFrom
, rect1
);
2144 GetItemRect(itemTo
, rect2
);
2146 wxRect rect
= rect1
;
2147 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2152 // ----------------------------------------------------------------------------
2154 // ----------------------------------------------------------------------------
2156 // List item structure
2157 wxListItem::wxListItem()
2167 m_format
= wxLIST_FORMAT_CENTRE
;
2173 void wxListItem::Clear()
2182 m_format
= wxLIST_FORMAT_CENTRE
;
2184 m_text
= wxEmptyString
;
2186 if (m_attr
) delete m_attr
;
2190 void wxListItem::ClearAttributes()
2192 if (m_attr
) delete m_attr
;
2196 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2200 info
.m_data
= lvItem
.lParam
;
2203 info
.m_stateMask
= 0;
2204 info
.m_itemId
= lvItem
.iItem
;
2206 long oldMask
= lvItem
.mask
;
2208 bool needText
= FALSE
;
2209 if (hwndListCtrl
!= 0)
2211 if ( lvItem
.mask
& LVIF_TEXT
)
2218 lvItem
.pszText
= new wxChar
[513];
2219 lvItem
.cchTextMax
= 512;
2221 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2222 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2225 if ( lvItem
.mask
& LVIF_STATE
)
2227 info
.m_mask
|= wxLIST_MASK_STATE
;
2229 if ( lvItem
.stateMask
& LVIS_CUT
)
2231 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2232 if ( lvItem
.state
& LVIS_CUT
)
2233 info
.m_state
|= wxLIST_STATE_CUT
;
2235 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2237 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2238 if ( lvItem
.state
& LVIS_DROPHILITED
)
2239 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2241 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2243 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2244 if ( lvItem
.state
& LVIS_FOCUSED
)
2245 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2247 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2249 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2250 if ( lvItem
.state
& LVIS_SELECTED
)
2251 info
.m_state
|= wxLIST_STATE_SELECTED
;
2255 if ( lvItem
.mask
& LVIF_TEXT
)
2257 info
.m_mask
|= wxLIST_MASK_TEXT
;
2258 info
.m_text
= lvItem
.pszText
;
2260 if ( lvItem
.mask
& LVIF_IMAGE
)
2262 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2263 info
.m_image
= lvItem
.iImage
;
2265 if ( lvItem
.mask
& LVIF_PARAM
)
2266 info
.m_mask
|= wxLIST_MASK_DATA
;
2267 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2268 info
.m_mask
|= wxLIST_SET_ITEM
;
2269 info
.m_col
= lvItem
.iSubItem
;
2274 delete[] lvItem
.pszText
;
2276 lvItem
.mask
= oldMask
;
2279 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2281 if (stateMask
& wxLIST_STATE_CUT
)
2283 lvItem
.stateMask
|= LVIS_CUT
;
2284 if (state
& wxLIST_STATE_CUT
)
2285 lvItem
.state
|= LVIS_CUT
;
2287 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2289 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2290 if (state
& wxLIST_STATE_DROPHILITED
)
2291 lvItem
.state
|= LVIS_DROPHILITED
;
2293 if (stateMask
& wxLIST_STATE_FOCUSED
)
2295 lvItem
.stateMask
|= LVIS_FOCUSED
;
2296 if (state
& wxLIST_STATE_FOCUSED
)
2297 lvItem
.state
|= LVIS_FOCUSED
;
2299 if (stateMask
& wxLIST_STATE_SELECTED
)
2301 lvItem
.stateMask
|= LVIS_SELECTED
;
2302 if (state
& wxLIST_STATE_SELECTED
)
2303 lvItem
.state
|= LVIS_SELECTED
;
2307 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2308 const wxListItem
& info
,
2311 lvItem
.iItem
= (int) info
.m_itemId
;
2313 lvItem
.iImage
= info
.m_image
;
2314 lvItem
.lParam
= info
.m_data
;
2315 lvItem
.stateMask
= 0;
2318 lvItem
.iSubItem
= info
.m_col
;
2320 if (info
.m_mask
& wxLIST_MASK_STATE
)
2322 lvItem
.mask
|= LVIF_STATE
;
2324 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2327 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2329 lvItem
.mask
|= LVIF_TEXT
;
2330 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2332 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2336 // pszText is not const, hence the cast
2337 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2338 if ( lvItem
.pszText
)
2339 lvItem
.cchTextMax
= info
.m_text
.Length();
2341 lvItem
.cchTextMax
= 0;
2344 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2345 lvItem
.mask
|= LVIF_IMAGE
;
2346 if (info
.m_mask
& wxLIST_MASK_DATA
)
2347 lvItem
.mask
|= LVIF_PARAM
;
2350 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2353 wxZeroMemory(lvCol
);
2355 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2357 lvCol
.mask
|= LVCF_TEXT
;
2358 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2361 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2363 lvCol
.mask
|= LVCF_FMT
;
2365 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2366 lvCol
.fmt
= LVCFMT_LEFT
;
2367 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2368 lvCol
.fmt
= LVCFMT_RIGHT
;
2369 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2370 lvCol
.fmt
= LVCFMT_CENTER
;
2373 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2375 lvCol
.mask
|= LVCF_WIDTH
;
2376 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2377 lvCol
.cx
= LVSCW_AUTOSIZE
;
2378 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2379 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2381 lvCol
.cx
= item
.m_width
;
2384 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2385 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2386 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2388 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2390 lvCol
.mask
|= LVCF_IMAGE
;
2391 lvCol
.iImage
= item
.m_image
;
2393 //else: it doesn't support item images anyhow
2398 #endif // wxUSE_LISTCTRL