1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
5 // Modified by: Agron Selimaj
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/settings.h"
35 #include "wx/dcclient.h"
36 #include "wx/textctrl.h"
39 #include "wx/imaglist.h"
40 #include "wx/listctrl.h"
42 #include "wx/msw/private.h"
44 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
52 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
53 // it by its old name NM_FINDTIEM.
55 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
56 #define HAVE_NMLVFINDITEM 1
57 #elif defined(__DMC__) || defined(NM_FINDITEM)
58 #define HAVE_NMLVFINDITEM 1
59 #define NMLVFINDITEM NM_FINDITEM
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // convert our state and mask flags to LV_ITEM constants
67 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
69 // convert wxListItem to LV_ITEM
70 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
71 const wxListItem
& info
, LV_ITEM
& lvItem
);
73 // convert LV_ITEM to wxListItem
74 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
76 /* const */ LV_ITEM
& lvItem
);
78 // convert our wxListItem to LV_COLUMN
79 static void wxConvertToMSWListCol(HWND hwndList
,
81 const wxListItem
& item
,
84 // ----------------------------------------------------------------------------
85 // private helper classes
86 // ----------------------------------------------------------------------------
88 // We have to handle both fooW and fooA notifications in several cases
89 // because of broken comctl32.dll and/or unicows.dll. This class is used to
90 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
91 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
92 // by wxConvertToMSWListItem().
94 #define LV_ITEM_NATIVE LV_ITEMW
95 #define LV_ITEM_OTHER LV_ITEMA
97 #define LV_CONV_TO_WX cMB2WX
98 #define LV_CONV_BUF wxMB2WXbuf
100 #define LV_ITEM_NATIVE LV_ITEMA
101 #define LV_ITEM_OTHER LV_ITEMW
103 #define LV_CONV_TO_WX cWC2WX
104 #define LV_CONV_BUF wxWC2WXbuf
105 #endif // Unicode/ANSI
110 // default ctor, use Init() later
111 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
113 // init without conversion
114 void Init(LV_ITEM_NATIVE
& item
)
116 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
121 // init with conversion
122 void Init(const LV_ITEM_OTHER
& item
)
124 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
125 // point to our own m_item
127 // memcpy() can't work if the struct sizes are different
128 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
129 CodeCantWorkIfDiffSizes
);
131 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
133 // convert text from ANSI to Unicod if necessary
134 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
136 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
137 m_item
.pszText
= (wxChar
*)m_buf
->data();
141 // ctor without conversion
142 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
144 // ctor with conversion
145 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
150 ~wxLV_ITEM() { delete m_buf
; }
152 // conversion to the real LV_ITEM
153 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
158 LV_ITEM_NATIVE
*m_pItem
;
159 LV_ITEM_NATIVE m_item
;
161 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
164 ///////////////////////////////////////////////////////
166 // The MSW version had problems with SetTextColour() et
167 // al as the wxListItemAttr's were stored keyed on the
168 // item index. If a item was inserted anywhere but the end
169 // of the list the the text attributes (colour etc) for
170 // the following items were out of sync.
173 // Under MSW the only way to associate data with a List
174 // item independent of its position in the list is to
175 // store a pointer to it in its lParam attribute. However
176 // user programs are already using this (via the
177 // SetItemData() GetItemData() calls).
179 // However what we can do is store a pointer to a
180 // structure which contains the attributes we want *and*
181 // a lParam for the users data, e.g.
183 // class wxListItemInternalData
186 // wxListItemAttr *attr;
187 // long lParam; // user data
190 // To conserve memory, a wxListItemInternalData is
191 // only allocated for a LV_ITEM if text attributes or
192 // user data(lparam) are being set.
195 // class wxListItemInternalData
196 class wxListItemInternalData
199 wxListItemAttr
*attr
;
200 LPARAM lParam
; // user data
202 wxListItemInternalData() : attr(NULL
), lParam(0) {}
203 ~wxListItemInternalData()
209 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
212 // Get the internal data structure
213 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
214 static wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
);
215 static wxListItemAttr
*wxGetInternalDataAttr(const wxListCtrl
*ctl
, long itemId
);
216 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
219 #if wxUSE_EXTENDED_RTTI
220 WX_DEFINE_FLAGS( wxListCtrlStyle
)
222 wxBEGIN_FLAGS( wxListCtrlStyle
)
223 // new style border flags, we put them first to
224 // use them for streaming out
225 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
226 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
227 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
228 wxFLAGS_MEMBER(wxBORDER_RAISED
)
229 wxFLAGS_MEMBER(wxBORDER_STATIC
)
230 wxFLAGS_MEMBER(wxBORDER_NONE
)
232 // old style border flags
233 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
234 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
235 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
236 wxFLAGS_MEMBER(wxRAISED_BORDER
)
237 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
238 wxFLAGS_MEMBER(wxBORDER
)
240 // standard window styles
241 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
242 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
243 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
244 wxFLAGS_MEMBER(wxWANTS_CHARS
)
245 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
246 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
247 wxFLAGS_MEMBER(wxVSCROLL
)
248 wxFLAGS_MEMBER(wxHSCROLL
)
250 wxFLAGS_MEMBER(wxLC_LIST
)
251 wxFLAGS_MEMBER(wxLC_REPORT
)
252 wxFLAGS_MEMBER(wxLC_ICON
)
253 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
254 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
255 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
256 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
257 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
258 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
259 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
260 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
261 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
262 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
263 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
265 wxEND_FLAGS( wxListCtrlStyle
)
267 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
269 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
270 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
272 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
273 wxEND_PROPERTIES_TABLE()
275 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
276 wxEND_HANDLERS_TABLE()
278 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
281 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
284 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
287 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
288 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
290 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
292 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
293 EVT_PAINT(wxListCtrl::OnPaint
)
296 // ============================================================================
298 // ============================================================================
300 // ----------------------------------------------------------------------------
301 // wxListCtrl construction
302 // ----------------------------------------------------------------------------
304 void wxListCtrl::Init()
306 m_imageListNormal
= NULL
;
307 m_imageListSmall
= NULL
;
308 m_imageListState
= NULL
;
309 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= false;
312 m_ignoreChangeMessages
= false;
314 m_AnyInternalData
= false;
315 m_hasAnyAttr
= false;
318 bool wxListCtrl::Create(wxWindow
*parent
,
323 const wxValidator
& validator
,
324 const wxString
& name
)
326 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
329 if ( !MSWCreateControl(WC_LISTVIEW
, wxEmptyString
, pos
, size
) )
332 // explicitly say that we want to use Unicode because otherwise we get ANSI
333 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
334 wxSetCCUnicodeFormat(GetHwnd());
336 // We must set the default text colour to the system/theme color, otherwise
337 // GetTextColour will always return black
338 SetTextColour(GetDefaultAttributes().colFg
);
340 // for comctl32.dll v 4.70+ we want to have some non default extended
341 // styles because it's prettier (and also because wxGTK does it like this)
342 if ( InReportView() && wxApp::GetComCtl32Version() >= 470 )
344 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
345 0, LVS_EX_LABELTIP
| LVS_EX_FULLROWSELECT
| LVS_EX_SUBITEMIMAGES
);
351 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
353 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
355 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
360 #define MAP_MODE_STYLE(wx, ms) \
361 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
362 #else // !__WXDEBUG__
363 #define MAP_MODE_STYLE(wx, ms) \
364 if ( style & (wx) ) wstyle |= (ms);
365 #endif // __WXDEBUG__
367 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
368 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
369 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
370 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
372 wxASSERT_MSG( nModes
== 1,
373 _T("wxListCtrl style should have exactly one mode bit set") );
375 #undef MAP_MODE_STYLE
377 if ( style
& wxLC_ALIGN_LEFT
)
378 wstyle
|= LVS_ALIGNLEFT
;
380 if ( style
& wxLC_ALIGN_TOP
)
381 wstyle
|= LVS_ALIGNTOP
;
383 if ( style
& wxLC_AUTOARRANGE
)
384 wstyle
|= LVS_AUTOARRANGE
;
386 if ( style
& wxLC_NO_SORT_HEADER
)
387 wstyle
|= LVS_NOSORTHEADER
;
389 if ( style
& wxLC_NO_HEADER
)
390 wstyle
|= LVS_NOCOLUMNHEADER
;
392 if ( style
& wxLC_EDIT_LABELS
)
393 wstyle
|= LVS_EDITLABELS
;
395 if ( style
& wxLC_SINGLE_SEL
)
396 wstyle
|= LVS_SINGLESEL
;
398 if ( style
& wxLC_SORT_ASCENDING
)
400 wstyle
|= LVS_SORTASCENDING
;
402 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
403 _T("can't sort in ascending and descending orders at once") );
405 else if ( style
& wxLC_SORT_DESCENDING
)
406 wstyle
|= LVS_SORTDESCENDING
;
408 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
409 if ( style
& wxLC_VIRTUAL
)
411 int ver
= wxApp::GetComCtl32Version();
414 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."),
415 ver
/ 100, ver
% 100);
418 wstyle
|= LVS_OWNERDATA
;
420 #endif // ancient cygwin
425 void wxListCtrl::UpdateStyle()
429 // The new window view style
430 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
432 // some styles are not returned by MSWGetStyle()
434 dwStyleNew
|= WS_VISIBLE
;
436 // Get the current window style.
437 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
439 // we don't have wxVSCROLL style, but the list control may have it,
440 // don't change it then
441 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
443 // Only set the window style if the view bits have changed.
444 if ( dwStyleOld
!= dwStyleNew
)
446 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
451 void wxListCtrl::FreeAllInternalData()
453 if (m_AnyInternalData
)
455 int n
= GetItemCount();
457 m_ignoreChangeMessages
= true;
458 for (int i
= 0; i
< n
; i
++)
459 wxDeleteInternalData(this, i
);
460 m_ignoreChangeMessages
= false;
462 m_AnyInternalData
= false;
466 wxListCtrl::~wxListCtrl()
468 FreeAllInternalData();
472 m_textCtrl
->UnsubclassWin();
473 m_textCtrl
->SetHWND(0);
478 if (m_ownsImageListNormal
)
479 delete m_imageListNormal
;
480 if (m_ownsImageListSmall
)
481 delete m_imageListSmall
;
482 if (m_ownsImageListState
)
483 delete m_imageListState
;
486 // ----------------------------------------------------------------------------
487 // set/get/change style
488 // ----------------------------------------------------------------------------
490 // Add or remove a single window style
491 void wxListCtrl::SetSingleStyle(long style
, bool add
)
493 long flag
= GetWindowStyleFlag();
495 // Get rid of conflicting styles
498 if ( style
& wxLC_MASK_TYPE
)
499 flag
= flag
& ~wxLC_MASK_TYPE
;
500 if ( style
& wxLC_MASK_ALIGN
)
501 flag
= flag
& ~wxLC_MASK_ALIGN
;
502 if ( style
& wxLC_MASK_SORT
)
503 flag
= flag
& ~wxLC_MASK_SORT
;
511 SetWindowStyleFlag(flag
);
514 // Set the whole window style
515 void wxListCtrl::SetWindowStyleFlag(long flag
)
517 if ( flag
!= m_windowStyle
)
519 m_windowStyle
= flag
;
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 /* static */ wxVisualAttributes
532 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
534 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
536 // common controls have their own default font
537 attrs
.font
= wxGetCCDefaultFont();
542 // Sets the foreground, i.e. text, colour
543 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
545 if ( !wxWindow::SetForegroundColour(col
) )
548 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
553 // Sets the background colour
554 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
556 if ( !wxWindow::SetBackgroundColour(col
) )
559 // we set the same colour for both the "empty" background and the items
561 COLORREF color
= wxColourToRGB(col
);
562 ListView_SetBkColor(GetHwnd(), color
);
563 ListView_SetTextBkColor(GetHwnd(), color
);
568 // Gets information about this column
569 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
574 lvCol
.mask
= LVCF_WIDTH
;
576 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
578 lvCol
.mask
|= LVCF_TEXT
;
579 lvCol
.pszText
= new wxChar
[513];
580 lvCol
.cchTextMax
= 512;
583 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
585 lvCol
.mask
|= LVCF_FMT
;
588 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
590 lvCol
.mask
|= LVCF_IMAGE
;
593 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
595 // item.m_subItem = lvCol.iSubItem;
596 item
.m_width
= lvCol
.cx
;
598 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
600 item
.m_text
= lvCol
.pszText
;
601 delete[] lvCol
.pszText
;
604 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
606 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
608 item
.m_format
= wxLIST_FORMAT_LEFT
;
611 item
.m_format
= wxLIST_FORMAT_RIGHT
;
614 item
.m_format
= wxLIST_FORMAT_CENTRE
;
617 item
.m_format
= -1; // Unknown?
622 // the column images were not supported in older versions but how to check
623 // for this? we can't use _WIN32_IE because we always define it to a very
624 // high value, so see if another symbol which is only defined starting from
625 // comctl32.dll 4.70 is available
626 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
627 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
629 item
.m_image
= lvCol
.iImage
;
631 #endif // LVCOLUMN::iImage exists
636 // Sets information about this column
637 bool wxListCtrl::SetColumn(int col
, const wxListItem
& item
)
640 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
642 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
645 // Gets the column width
646 int wxListCtrl::GetColumnWidth(int col
) const
648 return ListView_GetColumnWidth(GetHwnd(), col
);
651 // Sets the column width
652 bool wxListCtrl::SetColumnWidth(int col
, int width
)
654 if ( m_windowStyle
& wxLC_LIST
)
657 if ( width
== wxLIST_AUTOSIZE
)
658 width
= LVSCW_AUTOSIZE
;
659 else if ( width
== wxLIST_AUTOSIZE_USEHEADER
)
660 width
= LVSCW_AUTOSIZE_USEHEADER
;
662 return ListView_SetColumnWidth(GetHwnd(), col
, width
) != 0;
665 // Gets the number of items that can fit vertically in the
666 // visible area of the list control (list or report view)
667 // or the total number of items in the list control (icon
668 // or small icon view)
669 int wxListCtrl::GetCountPerPage() const
671 return ListView_GetCountPerPage(GetHwnd());
674 // Gets the edit control for editing labels.
675 wxTextCtrl
* wxListCtrl::GetEditControl() const
680 // Gets information about the item
681 bool wxListCtrl::GetItem(wxListItem
& info
) const
684 wxZeroMemory(lvItem
);
686 lvItem
.iItem
= info
.m_itemId
;
687 lvItem
.iSubItem
= info
.m_col
;
689 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
691 lvItem
.mask
|= LVIF_TEXT
;
692 lvItem
.pszText
= new wxChar
[513];
693 lvItem
.cchTextMax
= 512;
697 lvItem
.pszText
= NULL
;
700 if (info
.m_mask
& wxLIST_MASK_DATA
)
701 lvItem
.mask
|= LVIF_PARAM
;
703 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
704 lvItem
.mask
|= LVIF_IMAGE
;
706 if ( info
.m_mask
& wxLIST_MASK_STATE
)
708 lvItem
.mask
|= LVIF_STATE
;
709 wxConvertToMSWFlags(0, info
.m_stateMask
, lvItem
);
712 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
715 wxLogError(_("Couldn't retrieve information about list control item %d."),
720 // give NULL as hwnd as we already have everything we need
721 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
725 delete[] lvItem
.pszText
;
730 // Sets information about the item
731 bool wxListCtrl::SetItem(wxListItem
& info
)
733 const long id
= info
.GetId();
734 wxCHECK_MSG( id
>= 0 && id
< GetItemCount(), false,
735 _T("invalid item index in SetItem") );
738 wxConvertToMSWListItem(this, info
, item
);
740 // we never update the lParam if it contains our pointer
741 // to the wxListItemInternalData structure
742 item
.mask
&= ~LVIF_PARAM
;
744 // check if setting attributes or lParam
745 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
747 // get internal item data
748 // perhaps a cache here ?
749 wxListItemInternalData
*data
= wxGetInternalData(this, id
);
754 m_AnyInternalData
= true;
755 data
= new wxListItemInternalData();
756 item
.lParam
= (LPARAM
) data
;
757 item
.mask
|= LVIF_PARAM
;
762 if (info
.m_mask
& wxLIST_MASK_DATA
)
763 data
->lParam
= info
.m_data
;
766 if ( info
.HasAttributes() )
768 const wxListItemAttr
& attrNew
= *info
.GetAttributes();
770 // don't overwrite the already set attributes if we have them
772 data
->attr
->AssignFrom(attrNew
);
774 data
->attr
= new wxListItemAttr(attrNew
);
779 // we could be changing only the attribute in which case we don't need to
780 // call ListView_SetItem() at all
784 if ( !ListView_SetItem(GetHwnd(), &item
) )
786 wxLogDebug(_T("ListView_SetItem() failed"));
792 // we need to update the item immediately to show the new image
793 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
795 // check whether it has any custom attributes
796 if ( info
.HasAttributes() )
800 // if the colour has changed, we must redraw the item
806 // we need this to make the change visible right now
807 RefreshItem(item
.iItem
);
813 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
817 info
.m_mask
= wxLIST_MASK_TEXT
;
818 info
.m_itemId
= index
;
822 info
.m_image
= imageId
;
823 info
.m_mask
|= wxLIST_MASK_IMAGE
;
825 return SetItem(info
);
829 // Gets the item state
830 int wxListCtrl::GetItemState(long item
, long stateMask
) const
834 info
.m_mask
= wxLIST_MASK_STATE
;
835 info
.m_stateMask
= stateMask
;
836 info
.m_itemId
= item
;
844 // Sets the item state
845 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
847 // NB: don't use SetItem() here as it doesn't work with the virtual list
850 wxZeroMemory(lvItem
);
852 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
854 // for the virtual list controls we need to refresh the previously focused
855 // item manually when changing focus without changing selection
856 // programmatically because otherwise it keeps its focus rectangle until
857 // next repaint (yet another comctl32 bug)
860 (stateMask
& wxLIST_STATE_FOCUSED
) &&
861 (state
& wxLIST_STATE_FOCUSED
) )
863 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
870 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
871 (WPARAM
)item
, (LPARAM
)&lvItem
) )
873 wxLogLastError(_T("ListView_SetItemState"));
878 if ( focusOld
!= -1 )
880 // no need to refresh the item if it was previously selected, it would
881 // only result in annoying flicker
882 if ( !(GetItemState(focusOld
,
883 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
885 RefreshItem(focusOld
);
892 // Sets the item image
893 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
895 return SetItemColumnImage(item
, 0, image
);
898 // Sets the item image
899 bool wxListCtrl::SetItemColumnImage(long item
, long column
, int image
)
903 info
.m_mask
= wxLIST_MASK_IMAGE
;
904 info
.m_image
= image
;
905 info
.m_itemId
= item
;
908 return SetItem(info
);
911 // Gets the item text
912 wxString
wxListCtrl::GetItemText(long item
) const
916 info
.m_mask
= wxLIST_MASK_TEXT
;
917 info
.m_itemId
= item
;
920 return wxEmptyString
;
924 // Sets the item text
925 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
929 info
.m_mask
= wxLIST_MASK_TEXT
;
930 info
.m_itemId
= item
;
936 // Gets the item data
937 wxUIntPtr
wxListCtrl::GetItemData(long item
) const
941 info
.m_mask
= wxLIST_MASK_DATA
;
942 info
.m_itemId
= item
;
949 // Sets the item data
950 bool wxListCtrl::SetItemData(long item
, long data
)
954 info
.m_mask
= wxLIST_MASK_DATA
;
955 info
.m_itemId
= item
;
958 return SetItem(info
);
961 wxRect
wxListCtrl::GetViewRect() const
963 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
964 _T("wxListCtrl::GetViewRect() only works in icon mode") );
967 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
969 wxLogDebug(_T("ListView_GetViewRect() failed."));
975 wxCopyRECTToRect(rc
, rect
);
980 // Gets the item rectangle
981 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
983 return GetSubItemRect( item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
) ;
987 * Retrieve coordinates and size of a specified subitem of a listview control.
988 * This function only works if the listview control is in the report mode.
990 * @param item : Item number
991 * @param subItem : Subitem or column number, use -1 for the whole row including
992 * all columns or subitems
993 * @param rect : A pointer to an allocated wxRect object
994 * @param code : Specify the part of the subitem coordinates you need. Choices are
995 * wxLIST_RECT_BOUNDS, wxLIST_RECT_ICON, wxLIST_RECT_LABEL
997 * @return bool : True if successful.
999 bool wxListCtrl::GetSubItemRect(long item
, long subItem
, wxRect
& rect
, int code
) const
1004 if ( code
== wxLIST_RECT_BOUNDS
)
1005 codeWin
= LVIR_BOUNDS
;
1006 else if ( code
== wxLIST_RECT_ICON
)
1007 codeWin
= LVIR_ICON
;
1008 else if ( code
== wxLIST_RECT_LABEL
)
1009 codeWin
= LVIR_LABEL
;
1012 wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") );
1013 codeWin
= LVIR_BOUNDS
;
1017 if( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
)
1019 success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1021 else if( subItem
>= 0)
1023 success
= ListView_GetSubItemRect( GetHwnd(), (int) item
, (int) subItem
, codeWin
, &rectWin
) != 0;
1027 wxFAIL_MSG( _T("incorrect subItem number in GetSubItemRect()") );
1031 rect
.x
= rectWin
.left
;
1032 rect
.y
= rectWin
.top
;
1033 rect
.width
= rectWin
.right
- rectWin
.left
;
1034 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1042 // Gets the item position
1043 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1047 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1049 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1053 // Sets the item position.
1054 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1056 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1059 // Gets the number of items in the list control
1060 int wxListCtrl::GetItemCount() const
1065 wxSize
wxListCtrl::GetItemSpacing() const
1067 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1069 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1072 #if WXWIN_COMPATIBILITY_2_6
1074 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1076 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1079 #endif // WXWIN_COMPATIBILITY_2_6
1081 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1084 info
.m_itemId
= item
;
1085 info
.SetTextColour( col
);
1089 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1092 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1093 if ( data
&& data
->attr
)
1094 col
= data
->attr
->GetTextColour();
1099 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1102 info
.m_itemId
= item
;
1103 info
.SetBackgroundColour( col
);
1107 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1110 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1111 if ( data
&& data
->attr
)
1112 col
= data
->attr
->GetBackgroundColour();
1117 void wxListCtrl::SetItemFont( long item
, const wxFont
&f
)
1120 info
.m_itemId
= item
;
1125 wxFont
wxListCtrl::GetItemFont( long item
) const
1128 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1129 if ( data
&& data
->attr
)
1130 f
= data
->attr
->GetFont();
1135 // Gets the number of selected items in the list control
1136 int wxListCtrl::GetSelectedItemCount() const
1138 return ListView_GetSelectedCount(GetHwnd());
1141 // Gets the text colour of the listview
1142 wxColour
wxListCtrl::GetTextColour() const
1144 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1145 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1149 // Sets the text colour of the listview
1150 void wxListCtrl::SetTextColour(const wxColour
& col
)
1152 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1155 // Gets the index of the topmost visible item when in
1156 // list or report view
1157 long wxListCtrl::GetTopItem() const
1159 return (long) ListView_GetTopIndex(GetHwnd());
1162 // Searches for an item, starting from 'item'.
1163 // 'geometry' is one of
1164 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1165 // 'state' is a state bit flag, one or more of
1166 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1167 // item can be -1 to find the first item that matches the
1169 // Returns the item or -1 if unsuccessful.
1170 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1174 if ( geom
== wxLIST_NEXT_ABOVE
)
1175 flags
|= LVNI_ABOVE
;
1176 if ( geom
== wxLIST_NEXT_ALL
)
1178 if ( geom
== wxLIST_NEXT_BELOW
)
1179 flags
|= LVNI_BELOW
;
1180 if ( geom
== wxLIST_NEXT_LEFT
)
1181 flags
|= LVNI_TOLEFT
;
1182 if ( geom
== wxLIST_NEXT_RIGHT
)
1183 flags
|= LVNI_TORIGHT
;
1185 if ( state
& wxLIST_STATE_CUT
)
1187 if ( state
& wxLIST_STATE_DROPHILITED
)
1188 flags
|= LVNI_DROPHILITED
;
1189 if ( state
& wxLIST_STATE_FOCUSED
)
1190 flags
|= LVNI_FOCUSED
;
1191 if ( state
& wxLIST_STATE_SELECTED
)
1192 flags
|= LVNI_SELECTED
;
1194 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1198 wxImageList
*wxListCtrl::GetImageList(int which
) const
1200 if ( which
== wxIMAGE_LIST_NORMAL
)
1202 return m_imageListNormal
;
1204 else if ( which
== wxIMAGE_LIST_SMALL
)
1206 return m_imageListSmall
;
1208 else if ( which
== wxIMAGE_LIST_STATE
)
1210 return m_imageListState
;
1215 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1218 if ( which
== wxIMAGE_LIST_NORMAL
)
1220 flags
= LVSIL_NORMAL
;
1221 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1222 m_imageListNormal
= imageList
;
1223 m_ownsImageListNormal
= false;
1225 else if ( which
== wxIMAGE_LIST_SMALL
)
1227 flags
= LVSIL_SMALL
;
1228 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1229 m_imageListSmall
= imageList
;
1230 m_ownsImageListSmall
= false;
1232 else if ( which
== wxIMAGE_LIST_STATE
)
1234 flags
= LVSIL_STATE
;
1235 if (m_ownsImageListState
) delete m_imageListState
;
1236 m_imageListState
= imageList
;
1237 m_ownsImageListState
= false;
1239 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1242 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1244 SetImageList(imageList
, which
);
1245 if ( which
== wxIMAGE_LIST_NORMAL
)
1246 m_ownsImageListNormal
= true;
1247 else if ( which
== wxIMAGE_LIST_SMALL
)
1248 m_ownsImageListSmall
= true;
1249 else if ( which
== wxIMAGE_LIST_STATE
)
1250 m_ownsImageListState
= true;
1253 // ----------------------------------------------------------------------------
1255 // ----------------------------------------------------------------------------
1257 // Arranges the items
1258 bool wxListCtrl::Arrange(int flag
)
1261 if ( flag
== wxLIST_ALIGN_LEFT
)
1262 code
= LVA_ALIGNLEFT
;
1263 else if ( flag
== wxLIST_ALIGN_TOP
)
1264 code
= LVA_ALIGNTOP
;
1265 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1267 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1268 code
= LVA_SNAPTOGRID
;
1270 return (ListView_Arrange(GetHwnd(), code
) != 0);
1274 bool wxListCtrl::DeleteItem(long item
)
1276 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1278 wxLogLastError(_T("ListView_DeleteItem"));
1283 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1284 wxT("m_count should match ListView_GetItemCount"));
1286 // the virtual list control doesn't refresh itself correctly, help it
1289 // we need to refresh all the lines below the one which was deleted
1291 if ( item
> 0 && GetItemCount() )
1293 GetItemRect(item
- 1, rectItem
);
1298 rectItem
.height
= 0;
1301 wxRect rectWin
= GetRect();
1302 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1303 rectWin
.y
= rectItem
.GetBottom();
1305 RefreshRect(rectWin
);
1311 // Deletes all items
1312 bool wxListCtrl::DeleteAllItems()
1314 return ListView_DeleteAllItems(GetHwnd()) != 0;
1317 // Deletes all items
1318 bool wxListCtrl::DeleteAllColumns()
1320 while ( m_colCount
> 0 )
1322 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1324 wxLogLastError(wxT("ListView_DeleteColumn"));
1332 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1338 bool wxListCtrl::DeleteColumn(int col
)
1340 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1342 if ( success
&& (m_colCount
> 0) )
1347 // Clears items, and columns if there are any.
1348 void wxListCtrl::ClearAll()
1351 if ( m_colCount
> 0 )
1355 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1357 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1359 // ListView_EditLabel requires that the list has focus.
1362 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1365 // failed to start editing
1369 // [re]create the text control wrapping the HWND we got
1372 m_textCtrl
->UnsubclassWin();
1373 m_textCtrl
->SetHWND(0);
1377 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1378 m_textCtrl
->SetHWND(hWnd
);
1379 m_textCtrl
->SubclassWin(hWnd
);
1380 m_textCtrl
->SetParent(this);
1382 // we must disallow TABbing away from the control while the edit contol is
1383 // shown because this leaves it in some strange state (just try removing
1384 // this line and then pressing TAB while editing an item in listctrl
1386 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1391 // End label editing, optionally cancelling the edit
1392 bool wxListCtrl::EndEditLabel(bool cancel
)
1394 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1395 HWND hwnd
= ListView_GetEditControl(GetHwnd());
1396 bool b
= (hwnd
!= NULL
);
1400 ::SetWindowText(hwnd
, wxEmptyString
); // dubious but better than nothing
1403 m_textCtrl
->UnsubclassWin();
1404 m_textCtrl
->SetHWND(0);
1408 ::DestroyWindow(hwnd
);
1413 // Ensures this item is visible
1414 bool wxListCtrl::EnsureVisible(long item
)
1416 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1419 // Find an item whose label matches this string, starting from the item after 'start'
1420 // or the beginning if 'start' is -1.
1421 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1423 LV_FINDINFO findInfo
;
1425 findInfo
.flags
= LVFI_STRING
;
1427 findInfo
.flags
|= LVFI_PARTIAL
;
1430 // ListView_FindItem() excludes the first item from search and to look
1431 // through all the items you need to start from -1 which is unnatural and
1432 // inconsistent with the generic version - so we adjust the index
1435 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1438 // Find an item whose data matches this data, starting from the item after 'start'
1439 // or the beginning if 'start' is -1.
1440 // NOTE : Lindsay Mathieson - 14-July-2002
1441 // No longer use ListView_FindItem as the data attribute is now stored
1442 // in a wxListItemInternalData structure refernced by the actual lParam
1443 long wxListCtrl::FindItem(long start
, wxUIntPtr data
)
1445 long idx
= start
+ 1;
1446 long count
= GetItemCount();
1450 if (GetItemData(idx
) == data
)
1458 // Find an item nearest this position in the specified direction, starting from
1459 // the item after 'start' or the beginning if 'start' is -1.
1460 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1462 LV_FINDINFO findInfo
;
1464 findInfo
.flags
= LVFI_NEARESTXY
;
1465 findInfo
.pt
.x
= pt
.x
;
1466 findInfo
.pt
.y
= pt
.y
;
1467 findInfo
.vkDirection
= VK_RIGHT
;
1469 if ( direction
== wxLIST_FIND_UP
)
1470 findInfo
.vkDirection
= VK_UP
;
1471 else if ( direction
== wxLIST_FIND_DOWN
)
1472 findInfo
.vkDirection
= VK_DOWN
;
1473 else if ( direction
== wxLIST_FIND_LEFT
)
1474 findInfo
.vkDirection
= VK_LEFT
;
1475 else if ( direction
== wxLIST_FIND_RIGHT
)
1476 findInfo
.vkDirection
= VK_RIGHT
;
1478 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1481 // Determines which item (if any) is at the specified point,
1482 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1484 wxListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *ptrSubItem
) const
1486 LV_HITTESTINFO hitTestInfo
;
1487 hitTestInfo
.pt
.x
= (int) point
.x
;
1488 hitTestInfo
.pt
.y
= (int) point
.y
;
1491 #ifdef LVM_SUBITEMHITTEST
1492 if ( ptrSubItem
&& wxApp::GetComCtl32Version() >= 470 )
1494 item
= ListView_SubItemHitTest(GetHwnd(), &hitTestInfo
);
1495 *ptrSubItem
= hitTestInfo
.iSubItem
;
1498 #endif // LVM_SUBITEMHITTEST
1500 item
= ListView_HitTest(GetHwnd(), &hitTestInfo
);
1505 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1506 flags
|= wxLIST_HITTEST_ABOVE
;
1507 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1508 flags
|= wxLIST_HITTEST_BELOW
;
1509 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1510 flags
|= wxLIST_HITTEST_TOLEFT
;
1511 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1512 flags
|= wxLIST_HITTEST_TORIGHT
;
1514 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1515 flags
|= wxLIST_HITTEST_NOWHERE
;
1517 // note a bug or at least a very strange feature of comtl32.dll (tested
1518 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1519 // the right of the item label, ListView_HitTest() returns a combination of
1520 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1521 // the bits which don't make sense
1522 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1524 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1526 // do not translate LVHT_ONITEMICON here, as per above
1530 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1531 flags
|= wxLIST_HITTEST_ONITEMICON
;
1532 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1533 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1540 // Inserts an item, returning the index of the new item if successful,
1542 long wxListCtrl::InsertItem(const wxListItem
& info
)
1544 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1547 wxConvertToMSWListItem(this, info
, item
);
1548 item
.mask
&= ~LVIF_PARAM
;
1550 // check wether we need to allocate our internal data
1551 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1552 if (needInternalData
)
1554 m_AnyInternalData
= true;
1555 item
.mask
|= LVIF_PARAM
;
1557 // internal stucture that manages data
1558 wxListItemInternalData
*data
= new wxListItemInternalData();
1559 item
.lParam
= (LPARAM
) data
;
1561 if (info
.m_mask
& wxLIST_MASK_DATA
)
1562 data
->lParam
= info
.m_data
;
1564 // check whether it has any custom attributes
1565 if ( info
.HasAttributes() )
1567 // take copy of attributes
1568 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1570 // and remember that we have some now...
1571 m_hasAnyAttr
= true;
1575 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1578 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1579 wxT("m_count should match ListView_GetItemCount"));
1584 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1587 info
.m_text
= label
;
1588 info
.m_mask
= wxLIST_MASK_TEXT
;
1589 info
.m_itemId
= index
;
1590 return InsertItem(info
);
1593 // Inserts an image item
1594 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1597 info
.m_image
= imageIndex
;
1598 info
.m_mask
= wxLIST_MASK_IMAGE
;
1599 info
.m_itemId
= index
;
1600 return InsertItem(info
);
1603 // Inserts an image/string item
1604 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1607 info
.m_image
= imageIndex
;
1608 info
.m_text
= label
;
1609 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1610 info
.m_itemId
= index
;
1611 return InsertItem(info
);
1614 // For list view mode (only), inserts a column.
1615 long wxListCtrl::InsertColumn(long col
, const wxListItem
& item
)
1618 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
1620 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1622 // always give some width to the new column: this one is compatible
1623 // with the generic version
1624 lvCol
.mask
|= LVCF_WIDTH
;
1628 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1633 else // failed to insert?
1635 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1642 long wxListCtrl::InsertColumn(long col
,
1643 const wxString
& heading
,
1648 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1649 item
.m_text
= heading
;
1652 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1653 item
.m_width
= width
;
1655 item
.m_format
= format
;
1657 return InsertColumn(col
, item
);
1660 // scroll the control by the given number of pixels (exception: in list view,
1661 // dx is interpreted as number of columns)
1662 bool wxListCtrl::ScrollList(int dx
, int dy
)
1664 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1666 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1676 // fn is a function which takes 3 long arguments: item1, item2, data.
1677 // item1 is the long data associated with a first item (NOT the index).
1678 // item2 is the long data associated with a second item (NOT the index).
1679 // data is the same value as passed to SortItems.
1680 // The return value is a negative number if the first item should precede the second
1681 // item, a positive number of the second item should precede the first,
1682 // or zero if the two items are equivalent.
1684 // data is arbitrary data to be passed to the sort function.
1686 // Internal structures for proxying the user compare function
1687 // so that we can pass it the *real* user data
1689 // translate lParam data and call user func
1690 struct wxInternalDataSort
1692 wxListCtrlCompare user_fn
;
1696 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1698 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1700 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1701 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1703 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1704 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1706 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1710 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1712 struct wxInternalDataSort internalData
;
1713 internalData
.user_fn
= fn
;
1714 internalData
.data
= data
;
1716 // WPARAM cast is needed for mingw/cygwin
1717 if ( !ListView_SortItems(GetHwnd(),
1718 wxInternalDataCompareFunc
,
1719 (WPARAM
) &internalData
) )
1721 wxLogDebug(_T("ListView_SortItems() failed"));
1731 // ----------------------------------------------------------------------------
1732 // message processing
1733 // ----------------------------------------------------------------------------
1735 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1737 if ( msg
->message
== WM_KEYDOWN
)
1739 if ( msg
->wParam
== VK_RETURN
)
1741 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
1746 return wxControl::MSWShouldPreProcessMessage(msg
);
1749 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1751 if (cmd
== EN_UPDATE
)
1753 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1754 event
.SetEventObject( this );
1755 ProcessCommand(event
);
1758 else if (cmd
== EN_KILLFOCUS
)
1760 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1761 event
.SetEventObject( this );
1762 ProcessCommand(event
);
1769 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1772 // prepare the event
1773 // -----------------
1775 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1776 event
.SetEventObject(this);
1778 wxEventType eventType
= wxEVT_NULL
;
1780 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1782 // if your compiler is as broken as this, you should really change it: this
1783 // code is needed for normal operation! #ifdef below is only useful for
1784 // automatic rebuilds which are done with a very old compiler version
1785 #ifdef HDN_BEGINTRACKA
1787 // check for messages from the header (in report view)
1788 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1790 // is it a message from the header?
1791 if ( nmhdr
->hwndFrom
== hwndHdr
)
1793 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1795 event
.m_itemIndex
= -1;
1797 switch ( nmhdr
->code
)
1799 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1800 // TRACK messages even to ANSI programs: on my system I get
1801 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1803 // work around is to simply catch both versions and hope that it
1804 // works (why should this message exist in ANSI and Unicode is
1805 // beyond me as it doesn't deal with strings at all...)
1807 // note that fr HDN_TRACK another possibility could be to use
1808 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1809 // something other than the item width changes so we'd have to
1810 // filter out the unwanted events then
1811 case HDN_BEGINTRACKA
:
1812 case HDN_BEGINTRACKW
:
1813 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1818 if ( eventType
== wxEVT_NULL
)
1819 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1824 if ( eventType
== wxEVT_NULL
)
1825 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1827 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1828 event
.m_col
= nmHDR
->iItem
;
1831 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1832 case GN_CONTEXTMENU
:
1833 #endif //__WXWINCE__
1836 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1839 // find the column clicked: we have to search for it
1840 // ourselves as the notification message doesn't provide
1843 // where did the click occur?
1845 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1846 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1847 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1849 #endif //__WXWINCE__
1850 if ( !::GetCursorPos(&ptClick
) )
1852 wxLogLastError(_T("GetCursorPos"));
1855 if ( !::ScreenToClient(GetHwnd(), &ptClick
) )
1857 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1860 event
.m_pointDrag
.x
= ptClick
.x
;
1861 event
.m_pointDrag
.y
= ptClick
.y
;
1863 int colCount
= Header_GetItemCount(hwndHdr
);
1866 for ( int col
= 0; col
< colCount
; col
++ )
1868 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1870 if ( ::PtInRect(&rect
, ptClick
) )
1880 case HDN_GETDISPINFOW
:
1881 // letting Windows XP handle this message results in mysterious
1882 // crashes in comctl32.dll seemingly because of bad message
1885 // I have no idea what is the real cause of the bug (which is,
1886 // just to make things interesting, is impossible to reproduce
1887 // reliably) but ignoring all these messages does fix it and
1888 // doesn't seem to have any negative consequences
1892 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1896 #endif // defined(HDN_BEGINTRACKA)
1897 if ( nmhdr
->hwndFrom
== GetHwnd() )
1899 // almost all messages use NM_LISTVIEW
1900 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1902 const int iItem
= nmLV
->iItem
;
1905 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1906 // ignored for efficiency. It is done here because the internal data is in the
1907 // process of being deleted so we don't want to try and access it below.
1908 if ( m_ignoreChangeMessages
&&
1909 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) ||
1910 (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)) )
1916 // If we have a valid item then check if there is a data value
1917 // associated with it and put it in the event.
1918 if ( iItem
>= 0 && iItem
< GetItemCount() )
1920 wxListItemInternalData
*internaldata
=
1921 wxGetInternalData(GetHwnd(), iItem
);
1924 event
.m_item
.m_data
= internaldata
->lParam
;
1927 bool processed
= true;
1928 switch ( nmhdr
->code
)
1930 case LVN_BEGINRDRAG
:
1931 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1935 if ( eventType
== wxEVT_NULL
)
1937 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1940 event
.m_itemIndex
= iItem
;
1941 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1942 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1945 // NB: we have to handle both *A and *W versions here because some
1946 // versions of comctl32.dll send ANSI messages even to the
1948 case LVN_BEGINLABELEDITA
:
1949 case LVN_BEGINLABELEDITW
:
1952 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1954 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1956 else // LVN_BEGINLABELEDITW
1958 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1961 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1962 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1963 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1967 case LVN_ENDLABELEDITA
:
1968 case LVN_ENDLABELEDITW
:
1971 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1973 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1975 else // LVN_ENDLABELEDITW
1977 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1980 // was editing cancelled?
1981 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1982 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1984 // don't keep a stale wxTextCtrl around
1987 // EDIT control will be deleted by the list control itself so
1988 // prevent us from deleting it as well
1989 m_textCtrl
->UnsubclassWin();
1990 m_textCtrl
->SetHWND(0);
1995 event
.SetEditCanceled(true);
1998 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1999 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
2000 event
.m_itemIndex
= event
.m_item
.m_itemId
;
2004 case LVN_COLUMNCLICK
:
2005 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
2006 event
.m_itemIndex
= -1;
2007 event
.m_col
= nmLV
->iSubItem
;
2010 case LVN_DELETEALLITEMS
:
2011 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
2012 event
.m_itemIndex
= -1;
2015 case LVN_DELETEITEM
:
2018 // this should be prevented by the post-processing code
2019 // below, but "just in case"
2023 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2024 event
.m_itemIndex
= iItem
;
2025 // delete the assoicated internal data
2026 wxDeleteInternalData(this, iItem
);
2029 #if WXWIN_COMPATIBILITY_2_4
2030 case LVN_SETDISPINFO
:
2032 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
2033 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2034 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2039 case LVN_INSERTITEM
:
2040 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2041 event
.m_itemIndex
= iItem
;
2044 case LVN_ITEMCHANGED
:
2045 // we translate this catch all message into more interesting
2046 // (and more easy to process) wxWidgets events
2048 // first of all, we deal with the state change events only and
2049 // only for valid items (item == -1 for the virtual list
2051 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2053 // temp vars for readability
2054 const UINT stOld
= nmLV
->uOldState
;
2055 const UINT stNew
= nmLV
->uNewState
;
2057 event
.m_item
.SetId(iItem
);
2058 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2061 GetItem(event
.m_item
);
2063 // has the focus changed?
2064 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2066 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2067 event
.m_itemIndex
= iItem
;
2070 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2072 if ( eventType
!= wxEVT_NULL
)
2074 // focus and selection have both changed: send the
2075 // focus event from here and the selection one
2077 event
.SetEventType(eventType
);
2078 (void)GetEventHandler()->ProcessEvent(event
);
2080 else // no focus event to send
2082 // then need to set m_itemIndex as it wasn't done
2084 event
.m_itemIndex
= iItem
;
2087 eventType
= stNew
& LVIS_SELECTED
2088 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2089 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2093 if ( eventType
== wxEVT_NULL
)
2095 // not an interesting event for us
2103 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2104 WORD wVKey
= info
->wVKey
;
2106 // get the current selection
2107 long lItem
= GetNextItem(-1,
2109 wxLIST_STATE_SELECTED
);
2111 // <Enter> or <Space> activate the selected item if any (but
2112 // not with Shift and/or Ctrl as then they have a predefined
2113 // meaning for the list view)
2115 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2116 !(wxIsShiftDown() || wxIsCtrlDown()) )
2118 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2122 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2124 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2125 // value which should be used as is
2126 int code
= wxCharCodeMSWToWX(wVKey
);
2127 event
.m_code
= code
? code
: wVKey
;
2131 event
.m_item
.m_itemId
= lItem
;
2135 // fill the other fields too
2136 event
.m_item
.m_text
= GetItemText(lItem
);
2137 event
.m_item
.m_data
= GetItemData(lItem
);
2143 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2145 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2150 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2151 // if it happened on an item (and not on empty place)
2158 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2159 event
.m_itemIndex
= iItem
;
2160 event
.m_item
.m_text
= GetItemText(iItem
);
2161 event
.m_item
.m_data
= GetItemData(iItem
);
2164 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2165 case GN_CONTEXTMENU
:
2166 #endif //__WXWINCE__
2168 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2169 // don't do anything else
2170 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2175 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2176 LV_HITTESTINFO lvhti
;
2177 wxZeroMemory(lvhti
);
2179 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2180 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2181 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2183 #endif //__WXWINCE__
2184 ::GetCursorPos(&(lvhti
.pt
));
2185 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2186 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2188 if ( lvhti
.flags
& LVHT_ONITEM
)
2190 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2191 event
.m_itemIndex
= lvhti
.iItem
;
2192 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2193 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2198 #ifdef NM_CUSTOMDRAW
2200 *result
= OnCustomDraw(lParam
);
2202 return *result
!= CDRF_DODEFAULT
;
2203 #endif // _WIN32_IE >= 0x300
2205 case LVN_ODCACHEHINT
:
2207 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2209 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2211 // we get some really stupid cache hints like ones for
2212 // items in range 0..0 for an empty control or, after
2213 // deleting an item, for items in invalid range -- filter
2215 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2218 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2220 const long iMax
= GetItemCount();
2221 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2226 #ifdef HAVE_NMLVFINDITEM
2227 case LVN_ODFINDITEM
:
2228 // this message is only used with the virtual list control but
2229 // even there we don't want to always use it: in a control with
2230 // sufficiently big number of items (defined as > 1000 here),
2231 // accidentally pressing a key could result in hanging an
2232 // application waiting while it performs linear search
2233 if ( IsVirtual() && GetItemCount() <= 1000 )
2235 NMLVFINDITEM
* pFindInfo
= (NMLVFINDITEM
*)lParam
;
2237 // no match by default
2240 // we only handle string-based searches here
2242 // TODO: what about LVFI_PARTIAL, should we handle this?
2243 if ( !(pFindInfo
->lvfi
.flags
& LVFI_STRING
) )
2248 const wxChar
* const searchstr
= pFindInfo
->lvfi
.psz
;
2249 const size_t len
= wxStrlen(searchstr
);
2251 // this is the first item we should examine, search from it
2252 // wrapping if necessary
2253 const int startPos
= pFindInfo
->iStart
;
2254 const int maxPos
= GetItemCount();
2255 wxCHECK_MSG( startPos
<= maxPos
, false,
2256 _T("bad starting position in LVN_ODFINDITEM") );
2258 int currentPos
= startPos
;
2261 // wrap to the beginning if necessary
2262 if ( currentPos
== maxPos
)
2264 // somewhat surprizingly, LVFI_WRAP isn't set in
2265 // flags but we still should wrap
2269 // does this item begin with searchstr?
2270 if ( wxStrnicmp(searchstr
,
2271 GetItemText(currentPos
), len
) == 0 )
2273 *result
= currentPos
;
2277 while ( ++currentPos
!= startPos
);
2279 if ( *result
== -1 )
2285 SetItemState(*result
,
2286 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
,
2287 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
);
2288 EnsureVisible(*result
);
2296 #endif // HAVE_NMLVFINDITEM
2298 case LVN_GETDISPINFO
:
2301 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2303 LV_ITEM
& lvi
= info
->item
;
2304 long item
= lvi
.iItem
;
2306 if ( lvi
.mask
& LVIF_TEXT
)
2308 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2309 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2312 // see comment at the end of wxListCtrl::GetColumn()
2313 #ifdef NM_CUSTOMDRAW
2314 if ( lvi
.mask
& LVIF_IMAGE
)
2316 lvi
.iImage
= OnGetItemColumnImage(item
, lvi
.iSubItem
);
2318 #endif // NM_CUSTOMDRAW
2320 // a little dose of healthy paranoia: as we never use
2321 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2322 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2323 _T("we don't support state callbacks yet!") );
2334 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2338 // where did this one come from?
2342 // process the event
2343 // -----------------
2345 event
.SetEventType(eventType
);
2347 bool processed
= GetEventHandler()->ProcessEvent(event
);
2351 switch ( nmhdr
->code
)
2353 case LVN_DELETEALLITEMS
:
2354 // always return true to suppress all additional LVN_DELETEITEM
2355 // notifications - this makes deleting all items from a list ctrl
2359 // also, we may free all user data now (couldn't do it before as
2360 // the user should have access to it in OnDeleteAllItems() handler)
2361 FreeAllInternalData();
2363 // the control is empty now, synchronize the cached number of items
2364 // with the real one
2368 case LVN_ENDLABELEDITA
:
2369 case LVN_ENDLABELEDITW
:
2370 // logic here is inverted compared to all the other messages
2371 *result
= event
.IsAllowed();
2373 // don't keep a stale wxTextCtrl around
2376 // EDIT control will be deleted by the list control itself so
2377 // prevent us from deleting it as well
2378 m_textCtrl
->UnsubclassWin();
2379 m_textCtrl
->SetHWND(0);
2388 *result
= !event
.IsAllowed();
2393 // ----------------------------------------------------------------------------
2394 // custom draw stuff
2395 // ----------------------------------------------------------------------------
2397 // see comment at the end of wxListCtrl::GetColumn()
2398 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2400 static RECT
GetCustomDrawnItemRect(const NMCUSTOMDRAW
& nmcd
)
2403 ListView_GetItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, &rc
, LVIR_BOUNDS
);
2406 ListView_GetItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, &rcIcon
, LVIR_ICON
);
2408 // exclude the icon part, neither the selection background nor focus rect
2410 rc
.left
= rcIcon
.right
;
2415 static void HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
)
2417 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2420 HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2421 const DWORD item
= nmcd
.dwItemSpec
;
2424 // the font must be valid, otherwise we wouldn't be painting the item at all
2425 SelectInHDC
selFont(hdc
, hfont
);
2427 // get the rectangle to paint
2429 ListView_GetSubItemRect(hwndList
, item
, pLVCD
->iSubItem
, LVIR_BOUNDS
, &rc
);
2430 if ( !pLVCD
->iSubItem
)
2432 // broken ListView_GetSubItemRect() returns the entire item rect for
2433 // 0th subitem while we really need just the part for this column
2435 ListView_GetSubItemRect(hwndList
, item
, 1, LVIR_BOUNDS
, &rc2
);
2437 rc
.right
= rc2
.left
;
2440 else // not first subitem
2445 // get the image and text to draw
2449 it
.mask
= LVIF_TEXT
| LVIF_IMAGE
;
2451 it
.iSubItem
= pLVCD
->iSubItem
;
2453 it
.cchTextMax
= WXSIZEOF(text
);
2454 ListView_GetItem(hwndList
, &it
);
2456 HIMAGELIST himl
= ListView_GetImageList(hwndList
, LVSIL_SMALL
);
2457 if ( himl
&& ImageList_GetImageCount(himl
) )
2459 if ( it
.iImage
!= -1 )
2461 ImageList_Draw(himl
, it
.iImage
, hdc
, rc
.left
, rc
.top
,
2462 nmcd
.uItemState
& CDIS_SELECTED
? ILD_SELECTED
2466 // notice that even if this item doesn't have any image, the list
2467 // control still leaves space for the image in the first column if the
2468 // image list is not empty (presumably so that items with and without
2470 if ( it
.iImage
!= -1 || it
.iSubItem
== 0 )
2473 ImageList_GetIconSize(himl
, &wImage
, &hImage
);
2475 rc
.left
+= wImage
+ 2;
2479 ::SetBkMode(hdc
, TRANSPARENT
);
2481 // TODO: support for centred/right aligned columns
2482 ::DrawText(hdc
, text
, -1, &rc
,
2485 #endif // __WXWINCE__
2486 DT_NOPREFIX
| DT_SINGLELINE
| DT_VCENTER
);
2489 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd
)
2491 if ( nmcd
.uItemState
& CDIS_FOCUS
)
2493 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2495 // don't use the provided HDC, it's in some strange state by now
2496 ::DrawFocusRect(WindowHDC(nmcd
.hdr
.hwndFrom
), &rc
);
2500 // pLVCD->clrText and clrTextBk should contain the colours to use
2501 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
)
2503 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
; // just a shortcut
2505 const HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2506 const int item
= nmcd
.dwItemSpec
;
2508 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2509 // the item is not at all selected for some reason (comctl32 6), but we
2510 // also can't always trust ListView_GetItem() as it could return the old
2511 // item status if we're called just after the (de)selection, so remember
2512 // the last item to gain selection and also check for it here
2513 for ( int i
= -1;; )
2515 i
= ListView_GetNextItem(hwndList
, i
, LVNI_SELECTED
);
2518 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2524 nmcd
.uItemState
|= CDIS_SELECTED
;
2529 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2530 if ( ::GetFocus() == hwndList
&&
2531 ListView_GetNextItem(hwndList
, (WPARAM
)-1, LVNI_FOCUSED
) == item
)
2533 nmcd
.uItemState
|= CDIS_FOCUS
;
2537 nmcd
.uItemState
&= ~CDIS_FOCUS
;
2540 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2542 int syscolFg
, syscolBg
;
2543 if ( ::GetFocus() == hwndList
)
2545 syscolFg
= COLOR_HIGHLIGHTTEXT
;
2546 syscolBg
= COLOR_HIGHLIGHT
;
2548 else // selected but unfocused
2550 syscolFg
= COLOR_WINDOWTEXT
;
2551 syscolBg
= COLOR_BTNFACE
;
2553 // don't grey out the icon in this case neither
2554 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2557 pLVCD
->clrText
= ::GetSysColor(syscolFg
);
2558 pLVCD
->clrTextBk
= ::GetSysColor(syscolBg
);
2560 //else: not selected, use normal colours from pLVCD
2563 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2565 ::SetTextColor(hdc
, pLVCD
->clrText
);
2566 ::FillRect(hdc
, &rc
, AutoHBRUSH(pLVCD
->clrTextBk
));
2568 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2569 // problems so just draw everything except the focus rect from here instead
2570 const int colCount
= Header_GetItemCount(ListView_GetHeader(hwndList
));
2571 for ( int col
= 0; col
< colCount
; col
++ )
2573 pLVCD
->iSubItem
= col
;
2574 HandleSubItemPrepaint(pLVCD
, hfont
);
2577 HandleItemPostpaint(nmcd
);
2580 static WXLPARAM
HandleItemPrepaint(wxListCtrl
*listctrl
,
2581 LPNMLVCUSTOMDRAW pLVCD
,
2582 wxListItemAttr
*attr
)
2586 // nothing to do for this item
2587 return CDRF_DODEFAULT
;
2591 // set the colours to use for text drawing
2592 pLVCD
->clrText
= attr
->HasTextColour()
2593 ? wxColourToRGB(attr
->GetTextColour())
2594 : wxColourToRGB(listctrl
->GetTextColour());
2595 pLVCD
->clrTextBk
= attr
->HasBackgroundColour()
2596 ? wxColourToRGB(attr
->GetBackgroundColour())
2597 : wxColourToRGB(listctrl
->GetBackgroundColour());
2599 // select the font if non default one is specified
2600 if ( attr
->HasFont() )
2602 wxFont font
= attr
->GetFont();
2603 if ( font
.GetEncoding() != wxFONTENCODING_SYSTEM
)
2605 // the standard control ignores the font encoding/charset, at least
2606 // with recent comctl32.dll versions (5 and 6, it uses to work with
2607 // 4.something) so we have to draw the item entirely ourselves in
2609 HandleItemPaint(pLVCD
, GetHfontOf(font
));
2610 return CDRF_SKIPDEFAULT
;
2613 ::SelectObject(pLVCD
->nmcd
.hdc
, GetHfontOf(font
));
2615 return CDRF_NEWFONT
;
2618 return CDRF_DODEFAULT
;
2621 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2623 LPNMLVCUSTOMDRAW pLVCD
= (LPNMLVCUSTOMDRAW
)lParam
;
2624 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2625 switch ( nmcd
.dwDrawStage
)
2628 // if we've got any items with non standard attributes,
2629 // notify us before painting each item
2631 // for virtual controls, always suppose that we have attributes as
2632 // there is no way to check for this
2633 if ( IsVirtual() || m_hasAnyAttr
)
2634 return CDRF_NOTIFYITEMDRAW
;
2637 case CDDS_ITEMPREPAINT
:
2638 const int item
= nmcd
.dwItemSpec
;
2640 // we get this message with item == 0 for an empty control, we
2641 // must ignore it as calling OnGetItemAttr() would be wrong
2642 if ( item
< 0 || item
>= GetItemCount() )
2645 return HandleItemPrepaint(this, pLVCD
, DoGetItemAttr(item
));
2648 return CDRF_DODEFAULT
;
2651 #endif // NM_CUSTOMDRAW supported
2653 // Necessary for drawing hrules and vrules, if specified
2654 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2656 bool drawHRules
= HasFlag(wxLC_HRULES
);
2657 bool drawVRules
= HasFlag(wxLC_VRULES
);
2659 if (!InReportView() || !drawHRules
&& !drawVRules
)
2667 wxControl::OnPaint(event
);
2669 // Reset the device origin since it may have been set
2670 dc
.SetDeviceOrigin(0, 0);
2672 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2674 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2676 wxSize clientSize
= GetClientSize();
2679 int itemCount
= GetItemCount();
2683 long top
= GetTopItem();
2684 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2686 if (GetItemRect(i
, itemRect
))
2688 int cy
= itemRect
.GetTop();
2689 if (i
!= 0) // Don't draw the first one
2691 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2694 if (i
== itemCount
- 1)
2696 cy
= itemRect
.GetBottom();
2697 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2703 if (drawVRules
&& (i
> -1))
2705 wxRect firstItemRect
;
2706 GetItemRect(0, firstItemRect
);
2708 if (GetItemRect(i
, itemRect
))
2710 // this is a fix for bug 673394: erase the pixels which we would
2711 // otherwise leave on the screen
2712 static const int gap
= 2;
2713 dc
.SetPen(*wxTRANSPARENT_PEN
);
2714 dc
.SetBrush(wxBrush(GetBackgroundColour()));
2715 dc
.DrawRectangle(0, firstItemRect
.GetY() - gap
,
2716 clientSize
.GetWidth(), gap
);
2719 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2720 int x
= itemRect
.GetX();
2721 for (int col
= 0; col
< GetColumnCount(); col
++)
2723 int colWidth
= GetColumnWidth(col
);
2725 dc
.DrawLine(x
-1, firstItemRect
.GetY() - gap
,
2726 x
-1, itemRect
.GetBottom());
2733 wxListCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2739 // we should bypass our own WM_PRINT handling as we don't handle
2740 // PRF_CHILDREN flag, so leave it to the native control itself
2741 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
2744 case WM_CONTEXTMENU
:
2745 // because this message is propagated upwards the child-parent
2746 // chain, we get it for the right clicks on the header window but
2747 // this is confusing in wx as right clicking there already
2748 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2749 // so just ignore them
2750 if ( (HWND
)wParam
== ListView_GetHeader(GetHwnd()) )
2755 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2758 // ----------------------------------------------------------------------------
2759 // virtual list controls
2760 // ----------------------------------------------------------------------------
2762 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2764 // this is a pure virtual function, in fact - which is not really pure
2765 // because the controls which are not virtual don't need to implement it
2766 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2768 return wxEmptyString
;
2771 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2773 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
2775 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2779 int wxListCtrl::OnGetItemColumnImage(long item
, long column
) const
2782 return OnGetItemImage(item
);
2787 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2789 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2790 _T("invalid item index in OnGetItemAttr()") );
2792 // no attributes by default
2796 wxListItemAttr
*wxListCtrl::DoGetItemAttr(long item
) const
2798 return IsVirtual() ? OnGetItemAttr(item
)
2799 : wxGetInternalDataAttr(this, item
);
2802 void wxListCtrl::SetItemCount(long count
)
2804 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2806 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2807 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2809 wxLogLastError(_T("ListView_SetItemCount"));
2812 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2813 wxT("m_count should match ListView_GetItemCount"));
2816 void wxListCtrl::RefreshItem(long item
)
2818 // strangely enough, ListView_Update() results in much more flicker here
2819 // than a dumb Refresh() -- why?
2821 if ( !ListView_Update(GetHwnd(), item
) )
2823 wxLogLastError(_T("ListView_Update"));
2827 GetItemRect(item
, rect
);
2832 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2834 wxRect rect1
, rect2
;
2835 GetItemRect(itemFrom
, rect1
);
2836 GetItemRect(itemTo
, rect2
);
2838 wxRect rect
= rect1
;
2839 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2844 // ----------------------------------------------------------------------------
2845 // internal data stuff
2846 // ----------------------------------------------------------------------------
2848 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2851 it
.mask
= LVIF_PARAM
;
2854 if ( !ListView_GetItem(hwnd
, &it
) )
2857 return (wxListItemInternalData
*) it
.lParam
;
2861 wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
)
2863 return wxGetInternalData(GetHwndOf(ctl
), itemId
);
2867 wxListItemAttr
*wxGetInternalDataAttr(const wxListCtrl
*ctl
, long itemId
)
2869 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2871 return data
? data
->attr
: NULL
;
2874 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2876 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2880 memset(&item
, 0, sizeof(item
));
2881 item
.iItem
= itemId
;
2882 item
.mask
= LVIF_PARAM
;
2883 item
.lParam
= (LPARAM
) 0;
2884 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2889 // ----------------------------------------------------------------------------
2890 // wxWin <-> MSW items conversions
2891 // ----------------------------------------------------------------------------
2893 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2897 wxListItemInternalData
*internaldata
=
2898 (wxListItemInternalData
*) lvItem
.lParam
;
2901 info
.m_data
= internaldata
->lParam
;
2905 info
.m_stateMask
= 0;
2906 info
.m_itemId
= lvItem
.iItem
;
2908 long oldMask
= lvItem
.mask
;
2910 bool needText
= false;
2911 if (hwndListCtrl
!= 0)
2913 if ( lvItem
.mask
& LVIF_TEXT
)
2920 lvItem
.pszText
= new wxChar
[513];
2921 lvItem
.cchTextMax
= 512;
2923 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2924 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2927 if ( lvItem
.mask
& LVIF_STATE
)
2929 info
.m_mask
|= wxLIST_MASK_STATE
;
2931 if ( lvItem
.stateMask
& LVIS_CUT
)
2933 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2934 if ( lvItem
.state
& LVIS_CUT
)
2935 info
.m_state
|= wxLIST_STATE_CUT
;
2937 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2939 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2940 if ( lvItem
.state
& LVIS_DROPHILITED
)
2941 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2943 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2945 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2946 if ( lvItem
.state
& LVIS_FOCUSED
)
2947 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2949 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2951 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2952 if ( lvItem
.state
& LVIS_SELECTED
)
2953 info
.m_state
|= wxLIST_STATE_SELECTED
;
2957 if ( lvItem
.mask
& LVIF_TEXT
)
2959 info
.m_mask
|= wxLIST_MASK_TEXT
;
2960 info
.m_text
= lvItem
.pszText
;
2962 if ( lvItem
.mask
& LVIF_IMAGE
)
2964 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2965 info
.m_image
= lvItem
.iImage
;
2967 if ( lvItem
.mask
& LVIF_PARAM
)
2968 info
.m_mask
|= wxLIST_MASK_DATA
;
2969 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2970 info
.m_mask
|= wxLIST_SET_ITEM
;
2971 info
.m_col
= lvItem
.iSubItem
;
2976 delete[] lvItem
.pszText
;
2978 lvItem
.mask
= oldMask
;
2981 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2983 if (stateMask
& wxLIST_STATE_CUT
)
2985 lvItem
.stateMask
|= LVIS_CUT
;
2986 if (state
& wxLIST_STATE_CUT
)
2987 lvItem
.state
|= LVIS_CUT
;
2989 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2991 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2992 if (state
& wxLIST_STATE_DROPHILITED
)
2993 lvItem
.state
|= LVIS_DROPHILITED
;
2995 if (stateMask
& wxLIST_STATE_FOCUSED
)
2997 lvItem
.stateMask
|= LVIS_FOCUSED
;
2998 if (state
& wxLIST_STATE_FOCUSED
)
2999 lvItem
.state
|= LVIS_FOCUSED
;
3001 if (stateMask
& wxLIST_STATE_SELECTED
)
3003 lvItem
.stateMask
|= LVIS_SELECTED
;
3004 if (state
& wxLIST_STATE_SELECTED
)
3005 lvItem
.state
|= LVIS_SELECTED
;
3009 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
3010 const wxListItem
& info
,
3013 lvItem
.iItem
= (int) info
.m_itemId
;
3015 lvItem
.iImage
= info
.m_image
;
3016 lvItem
.stateMask
= 0;
3019 lvItem
.iSubItem
= info
.m_col
;
3021 if (info
.m_mask
& wxLIST_MASK_STATE
)
3023 lvItem
.mask
|= LVIF_STATE
;
3025 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
3028 if (info
.m_mask
& wxLIST_MASK_TEXT
)
3030 lvItem
.mask
|= LVIF_TEXT
;
3031 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
3033 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
3037 // pszText is not const, hence the cast
3038 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
3039 if ( lvItem
.pszText
)
3040 lvItem
.cchTextMax
= info
.m_text
.length();
3042 lvItem
.cchTextMax
= 0;
3045 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
3046 lvItem
.mask
|= LVIF_IMAGE
;
3049 static void wxConvertToMSWListCol(HWND hwndList
,
3051 const wxListItem
& item
,
3054 wxZeroMemory(lvCol
);
3056 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
3058 lvCol
.mask
|= LVCF_TEXT
;
3059 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
3062 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
3064 lvCol
.mask
|= LVCF_FMT
;
3066 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
3067 lvCol
.fmt
= LVCFMT_LEFT
;
3068 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
3069 lvCol
.fmt
= LVCFMT_RIGHT
;
3070 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
3071 lvCol
.fmt
= LVCFMT_CENTER
;
3074 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
3076 lvCol
.mask
|= LVCF_WIDTH
;
3077 if ( item
.m_width
== wxLIST_AUTOSIZE
)
3078 lvCol
.cx
= LVSCW_AUTOSIZE
;
3079 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3080 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
3082 lvCol
.cx
= item
.m_width
;
3085 // see comment at the end of wxListCtrl::GetColumn()
3086 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3087 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
3089 if ( wxApp::GetComCtl32Version() >= 470 )
3091 lvCol
.mask
|= LVCF_IMAGE
;
3093 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3094 // seem to be generally nicer than on the left and the generic
3095 // version only draws them on the right (we don't have a flag to
3096 // specify the image location anyhow)
3098 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3099 // make any difference in my tests -- but maybe we should?
3100 if ( item
.m_image
!= -1 )
3102 // as we're going to overwrite the format field, get its
3103 // current value first -- unless we want to overwrite it anyhow
3104 if ( !(lvCol
.mask
& LVCF_FMT
) )
3107 wxZeroMemory(lvColOld
);
3108 lvColOld
.mask
= LVCF_FMT
;
3109 if ( ListView_GetColumn(hwndList
, col
, &lvColOld
) )
3111 lvCol
.fmt
= lvColOld
.fmt
;
3114 lvCol
.mask
|= LVCF_FMT
;
3117 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
3120 lvCol
.iImage
= item
.m_image
;
3122 //else: it doesn't support item images anyhow
3124 #endif // _WIN32_IE >= 0x0300
3127 #endif // wxUSE_LISTCTRL