1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
54 #include "wx/msw/missing.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // convert our state and mask flags to LV_ITEM constants
61 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
63 // convert wxListItem to LV_ITEM
64 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
65 const wxListItem
& info
, LV_ITEM
& lvItem
);
67 // convert LV_ITEM to wxListItem
68 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
70 /* const */ LV_ITEM
& lvItem
);
72 // convert our wxListItem to LV_COLUMN
73 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
76 // ----------------------------------------------------------------------------
77 // private helper classes
78 // ----------------------------------------------------------------------------
80 // We have to handle both fooW and fooA notifications in several cases
81 // because of broken commctl.dll and/or unicows.dll. This class is used to
82 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
83 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
84 // by wxConvertToMSWListItem().
88 ~wxLV_ITEM() { delete m_buf
; }
89 operator LV_ITEM
&() const { return *m_item
; }
92 wxLV_ITEM(LV_ITEMW
&item
) : m_buf(NULL
), m_item(&item
) {}
93 wxLV_ITEM(LV_ITEMA
&item
)
95 m_item
= new LV_ITEM((LV_ITEM
&)item
);
96 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
98 m_buf
= new wxMB2WXbuf(wxConvLocal
.cMB2WX(item
.pszText
));
99 m_item
->pszText
= (wxChar
*)m_buf
->data();
107 #else // !wxUSE_UNICODE
108 wxLV_ITEM(LV_ITEMW
&item
)
110 m_item
= new LV_ITEM((LV_ITEM
&)item
);
112 // the code below doesn't compile without wxUSE_WCHAR_T and as I don't
113 // know if it's useful to have it at all (do we ever get Unicode
114 // notifications in ANSI mode? I don't think so...) I'm not going to
115 // write alternative implementation right now
117 // but if it is indeed used, we should simply directly use
118 // ::WideCharToMultiByte() here
120 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
122 m_buf
= new wxWC2WXbuf(wxConvLocal
.cWC2WX(item
.pszText
));
123 m_item
->pszText
= (wxChar
*)m_buf
->data();
126 #endif // wxUSE_WCHAR_T
129 wxLV_ITEM(LV_ITEMA
&item
) : m_buf(NULL
), m_item(&item
) {}
132 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
136 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
139 ///////////////////////////////////////////////////////
141 // The MSW version had problems with SetTextColour() et
142 // al as the wxListItemAttr's were stored keyed on the
143 // item index. If a item was inserted anywhere but the end
144 // of the list the the text attributes (colour etc) for
145 // the following items were out of sync.
148 // Under MSW the only way to associate data with a List
149 // item independant of its position in the list is to
150 // store a pointer to it in its lParam attribute. However
151 // user programs are already using this (via the
152 // SetItemData() GetItemData() calls).
154 // However what we can do is store a pointer to a
155 // structure which contains the attributes we want *and*
156 // a lParam for the users data, e.g.
158 // class wxListItemInternalData
161 // wxListItemAttr *attr;
162 // long lParam; // user data
165 // To conserve memory, a wxListItemInternalData is
166 // only allocated for a LV_ITEM if text attributes or
167 // user data(lparam) are being set.
170 // class wxListItemInternalData
171 class wxListItemInternalData
174 wxListItemAttr
*attr
;
175 LPARAM lParam
; // user data
177 wxListItemInternalData() : attr(NULL
), lParam(0) {}
178 ~wxListItemInternalData()
184 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
187 // Get the internal data structure
188 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
189 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
190 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
191 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
199 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
200 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
201 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
202 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
203 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
204 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
205 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
206 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
207 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
208 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
209 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
210 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
211 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
213 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
215 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
216 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
217 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
221 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
222 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
223 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
225 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
227 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
228 EVT_PAINT(wxListCtrl::OnPaint
)
231 // ============================================================================
233 // ============================================================================
235 // ----------------------------------------------------------------------------
236 // wxListCtrl construction
237 // ----------------------------------------------------------------------------
239 void wxListCtrl::Init()
241 m_imageListNormal
= NULL
;
242 m_imageListSmall
= NULL
;
243 m_imageListState
= NULL
;
244 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
248 m_ignoreChangeMessages
= FALSE
;
250 m_AnyInternalData
= FALSE
;
251 m_hasAnyAttr
= FALSE
;
254 bool wxListCtrl::Create(wxWindow
*parent
,
259 const wxValidator
& validator
,
260 const wxString
& name
)
263 SetValidator(validator
);
264 #endif // wxUSE_VALIDATORS
273 m_windowStyle
= style
;
286 m_windowId
= (id
== -1) ? NewControlId() : id
;
288 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
289 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
291 m_baseStyle
= wstyle
;
293 if ( !DoCreateControl(x
, y
, width
, height
) )
297 parent
->AddChild(this);
302 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
304 DWORD wstyle
= m_baseStyle
;
307 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
309 long oldStyle
= 0; // Dummy
310 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
311 wstyle
|= standardStyle
;
313 // Create the ListView control.
314 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
319 GetWinHwnd(GetParent()),
326 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
331 // for comctl32.dll v 4.70+ we want to have this attribute because it's
332 // prettier (and also because wxGTK does it like this)
333 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
335 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
336 0, LVS_EX_FULLROWSELECT
);
339 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
340 SetForegroundColour(GetParent()->GetForegroundColour());
347 void wxListCtrl::UpdateStyle()
351 // The new window view style
353 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
354 dwStyleNew
|= m_baseStyle
;
356 // Get the current window style.
357 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
359 // Only set the window style if the view bits have changed.
360 if ( dwStyleOld
!= dwStyleNew
)
362 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
367 void wxListCtrl::FreeAllInternalData()
369 if (m_AnyInternalData
)
371 int n
= GetItemCount();
374 m_ignoreChangeMessages
= TRUE
;
375 for (i
= 0; i
< n
; i
++)
376 wxDeleteInternalData(this, i
);
377 m_ignoreChangeMessages
= FALSE
;
379 m_AnyInternalData
= FALSE
;
383 wxListCtrl::~wxListCtrl()
385 FreeAllInternalData();
389 m_textCtrl
->UnsubclassWin();
390 m_textCtrl
->SetHWND(0);
395 if (m_ownsImageListNormal
) delete m_imageListNormal
;
396 if (m_ownsImageListSmall
) delete m_imageListSmall
;
397 if (m_ownsImageListState
) delete m_imageListState
;
400 // ----------------------------------------------------------------------------
401 // set/get/change style
402 // ----------------------------------------------------------------------------
404 // Add or remove a single window style
405 void wxListCtrl::SetSingleStyle(long style
, bool add
)
407 long flag
= GetWindowStyleFlag();
409 // Get rid of conflicting styles
412 if ( style
& wxLC_MASK_TYPE
)
413 flag
= flag
& ~wxLC_MASK_TYPE
;
414 if ( style
& wxLC_MASK_ALIGN
)
415 flag
= flag
& ~wxLC_MASK_ALIGN
;
416 if ( style
& wxLC_MASK_SORT
)
417 flag
= flag
& ~wxLC_MASK_SORT
;
433 m_windowStyle
= flag
;
438 // Set the whole window style
439 void wxListCtrl::SetWindowStyleFlag(long flag
)
441 m_windowStyle
= flag
;
446 // Can be just a single style, or a bitlist
447 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
450 if ( style
& wxLC_ICON
)
452 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
453 oldStyle
-= LVS_SMALLICON
;
454 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
455 oldStyle
-= LVS_REPORT
;
456 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
457 oldStyle
-= LVS_LIST
;
461 if ( style
& wxLC_SMALL_ICON
)
463 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
464 oldStyle
-= LVS_ICON
;
465 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
466 oldStyle
-= LVS_REPORT
;
467 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
468 oldStyle
-= LVS_LIST
;
469 wstyle
|= LVS_SMALLICON
;
472 if ( style
& wxLC_LIST
)
474 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
475 oldStyle
-= LVS_ICON
;
476 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
477 oldStyle
-= LVS_REPORT
;
478 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
479 oldStyle
-= LVS_SMALLICON
;
483 if ( style
& wxLC_REPORT
)
485 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
486 oldStyle
-= LVS_ICON
;
487 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
488 oldStyle
-= LVS_LIST
;
489 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
490 oldStyle
-= LVS_SMALLICON
;
492 wstyle
|= LVS_REPORT
;
495 if ( style
& wxLC_ALIGN_LEFT
)
497 if ( oldStyle
& LVS_ALIGNTOP
)
498 oldStyle
-= LVS_ALIGNTOP
;
499 wstyle
|= LVS_ALIGNLEFT
;
502 if ( style
& wxLC_ALIGN_TOP
)
504 if ( oldStyle
& LVS_ALIGNLEFT
)
505 oldStyle
-= LVS_ALIGNLEFT
;
506 wstyle
|= LVS_ALIGNTOP
;
509 if ( style
& wxLC_AUTOARRANGE
)
510 wstyle
|= LVS_AUTOARRANGE
;
512 if ( style
& wxLC_NO_SORT_HEADER
)
513 wstyle
|= LVS_NOSORTHEADER
;
515 if ( style
& wxLC_NO_HEADER
)
516 wstyle
|= LVS_NOCOLUMNHEADER
;
518 if ( style
& wxLC_EDIT_LABELS
)
519 wstyle
|= LVS_EDITLABELS
;
521 if ( style
& wxLC_SINGLE_SEL
)
522 wstyle
|= LVS_SINGLESEL
;
524 if ( style
& wxLC_SORT_ASCENDING
)
526 if ( oldStyle
& LVS_SORTDESCENDING
)
527 oldStyle
-= LVS_SORTDESCENDING
;
528 wstyle
|= LVS_SORTASCENDING
;
531 if ( style
& wxLC_SORT_DESCENDING
)
533 if ( oldStyle
& LVS_SORTASCENDING
)
534 oldStyle
-= LVS_SORTASCENDING
;
535 wstyle
|= LVS_SORTDESCENDING
;
538 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
539 if ( style
& wxLC_VIRTUAL
)
541 int ver
= wxTheApp
->GetComCtl32Version();
544 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."),
545 ver
/ 100, ver
% 100);
548 wstyle
|= LVS_OWNERDATA
;
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 // Sets the foreground, i.e. text, colour
560 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
562 if ( !wxWindow::SetForegroundColour(col
) )
565 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
570 // Sets the background colour
571 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
573 if ( !wxWindow::SetBackgroundColour(col
) )
576 // we set the same colour for both the "empty" background and the items
578 COLORREF color
= wxColourToRGB(col
);
579 ListView_SetBkColor(GetHwnd(), color
);
580 ListView_SetTextBkColor(GetHwnd(), color
);
585 // Gets information about this column
586 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
591 lvCol
.mask
= LVCF_WIDTH
;
593 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
595 lvCol
.mask
|= LVCF_TEXT
;
596 lvCol
.pszText
= new wxChar
[513];
597 lvCol
.cchTextMax
= 512;
600 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
602 lvCol
.mask
|= LVCF_FMT
;
605 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
607 lvCol
.mask
|= LVCF_IMAGE
;
610 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
612 // item.m_subItem = lvCol.iSubItem;
613 item
.m_width
= lvCol
.cx
;
615 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
617 item
.m_text
= lvCol
.pszText
;
618 delete[] lvCol
.pszText
;
621 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
623 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
625 item
.m_format
= wxLIST_FORMAT_LEFT
;
628 item
.m_format
= wxLIST_FORMAT_RIGHT
;
631 item
.m_format
= wxLIST_FORMAT_CENTRE
;
634 item
.m_format
= -1; // Unknown?
639 #if _WIN32_IE >= 0x0300
640 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
642 item
.m_image
= lvCol
.iImage
;
649 // Sets information about this column
650 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
653 wxConvertToMSWListCol(col
, item
, lvCol
);
655 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
658 // Gets the column width
659 int wxListCtrl::GetColumnWidth(int col
) const
661 return ListView_GetColumnWidth(GetHwnd(), col
);
664 // Sets the column width
665 bool wxListCtrl::SetColumnWidth(int col
, int width
)
668 if ( m_windowStyle
& wxLC_LIST
)
672 if ( width2
== wxLIST_AUTOSIZE
)
673 width2
= LVSCW_AUTOSIZE
;
674 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
675 width2
= LVSCW_AUTOSIZE_USEHEADER
;
677 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
680 // Gets the number of items that can fit vertically in the
681 // visible area of the list control (list or report view)
682 // or the total number of items in the list control (icon
683 // or small icon view)
684 int wxListCtrl::GetCountPerPage() const
686 return ListView_GetCountPerPage(GetHwnd());
689 // Gets the edit control for editing labels.
690 wxTextCtrl
* wxListCtrl::GetEditControl() const
695 // Gets information about the item
696 bool wxListCtrl::GetItem(wxListItem
& info
) const
699 wxZeroMemory(lvItem
);
701 lvItem
.iItem
= info
.m_itemId
;
702 lvItem
.iSubItem
= info
.m_col
;
704 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
706 lvItem
.mask
|= LVIF_TEXT
;
707 lvItem
.pszText
= new wxChar
[513];
708 lvItem
.cchTextMax
= 512;
712 lvItem
.pszText
= NULL
;
715 if (info
.m_mask
& wxLIST_MASK_DATA
)
716 lvItem
.mask
|= LVIF_PARAM
;
718 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
719 lvItem
.mask
|= LVIF_IMAGE
;
721 if ( info
.m_mask
& wxLIST_MASK_STATE
)
723 lvItem
.mask
|= LVIF_STATE
;
724 // the other bits are hardly interesting anyhow
725 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
728 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
731 wxLogError(_("Couldn't retrieve information about list control item %d."),
736 // give NULL as hwnd as we already have everything we need
737 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
741 delete[] lvItem
.pszText
;
746 // Sets information about the item
747 bool wxListCtrl::SetItem(wxListItem
& info
)
750 wxConvertToMSWListItem(this, info
, item
);
752 // we never update the lParam if it contains our pointer
753 // to the wxListItemInternalData structure
754 item
.mask
&= ~LVIF_PARAM
;
756 // check if setting attributes or lParam
757 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
759 // get internal item data
760 // perhaps a cache here ?
761 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
766 m_AnyInternalData
= TRUE
;
767 data
= new wxListItemInternalData();
768 item
.lParam
= (LPARAM
) data
;
769 item
.mask
|= LVIF_PARAM
;
774 if (info
.m_mask
& wxLIST_MASK_DATA
)
775 data
->lParam
= info
.m_data
;
778 if (info
.HasAttributes())
781 *data
->attr
= *info
.GetAttributes();
783 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
788 // we could be changing only the attribute in which case we don't need to
789 // call ListView_SetItem() at all
793 if ( !ListView_SetItem(GetHwnd(), &item
) )
795 wxLogDebug(_T("ListView_SetItem() failed"));
801 // we need to update the item immediately to show the new image
802 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
804 // check whether it has any custom attributes
805 if ( info
.HasAttributes() )
809 // if the colour has changed, we must redraw the item
815 // we need this to make the change visible right now
816 RefreshItem(item
.iItem
);
822 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
826 info
.m_mask
= wxLIST_MASK_TEXT
;
827 info
.m_itemId
= index
;
831 info
.m_image
= imageId
;
832 info
.m_mask
|= wxLIST_MASK_IMAGE
;
834 return SetItem(info
);
838 // Gets the item state
839 int wxListCtrl::GetItemState(long item
, long stateMask
) const
843 info
.m_mask
= wxLIST_MASK_STATE
;
844 info
.m_stateMask
= stateMask
;
845 info
.m_itemId
= item
;
853 // Sets the item state
854 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
856 // NB: don't use SetItem() here as it doesn't work with the virtual list
859 wxZeroMemory(lvItem
);
861 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
863 // for the virtual list controls we need to refresh the previously focused
864 // item manually when changing focus without changing selection
865 // programmatically because otherwise it keeps its focus rectangle until
866 // next repaint (yet another comctl32 bug)
869 (stateMask
& wxLIST_STATE_FOCUSED
) &&
870 (state
& wxLIST_STATE_FOCUSED
) )
872 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
879 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
880 (WPARAM
)item
, (LPARAM
)&lvItem
) )
882 wxLogLastError(_T("ListView_SetItemState"));
887 if ( focusOld
!= -1 )
889 // no need to refresh the item if it was previously selected, it would
890 // only result in annoying flicker
891 if ( !(GetItemState(focusOld
,
892 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
894 RefreshItem(focusOld
);
901 // Sets the item image
902 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
906 info
.m_mask
= wxLIST_MASK_IMAGE
;
907 info
.m_image
= image
;
908 info
.m_itemId
= item
;
910 return SetItem(info
);
913 // Gets the item text
914 wxString
wxListCtrl::GetItemText(long item
) const
918 info
.m_mask
= wxLIST_MASK_TEXT
;
919 info
.m_itemId
= item
;
922 return wxEmptyString
;
926 // Sets the item text
927 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
931 info
.m_mask
= wxLIST_MASK_TEXT
;
932 info
.m_itemId
= item
;
938 // Gets the item data
939 long wxListCtrl::GetItemData(long item
) const
943 info
.m_mask
= wxLIST_MASK_DATA
;
944 info
.m_itemId
= item
;
951 // Sets the item data
952 bool wxListCtrl::SetItemData(long item
, long data
)
956 info
.m_mask
= wxLIST_MASK_DATA
;
957 info
.m_itemId
= item
;
960 return SetItem(info
);
963 // Gets the item rectangle
964 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
969 if ( code
== wxLIST_RECT_BOUNDS
)
970 codeWin
= LVIR_BOUNDS
;
971 else if ( code
== wxLIST_RECT_ICON
)
973 else if ( code
== wxLIST_RECT_LABEL
)
974 codeWin
= LVIR_LABEL
;
977 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
979 codeWin
= LVIR_BOUNDS
;
982 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
984 rect
.x
= rectWin
.left
;
985 rect
.y
= rectWin
.top
;
986 rect
.width
= rectWin
.right
- rectWin
.left
;
987 rect
.height
= rectWin
.bottom
- rectWin
.top
;
992 // Gets the item position
993 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
997 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
999 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1003 // Sets the item position.
1004 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1006 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1009 // Gets the number of items in the list control
1010 int wxListCtrl::GetItemCount() const
1015 // Retrieves the spacing between icons in pixels.
1016 // If small is TRUE, gets the spacing for the small icon
1017 // view, otherwise the large icon view.
1018 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1020 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1023 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1026 info
.m_itemId
= item
;
1027 info
.SetTextColour( col
);
1031 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1034 info
.m_itemId
= item
;
1036 return info
.GetTextColour();
1039 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1042 info
.m_itemId
= item
;
1043 info
.SetBackgroundColour( col
);
1047 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1050 info
.m_itemId
= item
;
1052 return info
.GetBackgroundColour();
1055 // Gets the number of selected items in the list control
1056 int wxListCtrl::GetSelectedItemCount() const
1058 return ListView_GetSelectedCount(GetHwnd());
1061 // Gets the text colour of the listview
1062 wxColour
wxListCtrl::GetTextColour() const
1064 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1065 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1069 // Sets the text colour of the listview
1070 void wxListCtrl::SetTextColour(const wxColour
& col
)
1072 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1075 // Gets the index of the topmost visible item when in
1076 // list or report view
1077 long wxListCtrl::GetTopItem() const
1079 return (long) ListView_GetTopIndex(GetHwnd());
1082 // Searches for an item, starting from 'item'.
1083 // 'geometry' is one of
1084 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1085 // 'state' is a state bit flag, one or more of
1086 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1087 // item can be -1 to find the first item that matches the
1089 // Returns the item or -1 if unsuccessful.
1090 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1094 if ( geom
== wxLIST_NEXT_ABOVE
)
1095 flags
|= LVNI_ABOVE
;
1096 if ( geom
== wxLIST_NEXT_ALL
)
1098 if ( geom
== wxLIST_NEXT_BELOW
)
1099 flags
|= LVNI_BELOW
;
1100 if ( geom
== wxLIST_NEXT_LEFT
)
1101 flags
|= LVNI_TOLEFT
;
1102 if ( geom
== wxLIST_NEXT_RIGHT
)
1103 flags
|= LVNI_TORIGHT
;
1105 if ( state
& wxLIST_STATE_CUT
)
1107 if ( state
& wxLIST_STATE_DROPHILITED
)
1108 flags
|= LVNI_DROPHILITED
;
1109 if ( state
& wxLIST_STATE_FOCUSED
)
1110 flags
|= LVNI_FOCUSED
;
1111 if ( state
& wxLIST_STATE_SELECTED
)
1112 flags
|= LVNI_SELECTED
;
1114 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1118 wxImageList
*wxListCtrl::GetImageList(int which
) const
1120 if ( which
== wxIMAGE_LIST_NORMAL
)
1122 return m_imageListNormal
;
1124 else if ( which
== wxIMAGE_LIST_SMALL
)
1126 return m_imageListSmall
;
1128 else if ( which
== wxIMAGE_LIST_STATE
)
1130 return m_imageListState
;
1135 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1138 if ( which
== wxIMAGE_LIST_NORMAL
)
1140 flags
= LVSIL_NORMAL
;
1141 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1142 m_imageListNormal
= imageList
;
1143 m_ownsImageListNormal
= FALSE
;
1145 else if ( which
== wxIMAGE_LIST_SMALL
)
1147 flags
= LVSIL_SMALL
;
1148 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1149 m_imageListSmall
= imageList
;
1150 m_ownsImageListSmall
= FALSE
;
1152 else if ( which
== wxIMAGE_LIST_STATE
)
1154 flags
= LVSIL_STATE
;
1155 if (m_ownsImageListState
) delete m_imageListState
;
1156 m_imageListState
= imageList
;
1157 m_ownsImageListState
= FALSE
;
1159 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1162 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1164 SetImageList(imageList
, which
);
1165 if ( which
== wxIMAGE_LIST_NORMAL
)
1166 m_ownsImageListNormal
= TRUE
;
1167 else if ( which
== wxIMAGE_LIST_SMALL
)
1168 m_ownsImageListSmall
= TRUE
;
1169 else if ( which
== wxIMAGE_LIST_STATE
)
1170 m_ownsImageListState
= TRUE
;
1173 // ----------------------------------------------------------------------------
1175 // ----------------------------------------------------------------------------
1177 // Arranges the items
1178 bool wxListCtrl::Arrange(int flag
)
1181 if ( flag
== wxLIST_ALIGN_LEFT
)
1182 code
= LVA_ALIGNLEFT
;
1183 else if ( flag
== wxLIST_ALIGN_TOP
)
1184 code
= LVA_ALIGNTOP
;
1185 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1187 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1188 code
= LVA_SNAPTOGRID
;
1190 return (ListView_Arrange(GetHwnd(), code
) != 0);
1194 bool wxListCtrl::DeleteItem(long item
)
1196 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1198 wxLogLastError(_T("ListView_DeleteItem"));
1203 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1204 wxT("m_count should match ListView_GetItemCount"));
1206 // the virtual list control doesn't refresh itself correctly, help it
1209 // we need to refresh all the lines below the one which was deleted
1211 if ( item
> 0 && GetItemCount() )
1213 GetItemRect(item
- 1, rectItem
);
1218 rectItem
.height
= 0;
1221 wxRect rectWin
= GetRect();
1222 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1223 rectWin
.y
= rectItem
.GetBottom();
1225 RefreshRect(rectWin
);
1231 // Deletes all items
1232 bool wxListCtrl::DeleteAllItems()
1234 FreeAllInternalData();
1235 return ListView_DeleteAllItems(GetHwnd()) != 0;
1238 // Deletes all items
1239 bool wxListCtrl::DeleteAllColumns()
1241 while ( m_colCount
> 0 )
1243 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1245 wxLogLastError(wxT("ListView_DeleteColumn"));
1253 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1259 bool wxListCtrl::DeleteColumn(int col
)
1261 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1263 if ( success
&& (m_colCount
> 0) )
1268 // Clears items, and columns if there are any.
1269 void wxListCtrl::ClearAll()
1272 if ( m_colCount
> 0 )
1276 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1278 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1280 // ListView_EditLabel requires that the list has focus.
1283 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1286 // failed to start editing
1290 // [re]create the text control wrapping the HWND we got
1293 m_textCtrl
->UnsubclassWin();
1294 m_textCtrl
->SetHWND(0);
1298 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1299 m_textCtrl
->SetHWND(hWnd
);
1300 m_textCtrl
->SubclassWin(hWnd
);
1301 m_textCtrl
->SetParent(this);
1303 // we must disallow TABbing away from the control while the edit contol is
1304 // shown because this leaves it in some strange state (just try removing
1305 // this line and then pressing TAB while editing an item in listctrl
1307 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1312 // End label editing, optionally cancelling the edit
1313 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1315 wxFAIL_MSG( _T("not implemented") );
1320 // Ensures this item is visible
1321 bool wxListCtrl::EnsureVisible(long item
)
1323 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1326 // Find an item whose label matches this string, starting from the item after 'start'
1327 // or the beginning if 'start' is -1.
1328 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1330 LV_FINDINFO findInfo
;
1332 findInfo
.flags
= LVFI_STRING
;
1334 findInfo
.flags
|= LVFI_PARTIAL
;
1337 // ListView_FindItem() excludes the first item from search and to look
1338 // through all the items you need to start from -1 which is unnatural and
1339 // inconsistent with the generic version - so we adjust the index
1342 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1345 // Find an item whose data matches this data, starting from the item after 'start'
1346 // or the beginning if 'start' is -1.
1347 // NOTE : Lindsay Mathieson - 14-July-2002
1348 // No longer use ListView_FindItem as the data attribute is now stored
1349 // in a wxListItemInternalData structure refernced by the actual lParam
1350 long wxListCtrl::FindItem(long start
, long data
)
1352 long idx
= start
+ 1;
1353 long count
= GetItemCount();
1357 if (GetItemData(idx
) == data
)
1365 // Find an item nearest this position in the specified direction, starting from
1366 // the item after 'start' or the beginning if 'start' is -1.
1367 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1369 LV_FINDINFO findInfo
;
1371 findInfo
.flags
= LVFI_NEARESTXY
;
1372 findInfo
.pt
.x
= pt
.x
;
1373 findInfo
.pt
.y
= pt
.y
;
1374 findInfo
.vkDirection
= VK_RIGHT
;
1376 if ( direction
== wxLIST_FIND_UP
)
1377 findInfo
.vkDirection
= VK_UP
;
1378 else if ( direction
== wxLIST_FIND_DOWN
)
1379 findInfo
.vkDirection
= VK_DOWN
;
1380 else if ( direction
== wxLIST_FIND_LEFT
)
1381 findInfo
.vkDirection
= VK_LEFT
;
1382 else if ( direction
== wxLIST_FIND_RIGHT
)
1383 findInfo
.vkDirection
= VK_RIGHT
;
1385 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1388 // Determines which item (if any) is at the specified point,
1389 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1390 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1392 LV_HITTESTINFO hitTestInfo
;
1393 hitTestInfo
.pt
.x
= (int) point
.x
;
1394 hitTestInfo
.pt
.y
= (int) point
.y
;
1396 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1399 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1400 flags
|= wxLIST_HITTEST_ABOVE
;
1401 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1402 flags
|= wxLIST_HITTEST_BELOW
;
1403 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1404 flags
|= wxLIST_HITTEST_NOWHERE
;
1405 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1406 flags
|= wxLIST_HITTEST_ONITEMICON
;
1407 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1408 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1409 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1410 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1411 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1412 flags
|= wxLIST_HITTEST_TOLEFT
;
1413 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1414 flags
|= wxLIST_HITTEST_TORIGHT
;
1416 return (long) hitTestInfo
.iItem
;
1419 // Inserts an item, returning the index of the new item if successful,
1421 long wxListCtrl::InsertItem(wxListItem
& info
)
1423 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1426 wxConvertToMSWListItem(this, info
, item
);
1427 item
.mask
&= ~LVIF_PARAM
;
1429 // check wether we need to allocate our internal data
1430 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1431 if (needInternalData
)
1433 m_AnyInternalData
= TRUE
;
1434 item
.mask
|= LVIF_PARAM
;
1436 // internal stucture that manages data
1437 wxListItemInternalData
*data
= new wxListItemInternalData();
1438 item
.lParam
= (LPARAM
) data
;
1440 if (info
.m_mask
& wxLIST_MASK_DATA
)
1441 data
->lParam
= info
.m_data
;
1443 // check whether it has any custom attributes
1444 if ( info
.HasAttributes() )
1446 // take copy of attributes
1447 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1451 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1453 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1454 wxT("m_count should match ListView_GetItemCount"));
1459 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1462 info
.m_text
= label
;
1463 info
.m_mask
= wxLIST_MASK_TEXT
;
1464 info
.m_itemId
= index
;
1465 return InsertItem(info
);
1468 // Inserts an image item
1469 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1472 info
.m_image
= imageIndex
;
1473 info
.m_mask
= wxLIST_MASK_IMAGE
;
1474 info
.m_itemId
= index
;
1475 return InsertItem(info
);
1478 // Inserts an image/string item
1479 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1482 info
.m_image
= imageIndex
;
1483 info
.m_text
= label
;
1484 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1485 info
.m_itemId
= index
;
1486 return InsertItem(info
);
1489 // For list view mode (only), inserts a column.
1490 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1493 wxConvertToMSWListCol(col
, item
, lvCol
);
1495 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1497 // always give some width to the new column: this one is compatible
1498 // with the generic version
1499 lvCol
.mask
|= LVCF_WIDTH
;
1503 // when we insert a column which can contain an image, we must specify this
1504 // flag right now as doing it later in SetColumn() has no effect
1506 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1507 // way to dynamically set/clear the bitmap as the column without a bitmap
1508 // on the left looks ugly (there is a hole)
1510 // unfortunately with my version of comctl32.dll (5.80), the left column
1511 // image is always on the left and it seems that it's a "feature" - I
1512 // didn't find any way to work around it in any case
1513 if ( lvCol
.mask
& LVCF_IMAGE
)
1515 lvCol
.mask
|= LVCF_FMT
;
1516 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1519 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1526 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1533 long wxListCtrl::InsertColumn(long col
,
1534 const wxString
& heading
,
1539 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1540 item
.m_text
= heading
;
1543 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1544 item
.m_width
= width
;
1546 item
.m_format
= format
;
1548 return InsertColumn(col
, item
);
1551 // scroll the control by the given number of pixels (exception: in list view,
1552 // dx is interpreted as number of columns)
1553 bool wxListCtrl::ScrollList(int dx
, int dy
)
1555 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1557 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1567 // fn is a function which takes 3 long arguments: item1, item2, data.
1568 // item1 is the long data associated with a first item (NOT the index).
1569 // item2 is the long data associated with a second item (NOT the index).
1570 // data is the same value as passed to SortItems.
1571 // The return value is a negative number if the first item should precede the second
1572 // item, a positive number of the second item should precede the first,
1573 // or zero if the two items are equivalent.
1575 // data is arbitrary data to be passed to the sort function.
1577 // Internal structures for proxying the user compare function
1578 // so that we can pass it the *real* user data
1580 // translate lParam data and call user func
1581 struct wxInternalDataSort
1583 wxListCtrlCompare user_fn
;
1587 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1589 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1591 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1592 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1594 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1595 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1597 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1601 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1603 struct wxInternalDataSort internalData
;
1604 internalData
.user_fn
= fn
;
1605 internalData
.data
= data
;
1607 // WPARAM cast is needed for mingw/cygwin
1608 if ( !ListView_SortItems(GetHwnd(),
1609 wxInternalDataCompareFunc
,
1610 (WPARAM
) &internalData
) )
1612 wxLogDebug(_T("ListView_SortItems() failed"));
1622 // ----------------------------------------------------------------------------
1623 // message processing
1624 // ----------------------------------------------------------------------------
1626 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1628 if (cmd
== EN_UPDATE
)
1630 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1631 event
.SetEventObject( this );
1632 ProcessCommand(event
);
1635 else if (cmd
== EN_KILLFOCUS
)
1637 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1638 event
.SetEventObject( this );
1639 ProcessCommand(event
);
1646 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1649 // prepare the event
1650 // -----------------
1652 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1653 event
.SetEventObject(this);
1655 wxEventType eventType
= wxEVT_NULL
;
1657 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1659 // if your compiler is as broken as this, you should really change it: this
1660 // code is needed for normal operation! #ifdef below is only useful for
1661 // automatic rebuilds which are done with a very old compiler version
1662 #ifdef HDN_BEGINTRACKA
1664 // check for messages from the header (in report view)
1665 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1667 // is it a message from the header?
1668 if ( nmhdr
->hwndFrom
== hwndHdr
)
1670 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1672 event
.m_itemIndex
= -1;
1674 switch ( nmhdr
->code
)
1676 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1677 // TRACK messages even to ANSI programs: on my system I get
1678 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1680 // work around is to simply catch both versions and hope that it
1681 // works (why should this message exist in ANSI and Unicode is
1682 // beyond me as it doesn't deal with strings at all...)
1684 // note that fr HDN_TRACK another possibility could be to use
1685 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1686 // something other than the item width changes so we'd have to
1687 // filter out the unwanted events then
1688 case HDN_BEGINTRACKA
:
1689 case HDN_BEGINTRACKW
:
1690 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1695 if ( eventType
== wxEVT_NULL
)
1696 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1701 if ( eventType
== wxEVT_NULL
)
1702 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1704 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1705 event
.m_col
= nmHDR
->iItem
;
1710 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1713 // find the column clicked: we have to search for it
1714 // ourselves as the notification message doesn't provide
1717 // where did the click occur?
1719 if ( !::GetCursorPos(&ptClick
) )
1721 wxLogLastError(_T("GetCursorPos"));
1724 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1726 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1729 event
.m_pointDrag
.x
= ptClick
.x
;
1730 event
.m_pointDrag
.y
= ptClick
.y
;
1732 int colCount
= Header_GetItemCount(hwndHdr
);
1735 for ( int col
= 0; col
< colCount
; col
++ )
1737 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1739 if ( ::PtInRect(&rect
, ptClick
) )
1749 case HDN_GETDISPINFOW
:
1751 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1752 // This is a fix for a strange bug under XP.
1753 // Normally, info->iItem is a valid index, but
1754 // sometimes this is a silly (large) number
1755 // and when we return FALSE via wxControl::MSWOnNotify
1756 // to indicate that it hasn't yet been processed,
1757 // there's a GPF in Windows.
1758 // By returning TRUE here, we avoid further processing
1759 // of this strange message.
1760 if ( info
->iItem
>= GetColumnCount() )
1766 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1770 #endif // defined(HDN_BEGINTRACKA)
1771 if ( nmhdr
->hwndFrom
== GetHwnd() )
1773 // almost all messages use NM_LISTVIEW
1774 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1776 const int iItem
= nmLV
->iItem
;
1779 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1780 // ignored for efficiency. It is done here because the internal data is in the
1781 // process of being deleted so we don't want to try and access it below.
1782 if ( m_ignoreChangeMessages
&&
1783 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1789 // If we have a valid item then check if there is a data value
1790 // associated with it and put it in the event.
1791 if ( iItem
>= 0 && iItem
< GetItemCount() )
1793 wxListItemInternalData
*internaldata
=
1794 wxGetInternalData(GetHwnd(), iItem
);
1797 event
.m_item
.m_data
= internaldata
->lParam
;
1801 switch ( nmhdr
->code
)
1803 case LVN_BEGINRDRAG
:
1804 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1808 if ( eventType
== wxEVT_NULL
)
1810 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1813 event
.m_itemIndex
= iItem
;
1814 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1815 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1818 // NB: we have to handle both *A and *W versions here because some
1819 // versions of comctl32.dll send ANSI message to an Unicode app
1820 case LVN_BEGINLABELEDITA
:
1822 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1823 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1824 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1825 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1828 case LVN_BEGINLABELEDITW
:
1830 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1831 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1832 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1833 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1837 case LVN_ENDLABELEDITA
:
1839 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1840 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1841 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1842 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1843 ((LV_ITEM
)item
).iItem
== -1 )
1846 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1849 case LVN_ENDLABELEDITW
:
1851 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1852 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1853 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1854 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1855 ((LV_ITEM
)item
).iItem
== -1 )
1858 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1862 case LVN_COLUMNCLICK
:
1863 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1864 event
.m_itemIndex
= -1;
1865 event
.m_col
= nmLV
->iSubItem
;
1868 case LVN_DELETEALLITEMS
:
1870 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1871 event
.m_itemIndex
= -1;
1874 case LVN_DELETEITEM
:
1876 // this should be prevented by the post-processing code below,
1877 // but "just in case"
1880 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1881 event
.m_itemIndex
= iItem
;
1882 // delete the assoicated internal data
1883 wxDeleteInternalData(this, iItem
);
1886 case LVN_SETDISPINFO
:
1888 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1889 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1890 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1894 case LVN_INSERTITEM
:
1895 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1896 event
.m_itemIndex
= iItem
;
1899 case LVN_ITEMCHANGED
:
1900 // we translate this catch all message into more interesting
1901 // (and more easy to process) wxWindows events
1903 // first of all, we deal with the state change events only and
1904 // only for valid items (item == -1 for the virtual list
1906 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1908 // temp vars for readability
1909 const UINT stOld
= nmLV
->uOldState
;
1910 const UINT stNew
= nmLV
->uNewState
;
1912 event
.m_item
.SetId(iItem
);
1913 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1916 GetItem(event
.m_item
);
1918 // has the focus changed?
1919 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1921 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1922 event
.m_itemIndex
= iItem
;
1925 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1927 if ( eventType
!= wxEVT_NULL
)
1929 // focus and selection have both changed: send the
1930 // focus event from here and the selection one
1932 event
.SetEventType(eventType
);
1933 (void)GetEventHandler()->ProcessEvent(event
);
1935 else // no focus event to send
1937 // then need to set m_itemIndex as it wasn't done
1939 event
.m_itemIndex
= iItem
;
1942 eventType
= stNew
& LVIS_SELECTED
1943 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1944 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1948 if ( eventType
== wxEVT_NULL
)
1950 // not an interesting event for us
1958 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1959 WORD wVKey
= info
->wVKey
;
1961 // get the current selection
1962 long lItem
= GetNextItem(-1,
1964 wxLIST_STATE_SELECTED
);
1966 // <Enter> or <Space> activate the selected item if any (but
1967 // not with Shift and/or Ctrl as then they have a predefined
1968 // meaning for the list view)
1970 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1971 !(wxIsShiftDown() || wxIsCtrlDown()) )
1973 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1977 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1979 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1980 // value which should be used as is
1981 int code
= wxCharCodeMSWToWX(wVKey
);
1982 event
.m_code
= code
? code
: wVKey
;
1986 event
.m_item
.m_itemId
= lItem
;
1990 // fill the other fields too
1991 event
.m_item
.m_text
= GetItemText(lItem
);
1992 event
.m_item
.m_data
= GetItemData(lItem
);
1998 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2000 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2005 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2006 // if it happened on an item (and not on empty place)
2013 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2014 event
.m_itemIndex
= iItem
;
2015 event
.m_item
.m_text
= GetItemText(iItem
);
2016 event
.m_item
.m_data
= GetItemData(iItem
);
2020 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2021 // don't do anything else
2022 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2027 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2028 LV_HITTESTINFO lvhti
;
2029 wxZeroMemory(lvhti
);
2031 ::GetCursorPos(&(lvhti
.pt
));
2032 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2033 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2035 if ( lvhti
.flags
& LVHT_ONITEM
)
2037 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2038 event
.m_itemIndex
= lvhti
.iItem
;
2039 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2040 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2045 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2046 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2048 *result
= OnCustomDraw(lParam
);
2051 #endif // _WIN32_IE >= 0x300
2053 case LVN_ODCACHEHINT
:
2055 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2057 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2059 // we get some really stupid cache hints like ones for items in
2060 // range 0..0 for an empty control or, after deleting an item,
2061 // for items in invalid range - filter this garbage out
2062 if ( cacheHint
->iFrom
< cacheHint
->iTo
)
2064 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2066 long iMax
= GetItemCount();
2067 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2077 case LVN_GETDISPINFO
:
2080 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2082 LV_ITEM
& lvi
= info
->item
;
2083 long item
= lvi
.iItem
;
2085 if ( lvi
.mask
& LVIF_TEXT
)
2087 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2088 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2091 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2092 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2093 if ( lvi
.mask
& LVIF_IMAGE
)
2095 lvi
.iImage
= OnGetItemImage(item
);
2099 // a little dose of healthy paranoia: as we never use
2100 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2101 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2102 _T("we don't support state callbacks yet!") );
2109 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2114 // where did this one come from?
2118 // process the event
2119 // -----------------
2121 event
.SetEventType(eventType
);
2123 bool processed
= GetEventHandler()->ProcessEvent(event
);
2127 switch ( nmhdr
->code
)
2129 case LVN_DELETEALLITEMS
:
2130 // always return TRUE to suppress all additional LVN_DELETEITEM
2131 // notifications - this makes deleting all items from a list ctrl
2136 case LVN_ENDLABELEDITA
:
2137 case LVN_ENDLABELEDITW
:
2138 // logic here is inversed compared to all the other messages
2139 *result
= event
.IsAllowed();
2141 // don't keep a stale wxTextCtrl around
2144 // EDIT control will be deleted by the list control itself so
2145 // prevent us from deleting it as well
2146 m_textCtrl
->UnsubclassWin();
2147 m_textCtrl
->SetHWND(0);
2156 *result
= !event
.IsAllowed();
2161 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2163 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2165 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2166 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2167 switch ( nmcd
.dwDrawStage
)
2170 // if we've got any items with non standard attributes,
2171 // notify us before painting each item
2173 // for virtual controls, always suppose that we have attributes as
2174 // there is no way to check for this
2175 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2178 case CDDS_ITEMPREPAINT
:
2180 size_t item
= (size_t)nmcd
.dwItemSpec
;
2181 if ( item
>= (size_t)GetItemCount() )
2183 // we get this message with item == 0 for an empty control,
2184 // we must ignore it as calling OnGetItemAttr() would be
2186 return CDRF_DODEFAULT
;
2189 wxListItemAttr
*attr
=
2190 IsVirtual() ? OnGetItemAttr(item
)
2191 : wxGetInternalDataAttr(this, item
);
2195 // nothing to do for this item
2196 return CDRF_DODEFAULT
;
2200 wxColour colText
, colBack
;
2201 if ( attr
->HasFont() )
2203 wxFont font
= attr
->GetFont();
2204 hFont
= (HFONT
)font
.GetResourceHandle();
2211 if ( attr
->HasTextColour() )
2213 colText
= attr
->GetTextColour();
2217 colText
= GetTextColour();
2220 if ( attr
->HasBackgroundColour() )
2222 colBack
= attr
->GetBackgroundColour();
2226 colBack
= GetBackgroundColour();
2229 lplvcd
->clrText
= wxColourToRGB(colText
);
2230 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2232 // note that if we wanted to set colours for
2233 // individual columns (subitems), we would have
2234 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2237 ::SelectObject(nmcd
.hdc
, hFont
);
2239 return CDRF_NEWFONT
;
2242 // fall through to return CDRF_DODEFAULT
2245 return CDRF_DODEFAULT
;
2249 #endif // NM_CUSTOMDRAW supported
2251 // Necessary for drawing hrules and vrules, if specified
2252 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2256 wxControl::OnPaint(event
);
2258 // Reset the device origin since it may have been set
2259 dc
.SetDeviceOrigin(0, 0);
2261 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2262 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2264 if (!drawHRules
&& !drawVRules
)
2266 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2269 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2271 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2273 wxSize clientSize
= GetClientSize();
2277 int itemCount
= GetItemCount();
2281 long top
= GetTopItem();
2282 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2284 if (GetItemRect(i
, itemRect
))
2286 cy
= itemRect
.GetTop();
2287 if (i
!= 0) // Don't draw the first one
2289 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2292 if (i
== itemCount
- 1)
2294 cy
= itemRect
.GetBottom();
2295 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2301 if (drawVRules
&& (i
> -1))
2303 wxRect firstItemRect
;
2304 GetItemRect(0, firstItemRect
);
2306 if (GetItemRect(i
, itemRect
))
2309 int x
= itemRect
.GetX();
2310 for (col
= 0; col
< GetColumnCount(); col
++)
2312 int colWidth
= GetColumnWidth(col
);
2314 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2320 // ----------------------------------------------------------------------------
2321 // virtual list controls
2322 // ----------------------------------------------------------------------------
2324 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2326 // this is a pure virtual function, in fact - which is not really pure
2327 // because the controls which are not virtual don't need to implement it
2328 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2330 return wxEmptyString
;
2333 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2336 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2341 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2343 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2344 _T("invalid item index in OnGetItemAttr()") );
2346 // no attributes by default
2350 void wxListCtrl::SetItemCount(long count
)
2352 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2354 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, LVSICF_NOSCROLL
) )
2356 wxLogLastError(_T("ListView_SetItemCount"));
2359 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2360 wxT("m_count should match ListView_GetItemCount"));
2363 void wxListCtrl::RefreshItem(long item
)
2365 // strangely enough, ListView_Update() results in much more flicker here
2366 // than a dumb Refresh() -- why?
2368 if ( !ListView_Update(GetHwnd(), item
) )
2370 wxLogLastError(_T("ListView_Update"));
2374 GetItemRect(item
, rect
);
2379 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2381 wxRect rect1
, rect2
;
2382 GetItemRect(itemFrom
, rect1
);
2383 GetItemRect(itemTo
, rect2
);
2385 wxRect rect
= rect1
;
2386 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2391 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2394 it
.mask
= LVIF_PARAM
;
2397 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2399 return (wxListItemInternalData
*) it
.lParam
;
2404 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2406 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2409 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2411 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2418 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2420 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2424 memset(&item
, 0, sizeof(item
));
2425 item
.iItem
= itemId
;
2426 item
.mask
= LVIF_PARAM
;
2427 item
.lParam
= (LPARAM
) 0;
2428 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2433 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2437 wxListItemInternalData
*internaldata
=
2438 (wxListItemInternalData
*) lvItem
.lParam
;
2441 info
.m_data
= internaldata
->lParam
;
2445 info
.m_stateMask
= 0;
2446 info
.m_itemId
= lvItem
.iItem
;
2448 long oldMask
= lvItem
.mask
;
2450 bool needText
= FALSE
;
2451 if (hwndListCtrl
!= 0)
2453 if ( lvItem
.mask
& LVIF_TEXT
)
2460 lvItem
.pszText
= new wxChar
[513];
2461 lvItem
.cchTextMax
= 512;
2463 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2464 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2467 if ( lvItem
.mask
& LVIF_STATE
)
2469 info
.m_mask
|= wxLIST_MASK_STATE
;
2471 if ( lvItem
.stateMask
& LVIS_CUT
)
2473 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2474 if ( lvItem
.state
& LVIS_CUT
)
2475 info
.m_state
|= wxLIST_STATE_CUT
;
2477 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2479 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2480 if ( lvItem
.state
& LVIS_DROPHILITED
)
2481 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2483 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2485 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2486 if ( lvItem
.state
& LVIS_FOCUSED
)
2487 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2489 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2491 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2492 if ( lvItem
.state
& LVIS_SELECTED
)
2493 info
.m_state
|= wxLIST_STATE_SELECTED
;
2497 if ( lvItem
.mask
& LVIF_TEXT
)
2499 info
.m_mask
|= wxLIST_MASK_TEXT
;
2500 info
.m_text
= lvItem
.pszText
;
2502 if ( lvItem
.mask
& LVIF_IMAGE
)
2504 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2505 info
.m_image
= lvItem
.iImage
;
2507 if ( lvItem
.mask
& LVIF_PARAM
)
2508 info
.m_mask
|= wxLIST_MASK_DATA
;
2509 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2510 info
.m_mask
|= wxLIST_SET_ITEM
;
2511 info
.m_col
= lvItem
.iSubItem
;
2516 delete[] lvItem
.pszText
;
2518 lvItem
.mask
= oldMask
;
2521 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2523 if (stateMask
& wxLIST_STATE_CUT
)
2525 lvItem
.stateMask
|= LVIS_CUT
;
2526 if (state
& wxLIST_STATE_CUT
)
2527 lvItem
.state
|= LVIS_CUT
;
2529 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2531 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2532 if (state
& wxLIST_STATE_DROPHILITED
)
2533 lvItem
.state
|= LVIS_DROPHILITED
;
2535 if (stateMask
& wxLIST_STATE_FOCUSED
)
2537 lvItem
.stateMask
|= LVIS_FOCUSED
;
2538 if (state
& wxLIST_STATE_FOCUSED
)
2539 lvItem
.state
|= LVIS_FOCUSED
;
2541 if (stateMask
& wxLIST_STATE_SELECTED
)
2543 lvItem
.stateMask
|= LVIS_SELECTED
;
2544 if (state
& wxLIST_STATE_SELECTED
)
2545 lvItem
.state
|= LVIS_SELECTED
;
2549 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2550 const wxListItem
& info
,
2553 lvItem
.iItem
= (int) info
.m_itemId
;
2555 lvItem
.iImage
= info
.m_image
;
2556 lvItem
.stateMask
= 0;
2559 lvItem
.iSubItem
= info
.m_col
;
2561 if (info
.m_mask
& wxLIST_MASK_STATE
)
2563 lvItem
.mask
|= LVIF_STATE
;
2565 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2568 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2570 lvItem
.mask
|= LVIF_TEXT
;
2571 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2573 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2577 // pszText is not const, hence the cast
2578 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2579 if ( lvItem
.pszText
)
2580 lvItem
.cchTextMax
= info
.m_text
.Length();
2582 lvItem
.cchTextMax
= 0;
2585 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2586 lvItem
.mask
|= LVIF_IMAGE
;
2589 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2592 wxZeroMemory(lvCol
);
2594 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2596 lvCol
.mask
|= LVCF_TEXT
;
2597 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2600 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2602 lvCol
.mask
|= LVCF_FMT
;
2604 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2605 lvCol
.fmt
= LVCFMT_LEFT
;
2606 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2607 lvCol
.fmt
= LVCFMT_RIGHT
;
2608 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2609 lvCol
.fmt
= LVCFMT_CENTER
;
2612 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2614 lvCol
.mask
|= LVCF_WIDTH
;
2615 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2616 lvCol
.cx
= LVSCW_AUTOSIZE
;
2617 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2618 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2620 lvCol
.cx
= item
.m_width
;
2623 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2624 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2625 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2627 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2629 lvCol
.mask
|= LVCF_IMAGE
;
2630 lvCol
.iImage
= item
.m_image
;
2632 //else: it doesn't support item images anyhow
2637 #endif // wxUSE_LISTCTRL