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
71 // mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff
73 typedef struct tagNMLVCACHEHINT
80 #define NM_CACHEHINT NMLVCACHEHINT
83 #ifndef LVN_ODCACHEHINT
84 #define LVN_ODCACHEHINT (-113)
87 #ifndef ListView_GetHeader
88 #define ListView_GetHeader(w) (HWND)SendMessage((w),LVM_GETHEADER,0,0)
92 #define LVM_GETHEADER (LVM_FIRST+31)
95 #ifndef Header_GetItemRect
96 #define Header_GetItemRect(w,i,r) \
97 (BOOL)SendMessage((w),HDM_GETITEMRECT,(WPARAM)(i),(LPARAM)(r))
100 #ifndef HDM_GETITEMRECT
101 #define HDM_GETITEMRECT (HDM_FIRST+7)
105 #define LVCF_IMAGE 0x0010
108 #ifndef LVCFMT_BITMAP_ON_RIGHT
109 #define LVCFMT_BITMAP_ON_RIGHT 0x1000
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // convert our state and mask flags to LV_ITEM constants
117 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
119 // convert wxListItem to LV_ITEM
120 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
121 const wxListItem
& info
, LV_ITEM
& lvItem
);
123 // convert LV_ITEM to wxListItem
124 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
126 /* const */ LV_ITEM
& lvItem
);
128 // convert our wxListItem to LV_COLUMN
129 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
137 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
138 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
139 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
140 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
141 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
142 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
143 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
144 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
145 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
146 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
147 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
148 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
149 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
150 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
151 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
152 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
153 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
154 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
155 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
156 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
157 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
159 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
160 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
161 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
163 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
165 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
166 EVT_PAINT(wxListCtrl::OnPaint
)
169 // ============================================================================
171 // ============================================================================
173 // ----------------------------------------------------------------------------
174 // wxListCtrl construction
175 // ----------------------------------------------------------------------------
177 void wxListCtrl::Init()
179 m_imageListNormal
= NULL
;
180 m_imageListSmall
= NULL
;
181 m_imageListState
= NULL
;
182 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
186 m_hasAnyAttr
= FALSE
;
189 bool wxListCtrl::Create(wxWindow
*parent
,
194 const wxValidator
& validator
,
195 const wxString
& name
)
198 SetValidator(validator
);
199 #endif // wxUSE_VALIDATORS
208 m_windowStyle
= style
;
221 m_windowId
= (id
== -1) ? NewControlId() : id
;
223 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
224 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
226 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
227 wstyle
|= WS_CLIPSIBLINGS
;
229 if ( wxStyleHasBorder(m_windowStyle
) )
231 m_baseStyle
= wstyle
;
233 if ( !DoCreateControl(x
, y
, width
, height
) )
237 parent
->AddChild(this);
242 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
244 DWORD wstyle
= m_baseStyle
;
247 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
249 // Even with extended styles, need to combine with WS_BORDER
250 // for them to look right.
254 long oldStyle
= 0; // Dummy
255 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
257 // Create the ListView control.
258 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
263 GetWinHwnd(GetParent()),
270 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
275 // for comctl32.dll v 4.70+ we want to have this attribute because it's
276 // prettier (and also because wxGTK does it like this)
277 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
279 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
280 0, LVS_EX_FULLROWSELECT
);
283 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
284 SetForegroundColour(GetParent()->GetForegroundColour());
291 void wxListCtrl::UpdateStyle()
295 // The new window view style
297 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
298 dwStyleNew
|= m_baseStyle
;
300 // Get the current window style.
301 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
303 // Only set the window style if the view bits have changed.
304 if ( dwStyleOld
!= dwStyleNew
)
306 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
311 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
315 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
317 delete (wxListItemAttr
*)node
->Data();
323 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
326 m_hasAnyAttr
= FALSE
;
330 wxListCtrl::~wxListCtrl()
332 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
336 m_textCtrl
->SetHWND(0);
337 m_textCtrl
->UnsubclassWin();
342 if (m_ownsImageListNormal
) delete m_imageListNormal
;
343 if (m_ownsImageListSmall
) delete m_imageListSmall
;
344 if (m_ownsImageListState
) delete m_imageListState
;
347 // ----------------------------------------------------------------------------
348 // set/get/change style
349 // ----------------------------------------------------------------------------
351 // Add or remove a single window style
352 void wxListCtrl::SetSingleStyle(long style
, bool add
)
354 long flag
= GetWindowStyleFlag();
356 // Get rid of conflicting styles
359 if ( style
& wxLC_MASK_TYPE
)
360 flag
= flag
& ~wxLC_MASK_TYPE
;
361 if ( style
& wxLC_MASK_ALIGN
)
362 flag
= flag
& ~wxLC_MASK_ALIGN
;
363 if ( style
& wxLC_MASK_SORT
)
364 flag
= flag
& ~wxLC_MASK_SORT
;
380 m_windowStyle
= flag
;
385 // Set the whole window style
386 void wxListCtrl::SetWindowStyleFlag(long flag
)
388 m_windowStyle
= flag
;
393 // Can be just a single style, or a bitlist
394 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
397 if ( style
& wxLC_ICON
)
399 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
400 oldStyle
-= LVS_SMALLICON
;
401 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
402 oldStyle
-= LVS_REPORT
;
403 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
404 oldStyle
-= LVS_LIST
;
408 if ( style
& wxLC_SMALL_ICON
)
410 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
411 oldStyle
-= LVS_ICON
;
412 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
413 oldStyle
-= LVS_REPORT
;
414 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
415 oldStyle
-= LVS_LIST
;
416 wstyle
|= LVS_SMALLICON
;
419 if ( style
& wxLC_LIST
)
421 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
422 oldStyle
-= LVS_ICON
;
423 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
424 oldStyle
-= LVS_REPORT
;
425 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
426 oldStyle
-= LVS_SMALLICON
;
430 if ( style
& wxLC_REPORT
)
432 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
433 oldStyle
-= LVS_ICON
;
434 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
435 oldStyle
-= LVS_LIST
;
436 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
437 oldStyle
-= LVS_SMALLICON
;
439 wstyle
|= LVS_REPORT
;
442 if ( style
& wxLC_ALIGN_LEFT
)
444 if ( oldStyle
& LVS_ALIGNTOP
)
445 oldStyle
-= LVS_ALIGNTOP
;
446 wstyle
|= LVS_ALIGNLEFT
;
449 if ( style
& wxLC_ALIGN_TOP
)
451 if ( oldStyle
& LVS_ALIGNLEFT
)
452 oldStyle
-= LVS_ALIGNLEFT
;
453 wstyle
|= LVS_ALIGNTOP
;
456 if ( style
& wxLC_AUTOARRANGE
)
457 wstyle
|= LVS_AUTOARRANGE
;
459 if ( style
& wxLC_NO_SORT_HEADER
)
460 wstyle
|= LVS_NOSORTHEADER
;
462 if ( style
& wxLC_NO_HEADER
)
463 wstyle
|= LVS_NOCOLUMNHEADER
;
465 if ( style
& wxLC_EDIT_LABELS
)
466 wstyle
|= LVS_EDITLABELS
;
468 if ( style
& wxLC_SINGLE_SEL
)
469 wstyle
|= LVS_SINGLESEL
;
471 if ( style
& wxLC_SORT_ASCENDING
)
473 if ( oldStyle
& LVS_SORTDESCENDING
)
474 oldStyle
-= LVS_SORTDESCENDING
;
475 wstyle
|= LVS_SORTASCENDING
;
478 if ( style
& wxLC_SORT_DESCENDING
)
480 if ( oldStyle
& LVS_SORTASCENDING
)
481 oldStyle
-= LVS_SORTASCENDING
;
482 wstyle
|= LVS_SORTDESCENDING
;
485 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
486 if ( style
& wxLC_VIRTUAL
)
488 int ver
= wxTheApp
->GetComCtl32Version();
491 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."),
492 ver
/ 100, ver
% 100);
495 wstyle
|= LVS_OWNERDATA
;
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 // Sets the foreground, i.e. text, colour
507 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
509 if ( !wxWindow::SetForegroundColour(col
) )
512 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
517 // Sets the background colour
518 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
520 if ( !wxWindow::SetBackgroundColour(col
) )
523 // we set the same colour for both the "empty" background and the items
525 COLORREF color
= wxColourToRGB(col
);
526 ListView_SetBkColor(GetHwnd(), color
);
527 ListView_SetTextBkColor(GetHwnd(), color
);
532 // Gets information about this column
533 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
538 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
540 lvCol
.mask
|= LVCF_TEXT
;
541 lvCol
.pszText
= new wxChar
[513];
542 lvCol
.cchTextMax
= 512;
545 bool success
= ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0;
547 // item.m_subItem = lvCol.iSubItem;
548 item
.m_width
= lvCol
.cx
;
550 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
552 item
.m_text
= lvCol
.pszText
;
553 delete[] lvCol
.pszText
;
556 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
558 if (lvCol
.fmt
== LVCFMT_LEFT
)
559 item
.m_format
= wxLIST_FORMAT_LEFT
;
560 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
561 item
.m_format
= wxLIST_FORMAT_RIGHT
;
562 else if (lvCol
.fmt
== LVCFMT_CENTER
)
563 item
.m_format
= wxLIST_FORMAT_CENTRE
;
569 // Sets information about this column
570 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
573 wxConvertToMSWListCol(col
, item
, lvCol
);
575 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
578 // Gets the column width
579 int wxListCtrl::GetColumnWidth(int col
) const
581 return ListView_GetColumnWidth(GetHwnd(), col
);
584 // Sets the column width
585 bool wxListCtrl::SetColumnWidth(int col
, int width
)
588 if ( m_windowStyle
& wxLC_LIST
)
592 if ( width2
== wxLIST_AUTOSIZE
)
593 width2
= LVSCW_AUTOSIZE
;
594 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
595 width2
= LVSCW_AUTOSIZE_USEHEADER
;
597 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
600 // Gets the number of items that can fit vertically in the
601 // visible area of the list control (list or report view)
602 // or the total number of items in the list control (icon
603 // or small icon view)
604 int wxListCtrl::GetCountPerPage() const
606 return ListView_GetCountPerPage(GetHwnd());
609 // Gets the edit control for editing labels.
610 wxTextCtrl
* wxListCtrl::GetEditControl() const
615 // Gets information about the item
616 bool wxListCtrl::GetItem(wxListItem
& info
) const
619 wxZeroMemory(lvItem
);
621 lvItem
.iItem
= info
.m_itemId
;
622 lvItem
.iSubItem
= info
.m_col
;
624 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
626 lvItem
.mask
|= LVIF_TEXT
;
627 lvItem
.pszText
= new wxChar
[513];
628 lvItem
.cchTextMax
= 512;
632 lvItem
.pszText
= NULL
;
635 if (info
.m_mask
& wxLIST_MASK_DATA
)
636 lvItem
.mask
|= LVIF_PARAM
;
638 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
639 lvItem
.mask
|= LVIF_IMAGE
;
641 if ( info
.m_mask
& wxLIST_MASK_STATE
)
643 lvItem
.mask
|= LVIF_STATE
;
644 // the other bits are hardly interesting anyhow
645 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
648 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
651 wxLogError(_("Couldn't retrieve information about list control item %d."),
656 // give NULL as hwnd as we already have everything we need
657 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
661 delete[] lvItem
.pszText
;
666 // Sets information about the item
667 bool wxListCtrl::SetItem(wxListItem
& info
)
670 wxConvertToMSWListItem(this, info
, item
);
672 // we could be changing only the attribute in which case we don't need to
673 // call ListView_SetItem() at all
677 if ( !ListView_SetItem(GetHwnd(), &item
) )
679 wxLogDebug(_T("ListView_SetItem() failed"));
685 // we need to update the item immediately to show the new image
686 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
688 // check whether it has any custom attributes
689 if ( info
.HasAttributes() )
691 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
694 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
696 *attr
= *info
.GetAttributes();
700 // if the colour has changed, we must redraw the item
706 // we need this to make the change visible right now
707 RefreshItem(item
.iItem
);
713 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
717 info
.m_mask
= wxLIST_MASK_TEXT
;
718 info
.m_itemId
= index
;
722 info
.m_image
= imageId
;
723 info
.m_mask
|= wxLIST_MASK_IMAGE
;
725 return SetItem(info
);
729 // Gets the item state
730 int wxListCtrl::GetItemState(long item
, long stateMask
) const
734 info
.m_mask
= wxLIST_MASK_STATE
;
735 info
.m_stateMask
= stateMask
;
736 info
.m_itemId
= item
;
744 // Sets the item state
745 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
747 // NB: don't use SetItem() here as it doesn't work with the virtual list
750 wxZeroMemory(lvItem
);
752 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
754 // for the virtual list controls we need to refresh the previously focused
755 // item manually when changing focus programmatically because otherwise it
756 // keeps its focus rectangle until next repaint (yet another comctl32 bug)
759 (stateMask
& wxLIST_STATE_FOCUSED
) &&
760 (state
& wxLIST_STATE_FOCUSED
) )
762 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
769 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
770 (WPARAM
)item
, (LPARAM
)&lvItem
) )
772 wxLogLastError(_T("ListView_SetItemState"));
777 if ( focusOld
!= -1 )
779 RefreshItem(focusOld
);
785 // Sets the item image
786 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
790 info
.m_mask
= wxLIST_MASK_IMAGE
;
791 info
.m_image
= image
;
792 info
.m_itemId
= item
;
794 return SetItem(info
);
797 // Gets the item text
798 wxString
wxListCtrl::GetItemText(long item
) const
802 info
.m_mask
= wxLIST_MASK_TEXT
;
803 info
.m_itemId
= item
;
810 // Sets the item text
811 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
815 info
.m_mask
= wxLIST_MASK_TEXT
;
816 info
.m_itemId
= item
;
822 // Gets the item data
823 long wxListCtrl::GetItemData(long item
) const
827 info
.m_mask
= wxLIST_MASK_DATA
;
828 info
.m_itemId
= item
;
835 // Sets the item data
836 bool wxListCtrl::SetItemData(long item
, long data
)
840 info
.m_mask
= wxLIST_MASK_DATA
;
841 info
.m_itemId
= item
;
844 return SetItem(info
);
847 // Gets the item rectangle
848 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
853 if ( code
== wxLIST_RECT_BOUNDS
)
854 codeWin
= LVIR_BOUNDS
;
855 else if ( code
== wxLIST_RECT_ICON
)
857 else if ( code
== wxLIST_RECT_LABEL
)
858 codeWin
= LVIR_LABEL
;
861 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
863 codeWin
= LVIR_BOUNDS
;
867 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
) != 0;
869 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
872 rect
.x
= rectWin
.left
;
873 rect
.y
= rectWin
.top
;
874 rect
.width
= rectWin
.right
- rectWin
.left
;
875 rect
.height
= rectWin
.bottom
- rectWin
.top
;
880 // Gets the item position
881 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
885 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
887 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
891 // Sets the item position.
892 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
894 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
897 // Gets the number of items in the list control
898 int wxListCtrl::GetItemCount() const
900 return ListView_GetItemCount(GetHwnd());
903 // Retrieves the spacing between icons in pixels.
904 // If small is TRUE, gets the spacing for the small icon
905 // view, otherwise the large icon view.
906 int wxListCtrl::GetItemSpacing(bool isSmall
) const
908 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
911 // Gets the number of selected items in the list control
912 int wxListCtrl::GetSelectedItemCount() const
914 return ListView_GetSelectedCount(GetHwnd());
917 // Gets the text colour of the listview
918 wxColour
wxListCtrl::GetTextColour() const
920 COLORREF ref
= ListView_GetTextColor(GetHwnd());
921 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
925 // Sets the text colour of the listview
926 void wxListCtrl::SetTextColour(const wxColour
& col
)
928 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
931 // Gets the index of the topmost visible item when in
932 // list or report view
933 long wxListCtrl::GetTopItem() const
935 return (long) ListView_GetTopIndex(GetHwnd());
938 // Searches for an item, starting from 'item'.
939 // 'geometry' is one of
940 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
941 // 'state' is a state bit flag, one or more of
942 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
943 // item can be -1 to find the first item that matches the
945 // Returns the item or -1 if unsuccessful.
946 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
950 if ( geom
== wxLIST_NEXT_ABOVE
)
952 if ( geom
== wxLIST_NEXT_ALL
)
954 if ( geom
== wxLIST_NEXT_BELOW
)
956 if ( geom
== wxLIST_NEXT_LEFT
)
957 flags
|= LVNI_TOLEFT
;
958 if ( geom
== wxLIST_NEXT_RIGHT
)
959 flags
|= LVNI_TORIGHT
;
961 if ( state
& wxLIST_STATE_CUT
)
963 if ( state
& wxLIST_STATE_DROPHILITED
)
964 flags
|= LVNI_DROPHILITED
;
965 if ( state
& wxLIST_STATE_FOCUSED
)
966 flags
|= LVNI_FOCUSED
;
967 if ( state
& wxLIST_STATE_SELECTED
)
968 flags
|= LVNI_SELECTED
;
970 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
974 wxImageList
*wxListCtrl::GetImageList(int which
) const
976 if ( which
== wxIMAGE_LIST_NORMAL
)
978 return m_imageListNormal
;
980 else if ( which
== wxIMAGE_LIST_SMALL
)
982 return m_imageListSmall
;
984 else if ( which
== wxIMAGE_LIST_STATE
)
986 return m_imageListState
;
991 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
994 if ( which
== wxIMAGE_LIST_NORMAL
)
996 flags
= LVSIL_NORMAL
;
997 if (m_ownsImageListNormal
) delete m_imageListNormal
;
998 m_imageListNormal
= imageList
;
999 m_ownsImageListNormal
= FALSE
;
1001 else if ( which
== wxIMAGE_LIST_SMALL
)
1003 flags
= LVSIL_SMALL
;
1004 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1005 m_imageListSmall
= imageList
;
1006 m_ownsImageListSmall
= FALSE
;
1008 else if ( which
== wxIMAGE_LIST_STATE
)
1010 flags
= LVSIL_STATE
;
1011 if (m_ownsImageListState
) delete m_imageListState
;
1012 m_imageListState
= imageList
;
1013 m_ownsImageListState
= FALSE
;
1015 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1018 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1020 SetImageList(imageList
, which
);
1021 if ( which
== wxIMAGE_LIST_NORMAL
)
1022 m_ownsImageListNormal
= TRUE
;
1023 else if ( which
== wxIMAGE_LIST_SMALL
)
1024 m_ownsImageListSmall
= TRUE
;
1025 else if ( which
== wxIMAGE_LIST_STATE
)
1026 m_ownsImageListState
= TRUE
;
1029 // ----------------------------------------------------------------------------
1031 // ----------------------------------------------------------------------------
1033 // Arranges the items
1034 bool wxListCtrl::Arrange(int flag
)
1037 if ( flag
== wxLIST_ALIGN_LEFT
)
1038 code
= LVA_ALIGNLEFT
;
1039 else if ( flag
== wxLIST_ALIGN_TOP
)
1040 code
= LVA_ALIGNTOP
;
1041 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1043 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1044 code
= LVA_SNAPTOGRID
;
1046 return (ListView_Arrange(GetHwnd(), code
) != 0);
1050 bool wxListCtrl::DeleteItem(long item
)
1052 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1054 wxLogLastError(_T("ListView_DeleteItem"));
1058 // the virtual list control doesn't refresh itself correctly, help it
1061 // we need to refresh all the lines below the one which was deleted
1063 if ( item
> 0 && GetItemCount() )
1065 GetItemRect(item
- 1, rectItem
);
1070 rectItem
.height
= 0;
1073 wxRect rectWin
= GetRect();
1074 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1075 rectWin
.y
= rectItem
.GetBottom();
1077 RefreshRect(rectWin
);
1083 // Deletes all items
1084 bool wxListCtrl::DeleteAllItems()
1086 return ListView_DeleteAllItems(GetHwnd()) != 0;
1089 // Deletes all items
1090 bool wxListCtrl::DeleteAllColumns()
1092 while ( m_colCount
> 0 )
1094 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1096 wxLogLastError(wxT("ListView_DeleteColumn"));
1104 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1110 bool wxListCtrl::DeleteColumn(int col
)
1112 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1114 if ( success
&& (m_colCount
> 0) )
1119 // Clears items, and columns if there are any.
1120 void wxListCtrl::ClearAll()
1123 if ( m_colCount
> 0 )
1127 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1129 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1131 // VS: ListView_EditLabel requires that the list has focus.
1133 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1137 m_textCtrl
->SetHWND(0);
1138 m_textCtrl
->UnsubclassWin();
1143 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1144 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1145 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1150 // End label editing, optionally cancelling the edit
1151 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1153 wxFAIL_MSG( _T("not implemented") );
1158 // Ensures this item is visible
1159 bool wxListCtrl::EnsureVisible(long item
)
1161 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1164 // Find an item whose label matches this string, starting from the item after 'start'
1165 // or the beginning if 'start' is -1.
1166 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1168 LV_FINDINFO findInfo
;
1170 findInfo
.flags
= LVFI_STRING
;
1172 findInfo
.flags
|= LVFI_PARTIAL
;
1175 // ListView_FindItem() excludes the first item from search and to look
1176 // through all the items you need to start from -1 which is unnatural and
1177 // inconsistent with the generic version - so we adjust the index
1180 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1183 // Find an item whose data matches this data, starting from the item after 'start'
1184 // or the beginning if 'start' is -1.
1185 long wxListCtrl::FindItem(long start
, long data
)
1187 LV_FINDINFO findInfo
;
1189 findInfo
.flags
= LVFI_PARAM
;
1190 findInfo
.lParam
= data
;
1192 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1195 // Find an item nearest this position in the specified direction, starting from
1196 // the item after 'start' or the beginning if 'start' is -1.
1197 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1199 LV_FINDINFO findInfo
;
1201 findInfo
.flags
= LVFI_NEARESTXY
;
1202 findInfo
.pt
.x
= pt
.x
;
1203 findInfo
.pt
.y
= pt
.y
;
1204 findInfo
.vkDirection
= VK_RIGHT
;
1206 if ( direction
== wxLIST_FIND_UP
)
1207 findInfo
.vkDirection
= VK_UP
;
1208 else if ( direction
== wxLIST_FIND_DOWN
)
1209 findInfo
.vkDirection
= VK_DOWN
;
1210 else if ( direction
== wxLIST_FIND_LEFT
)
1211 findInfo
.vkDirection
= VK_LEFT
;
1212 else if ( direction
== wxLIST_FIND_RIGHT
)
1213 findInfo
.vkDirection
= VK_RIGHT
;
1215 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1218 // Determines which item (if any) is at the specified point,
1219 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1220 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1222 LV_HITTESTINFO hitTestInfo
;
1223 hitTestInfo
.pt
.x
= (int) point
.x
;
1224 hitTestInfo
.pt
.y
= (int) point
.y
;
1226 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1229 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1230 flags
|= wxLIST_HITTEST_ABOVE
;
1231 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1232 flags
|= wxLIST_HITTEST_BELOW
;
1233 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1234 flags
|= wxLIST_HITTEST_NOWHERE
;
1235 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1236 flags
|= wxLIST_HITTEST_ONITEMICON
;
1237 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1238 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1239 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1240 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1241 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1242 flags
|= wxLIST_HITTEST_TOLEFT
;
1243 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1244 flags
|= wxLIST_HITTEST_TORIGHT
;
1246 return (long) hitTestInfo
.iItem
;
1249 // Inserts an item, returning the index of the new item if successful,
1251 long wxListCtrl::InsertItem(wxListItem
& info
)
1253 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1256 wxConvertToMSWListItem(this, info
, item
);
1258 // check whether it has any custom attributes
1259 if ( info
.HasAttributes() )
1262 wxListItemAttr
*attr
;
1263 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1267 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1269 else *attr
= *info
.GetAttributes();
1271 m_hasAnyAttr
= TRUE
;
1274 return (long) ListView_InsertItem(GetHwnd(), & item
);
1277 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1280 info
.m_text
= label
;
1281 info
.m_mask
= wxLIST_MASK_TEXT
;
1282 info
.m_itemId
= index
;
1283 return InsertItem(info
);
1286 // Inserts an image item
1287 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1290 info
.m_image
= imageIndex
;
1291 info
.m_mask
= wxLIST_MASK_IMAGE
;
1292 info
.m_itemId
= index
;
1293 return InsertItem(info
);
1296 // Inserts an image/string item
1297 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1300 info
.m_image
= imageIndex
;
1301 info
.m_text
= label
;
1302 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1303 info
.m_itemId
= index
;
1304 return InsertItem(info
);
1307 // For list view mode (only), inserts a column.
1308 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1311 wxConvertToMSWListCol(col
, item
, lvCol
);
1313 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1315 // always give some width to the new column: this one is compatible
1316 // with the generic version
1317 lvCol
.mask
|= LVCF_WIDTH
;
1321 // when we insert a column which can contain an image, we must specify this
1322 // flag right now as doing it later in SetColumn() has no effect
1324 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1325 // way to dynamically set/clear the bitmap as the column without a bitmap
1326 // on the left looks ugly (there is a hole)
1328 // unfortunately with my version of comctl32.dll (5.80), the left column
1329 // image is always on the left and it seems that it's a "feature" - I
1330 // didn't find any way to work around it in any case
1331 if ( lvCol
.mask
& LVCF_IMAGE
)
1333 lvCol
.mask
|= LVCF_FMT
;
1334 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1337 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1344 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1351 long wxListCtrl::InsertColumn(long col
,
1352 const wxString
& heading
,
1357 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1358 item
.m_text
= heading
;
1361 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1362 item
.m_width
= width
;
1364 item
.m_format
= format
;
1366 return InsertColumn(col
, item
);
1369 // Scrolls the list control. If in icon, small icon or report view mode,
1370 // x specifies the number of pixels to scroll. If in list view mode, x
1371 // specifies the number of columns to scroll.
1372 // If in icon, small icon or list view mode, y specifies the number of pixels
1373 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1374 bool wxListCtrl::ScrollList(int dx
, int dy
)
1376 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1381 // fn is a function which takes 3 long arguments: item1, item2, data.
1382 // item1 is the long data associated with a first item (NOT the index).
1383 // item2 is the long data associated with a second item (NOT the index).
1384 // data is the same value as passed to SortItems.
1385 // The return value is a negative number if the first item should precede the second
1386 // item, a positive number of the second item should precede the first,
1387 // or zero if the two items are equivalent.
1389 // data is arbitrary data to be passed to the sort function.
1391 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1392 // time for anything better right now (VZ)
1393 static long gs_sortData
= 0;
1394 static wxListCtrl
*gs_sortCtrl
= NULL
;
1395 static wxListCtrlCompare gs_sortFunction
= NULL
;
1397 int wxCMPFUNC_CONV
wxListCtrlCompareFn(const void *arg1
, const void *arg2
)
1399 int n1
= *(const int *)arg1
,
1400 n2
= *(const int *)arg2
;
1402 return gs_sortFunction(gs_sortCtrl
->GetItemData(n1
),
1403 gs_sortCtrl
->GetItemData(n2
),
1407 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1409 // sort the attributes too
1413 count
= GetItemCount();
1414 int *aItems
= new int[count
];
1415 for ( n
= 0; n
< count
; n
++ )
1422 gs_sortFunction
= fn
;
1424 qsort(aItems
, count
, sizeof(int), wxListCtrlCompareFn
);
1428 gs_sortFunction
= NULL
;
1430 wxHashTable
attrsNew(wxKEY_INTEGER
, 1000);
1431 for ( n
= 0; n
< count
; n
++ )
1433 wxObject
*attr
= m_attrs
.Delete(aItems
[n
]);
1436 attrsNew
.Put(n
, attr
);
1446 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
)fn
, data
) )
1448 wxLogDebug(_T("ListView_SortItems() failed"));
1458 // ----------------------------------------------------------------------------
1459 // message processing
1460 // ----------------------------------------------------------------------------
1462 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1464 if (cmd
== EN_UPDATE
)
1466 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1467 event
.SetEventObject( this );
1468 ProcessCommand(event
);
1471 else if (cmd
== EN_KILLFOCUS
)
1473 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1474 event
.SetEventObject( this );
1475 ProcessCommand(event
);
1482 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1484 // prepare the event
1485 // -----------------
1487 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1488 event
.SetEventObject(this);
1490 wxEventType eventType
= wxEVT_NULL
;
1492 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1494 // if your compiler is as broken as this, you should really change it: this
1495 // code is needed for normal operation! #ifdef below is only useful for
1496 // automatic rebuilds which are done with a very old compiler version
1499 // check for messages from the header (in report view)
1500 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1502 // is it a message from the header?
1503 if ( nmhdr
->hwndFrom
== hwndHdr
)
1505 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1506 event
.m_itemIndex
= -1;
1508 switch ( nmhdr
->code
)
1510 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1511 // TRACK messages even to ANSI programs: on my system I get
1512 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1514 // work around is to simply catch both versions and hope that it
1515 // works (why should this message exist in ANSI and Unicode is
1516 // beyond me as it doesn't deal with strings at all...)
1517 case HDN_BEGINTRACKA
:
1518 case HDN_BEGINTRACKW
:
1519 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1524 if ( eventType
== wxEVT_NULL
)
1525 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1530 if ( eventType
== wxEVT_NULL
)
1531 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1532 event
.m_col
= nmHDR
->iItem
;
1537 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1540 // find the column clicked: we have to search for it
1541 // ourselves as the notification message doesn't provide
1544 // where did the click occur?
1546 if ( !::GetCursorPos(&ptClick
) )
1548 wxLogLastError(_T("GetCursorPos"));
1551 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1553 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1556 event
.m_pointDrag
.x
= ptClick
.x
;
1557 event
.m_pointDrag
.y
= ptClick
.y
;
1559 int colCount
= Header_GetItemCount(hwndHdr
);
1562 for ( int col
= 0; col
< colCount
; col
++ )
1564 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1566 if ( ::PtInRect(&rect
, ptClick
) )
1577 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1581 #endif // defined(LVM_FIRST)
1582 if ( nmhdr
->hwndFrom
== GetHwnd() )
1584 // almost all messages use NM_LISTVIEW
1585 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1587 // this is true for almost all events
1588 event
.m_item
.m_data
= nmLV
->lParam
;
1590 switch ( nmhdr
->code
)
1592 case LVN_BEGINRDRAG
:
1593 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1597 if ( eventType
== wxEVT_NULL
)
1599 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1602 event
.m_itemIndex
= nmLV
->iItem
;
1603 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1604 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1607 case LVN_BEGINLABELEDIT
:
1609 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1610 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1611 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1612 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1616 case LVN_COLUMNCLICK
:
1617 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1618 event
.m_itemIndex
= -1;
1619 event
.m_col
= nmLV
->iSubItem
;
1622 case LVN_DELETEALLITEMS
:
1623 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1624 event
.m_itemIndex
= -1;
1630 case LVN_DELETEITEM
:
1631 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1632 event
.m_itemIndex
= nmLV
->iItem
;
1636 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1640 case LVN_ENDLABELEDIT
:
1642 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1643 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1644 wxConvertFromMSWListItem(NULL
, event
.m_item
, info
->item
);
1645 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1648 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1652 case LVN_SETDISPINFO
:
1654 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1655 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1656 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1660 case LVN_INSERTITEM
:
1661 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1662 event
.m_itemIndex
= nmLV
->iItem
;
1665 case LVN_ITEMCHANGED
:
1666 // we translate this catch all message into more interesting
1667 // (and more easy to process) wxWindows events
1669 // first of all, we deal with the state change events only
1670 if ( nmLV
->uChanged
& LVIF_STATE
)
1672 // temp vars for readability
1673 const UINT stOld
= nmLV
->uOldState
;
1674 const UINT stNew
= nmLV
->uNewState
;
1676 // has the focus changed?
1677 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1679 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1680 event
.m_itemIndex
= nmLV
->iItem
;
1683 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1685 if ( eventType
!= wxEVT_NULL
)
1687 // focus and selection have both changed: send the
1688 // focus event from here and the selection one
1690 event
.SetEventType(eventType
);
1691 (void)GetEventHandler()->ProcessEvent(event
);
1693 else // no focus event to send
1695 // then need to set m_itemIndex as it wasn't done
1697 event
.m_itemIndex
= nmLV
->iItem
;
1700 eventType
= stNew
& LVIS_SELECTED
1701 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1702 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1706 if ( eventType
== wxEVT_NULL
)
1708 // not an interesting event for us
1716 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1717 WORD wVKey
= info
->wVKey
;
1719 // get the current selection
1720 long lItem
= GetNextItem(-1,
1722 wxLIST_STATE_SELECTED
);
1724 // <Enter> or <Space> activate the selected item if any (but
1725 // not with Shift and/or Ctrl as then they have a predefined
1726 // meaning for the list view)
1728 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1729 !(wxIsShiftDown() || wxIsCtrlDown()) )
1731 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1735 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1737 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1738 // value which should be used as is
1739 int code
= wxCharCodeMSWToWX(wVKey
);
1740 event
.m_code
= code
? code
: wVKey
;
1744 event
.m_item
.m_itemId
= lItem
;
1748 // fill the other fields too
1749 event
.m_item
.m_text
= GetItemText(lItem
);
1750 event
.m_item
.m_data
= GetItemData(lItem
);
1756 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1758 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1763 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1764 // if it happened on an item (and not on empty place)
1765 if ( nmLV
->iItem
== -1 )
1771 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1772 event
.m_itemIndex
= nmLV
->iItem
;
1773 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1774 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1778 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1779 // don't do anything else
1780 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1785 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1786 LV_HITTESTINFO lvhti
;
1787 wxZeroMemory(lvhti
);
1789 ::GetCursorPos(&(lvhti
.pt
));
1790 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1791 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1793 if ( lvhti
.flags
& LVHT_ONITEM
)
1795 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1796 event
.m_itemIndex
= lvhti
.iItem
;
1797 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1798 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1803 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1804 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1806 *result
= OnCustomDraw(lParam
);
1809 #endif // _WIN32_IE >= 0x300
1811 case LVN_ODCACHEHINT
:
1813 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
1815 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
1817 // we get some really stupid cache hints like ones for items in
1818 // range 0..0 for an empty control or, after deleting an item,
1819 // for items in invalid range - filter this garbage out
1820 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
1822 event
.m_oldItemIndex
= cacheHint
->iFrom
;
1824 long iMax
= GetItemCount();
1825 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
1835 case LVN_GETDISPINFO
:
1838 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1840 LV_ITEM
& lvi
= info
->item
;
1841 long item
= lvi
.iItem
;
1843 if ( lvi
.mask
& LVIF_TEXT
)
1845 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1846 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1849 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1850 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
1851 if ( lvi
.mask
& LVIF_IMAGE
)
1853 lvi
.iImage
= OnGetItemImage(item
);
1857 // a little dose of healthy paranoia: as we never use
1858 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1859 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1860 _T("we don't support state callbacks yet!") );
1867 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1872 // where did this one come from?
1876 // process the event
1877 // -----------------
1879 event
.SetEventType(eventType
);
1881 if ( !GetEventHandler()->ProcessEvent(event
) )
1887 switch ( nmhdr
->code
)
1889 case LVN_DELETEALLITEMS
:
1890 // always return TRUE to suppress all additional LVN_DELETEITEM
1891 // notifications - this makes deleting all items from a list ctrl
1897 case LVN_ENDLABELEDIT
:
1898 // logic here is inversed compared to all the other messages
1899 *result
= event
.IsAllowed();
1904 *result
= !event
.IsAllowed();
1909 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1911 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1913 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1914 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1915 switch ( nmcd
.dwDrawStage
)
1918 // if we've got any items with non standard attributes,
1919 // notify us before painting each item
1921 // for virtual controls, always suppose that we have attributes as
1922 // there is no way to check for this
1923 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1926 case CDDS_ITEMPREPAINT
:
1928 size_t item
= (size_t)nmcd
.dwItemSpec
;
1929 if ( item
>= (size_t)GetItemCount() )
1931 // we get this message with item == 0 for an empty control,
1932 // we must ignore it as calling OnGetItemAttr() would be
1934 return CDRF_DODEFAULT
;
1937 wxListItemAttr
*attr
=
1938 IsVirtual() ? OnGetItemAttr(item
)
1939 : (wxListItemAttr
*)m_attrs
.Get(item
);
1943 // nothing to do for this item
1944 return CDRF_DODEFAULT
;
1948 wxColour colText
, colBack
;
1949 if ( attr
->HasFont() )
1951 wxFont font
= attr
->GetFont();
1952 hFont
= (HFONT
)font
.GetResourceHandle();
1959 if ( attr
->HasTextColour() )
1961 colText
= attr
->GetTextColour();
1965 colText
= GetTextColour();
1968 if ( attr
->HasBackgroundColour() )
1970 colBack
= attr
->GetBackgroundColour();
1974 colBack
= GetBackgroundColour();
1977 lplvcd
->clrText
= wxColourToRGB(colText
);
1978 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1980 // note that if we wanted to set colours for
1981 // individual columns (subitems), we would have
1982 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1985 ::SelectObject(nmcd
.hdc
, hFont
);
1987 return CDRF_NEWFONT
;
1990 // fall through to return CDRF_DODEFAULT
1993 return CDRF_DODEFAULT
;
1997 #endif // NM_CUSTOMDRAW supported
1999 // Necessary for drawing hrules and vrules, if specified
2000 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2004 wxControl::OnPaint(event
);
2006 // Reset the device origin since it may have been set
2007 dc
.SetDeviceOrigin(0, 0);
2009 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2010 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2012 if (!drawHRules
&& !drawVRules
)
2014 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2017 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2019 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2021 wxSize clientSize
= GetClientSize();
2025 int itemCount
= GetItemCount();
2029 long top
= GetTopItem();
2030 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2032 if (GetItemRect(i
, itemRect
))
2034 cy
= itemRect
.GetTop();
2035 if (i
!= 0) // Don't draw the first one
2037 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2040 if (i
== itemCount
- 1)
2042 cy
= itemRect
.GetBottom();
2043 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2049 if (drawVRules
&& (i
> -1))
2051 wxRect firstItemRect
;
2052 GetItemRect(0, firstItemRect
);
2054 if (GetItemRect(i
, itemRect
))
2057 int x
= itemRect
.GetX();
2058 for (col
= 0; col
< GetColumnCount(); col
++)
2060 int colWidth
= GetColumnWidth(col
);
2062 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
2068 // ----------------------------------------------------------------------------
2069 // virtual list controls
2070 // ----------------------------------------------------------------------------
2072 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2074 // this is a pure virtual function, in fact - which is not really pure
2075 // because the controls which are not virtual don't need to implement it
2076 wxFAIL_MSG( _T("not supposed to be called") );
2078 return wxEmptyString
;
2081 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2084 wxFAIL_MSG( _T("not supposed to be called") );
2089 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2091 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2092 _T("invalid item index in OnGetItemAttr()") );
2094 // no attributes by default
2098 void wxListCtrl::SetItemCount(long count
)
2100 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2102 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
2104 wxLogLastError(_T("ListView_SetItemCount"));
2108 void wxListCtrl::RefreshItem(long item
)
2110 // strangely enough, ListView_Update() results in much more flicker here
2111 // than a dumb Refresh() -- why?
2113 if ( !ListView_Update(GetHwnd(), item
) )
2115 wxLogLastError(_T("ListView_Update"));
2119 GetItemRect(item
, rect
);
2124 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2126 wxRect rect1
, rect2
;
2127 GetItemRect(itemFrom
, rect1
);
2128 GetItemRect(itemTo
, rect2
);
2130 wxRect rect
= rect1
;
2131 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2136 // ----------------------------------------------------------------------------
2138 // ----------------------------------------------------------------------------
2140 // List item structure
2141 wxListItem::wxListItem()
2151 m_format
= wxLIST_FORMAT_CENTRE
;
2157 void wxListItem::Clear()
2166 m_format
= wxLIST_FORMAT_CENTRE
;
2168 m_text
= wxEmptyString
;
2170 if (m_attr
) delete m_attr
;
2174 void wxListItem::ClearAttributes()
2176 if (m_attr
) delete m_attr
;
2180 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2184 info
.m_data
= lvItem
.lParam
;
2187 info
.m_stateMask
= 0;
2188 info
.m_itemId
= lvItem
.iItem
;
2190 long oldMask
= lvItem
.mask
;
2192 bool needText
= FALSE
;
2193 if (hwndListCtrl
!= 0)
2195 if ( lvItem
.mask
& LVIF_TEXT
)
2202 lvItem
.pszText
= new wxChar
[513];
2203 lvItem
.cchTextMax
= 512;
2205 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2206 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2209 if ( lvItem
.mask
& LVIF_STATE
)
2211 info
.m_mask
|= wxLIST_MASK_STATE
;
2213 if ( lvItem
.stateMask
& LVIS_CUT
)
2215 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2216 if ( lvItem
.state
& LVIS_CUT
)
2217 info
.m_state
|= wxLIST_STATE_CUT
;
2219 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2221 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2222 if ( lvItem
.state
& LVIS_DROPHILITED
)
2223 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2225 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2227 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2228 if ( lvItem
.state
& LVIS_FOCUSED
)
2229 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2231 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2233 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2234 if ( lvItem
.state
& LVIS_SELECTED
)
2235 info
.m_state
|= wxLIST_STATE_SELECTED
;
2239 if ( lvItem
.mask
& LVIF_TEXT
)
2241 info
.m_mask
|= wxLIST_MASK_TEXT
;
2242 info
.m_text
= lvItem
.pszText
;
2244 if ( lvItem
.mask
& LVIF_IMAGE
)
2246 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2247 info
.m_image
= lvItem
.iImage
;
2249 if ( lvItem
.mask
& LVIF_PARAM
)
2250 info
.m_mask
|= wxLIST_MASK_DATA
;
2251 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2252 info
.m_mask
|= wxLIST_SET_ITEM
;
2253 info
.m_col
= lvItem
.iSubItem
;
2258 delete[] lvItem
.pszText
;
2260 lvItem
.mask
= oldMask
;
2263 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2265 if (stateMask
& wxLIST_STATE_CUT
)
2267 lvItem
.stateMask
|= LVIS_CUT
;
2268 if (state
& wxLIST_STATE_CUT
)
2269 lvItem
.state
|= LVIS_CUT
;
2271 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2273 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2274 if (state
& wxLIST_STATE_DROPHILITED
)
2275 lvItem
.state
|= LVIS_DROPHILITED
;
2277 if (stateMask
& wxLIST_STATE_FOCUSED
)
2279 lvItem
.stateMask
|= LVIS_FOCUSED
;
2280 if (state
& wxLIST_STATE_FOCUSED
)
2281 lvItem
.state
|= LVIS_FOCUSED
;
2283 if (stateMask
& wxLIST_STATE_SELECTED
)
2285 lvItem
.stateMask
|= LVIS_SELECTED
;
2286 if (state
& wxLIST_STATE_SELECTED
)
2287 lvItem
.state
|= LVIS_SELECTED
;
2291 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2292 const wxListItem
& info
,
2295 lvItem
.iItem
= (int) info
.m_itemId
;
2297 lvItem
.iImage
= info
.m_image
;
2298 lvItem
.lParam
= info
.m_data
;
2299 lvItem
.stateMask
= 0;
2302 lvItem
.iSubItem
= info
.m_col
;
2304 if (info
.m_mask
& wxLIST_MASK_STATE
)
2306 lvItem
.mask
|= LVIF_STATE
;
2308 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2311 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2313 lvItem
.mask
|= LVIF_TEXT
;
2314 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2316 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2320 // pszText is not const, hence the cast
2321 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2322 if ( lvItem
.pszText
)
2323 lvItem
.cchTextMax
= info
.m_text
.Length();
2325 lvItem
.cchTextMax
= 0;
2328 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2329 lvItem
.mask
|= LVIF_IMAGE
;
2330 if (info
.m_mask
& wxLIST_MASK_DATA
)
2331 lvItem
.mask
|= LVIF_PARAM
;
2334 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
2337 wxZeroMemory(lvCol
);
2339 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2341 lvCol
.mask
|= LVCF_TEXT
;
2342 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2345 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2347 lvCol
.mask
|= LVCF_FMT
;
2349 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2350 lvCol
.fmt
= LVCFMT_LEFT
;
2351 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2352 lvCol
.fmt
= LVCFMT_RIGHT
;
2353 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2354 lvCol
.fmt
= LVCFMT_CENTER
;
2357 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2359 lvCol
.mask
|= LVCF_WIDTH
;
2360 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2361 lvCol
.cx
= LVSCW_AUTOSIZE
;
2362 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2363 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2365 lvCol
.cx
= item
.m_width
;
2368 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2369 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2370 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2372 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2374 lvCol
.mask
|= LVCF_IMAGE
;
2375 lvCol
.iImage
= item
.m_image
;
2377 //else: it doesn't support item images anyhow
2382 #endif // wxUSE_LISTCTRL