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"
29 #include "wx/listctrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
36 #include "wx/settings.h"
37 #include "wx/dcclient.h"
38 #include "wx/textctrl.h"
41 #include "wx/imaglist.h"
42 #include "wx/vector.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/private/keyboard.h"
47 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
55 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
56 // it by its old name NM_FINDTIEM.
58 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
59 #define HAVE_NMLVFINDITEM 1
60 #elif defined(__DMC__) || defined(NM_FINDITEM)
61 #define HAVE_NMLVFINDITEM 1
62 #define NMLVFINDITEM NM_FINDITEM
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // convert our state and mask flags to LV_ITEM constants
70 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
72 // convert wxListItem to LV_ITEM
73 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
74 const wxListItem
& info
, LV_ITEM
& lvItem
);
76 // convert LV_ITEM to wxListItem
77 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
79 /* const */ LV_ITEM
& lvItem
);
81 // convert our wxListItem to LV_COLUMN
82 static void wxConvertToMSWListCol(HWND hwndList
,
84 const wxListItem
& item
,
90 // replacement for ListView_GetSubItemRect() which provokes warnings like
91 // "the address of 'rc' will always evaluate as 'true'" when used with mingw32
94 // this function does no error checking on item and subitem parameters, notice
95 // that subitem 0 means the whole item so there is no way to retrieve the
96 // rectangle of the first subitem using this function, in particular notice
97 // that the index is *not* 1-based, in spite of what MSDN says
99 wxGetListCtrlSubItemRect(HWND hwnd
, int item
, int subitem
, int flags
, RECT
& rect
)
103 return ::SendMessage(hwnd
, LVM_GETSUBITEMRECT
, item
, (LPARAM
)&rect
) != 0;
107 wxGetListCtrlItemRect(HWND hwnd
, int item
, int flags
, RECT
& rect
)
109 return wxGetListCtrlSubItemRect(hwnd
, item
, 0, flags
, rect
);
112 } // anonymous namespace
114 // ----------------------------------------------------------------------------
115 // private helper classes
116 // ----------------------------------------------------------------------------
118 // We have to handle both fooW and fooA notifications in several cases
119 // because of broken comctl32.dll and/or unicows.dll. This class is used to
120 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
121 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
122 // by wxConvertToMSWListItem().
124 #define LV_ITEM_NATIVE LV_ITEMW
125 #define LV_ITEM_OTHER LV_ITEMA
127 #define LV_CONV_TO_WX cMB2WX
128 #define LV_CONV_BUF wxMB2WXbuf
130 #define LV_ITEM_NATIVE LV_ITEMA
131 #define LV_ITEM_OTHER LV_ITEMW
133 #define LV_CONV_TO_WX cWC2WX
134 #define LV_CONV_BUF wxWC2WXbuf
135 #endif // Unicode/ANSI
140 // default ctor, use Init() later
141 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
143 // init without conversion
144 void Init(LV_ITEM_NATIVE
& item
)
146 wxASSERT_MSG( !m_pItem
, wxT("Init() called twice?") );
151 // init with conversion
152 void Init(const LV_ITEM_OTHER
& item
)
154 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
155 // point to our own m_item
157 // memcpy() can't work if the struct sizes are different
158 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
159 CodeCantWorkIfDiffSizes
);
161 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
163 // convert text from ANSI to Unicod if necessary
164 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
166 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
167 m_item
.pszText
= (wxChar
*)m_buf
->data();
171 // ctor without conversion
172 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
174 // ctor with conversion
175 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
180 ~wxLV_ITEM() { delete m_buf
; }
182 // conversion to the real LV_ITEM
183 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
188 LV_ITEM_NATIVE
*m_pItem
;
189 LV_ITEM_NATIVE m_item
;
191 wxDECLARE_NO_COPY_CLASS(wxLV_ITEM
);
194 ///////////////////////////////////////////////////////
196 // The MSW version had problems with SetTextColour() et
197 // al as the wxListItemAttr's were stored keyed on the
198 // item index. If a item was inserted anywhere but the end
199 // of the list the text attributes (colour etc) for
200 // the following items were out of sync.
203 // Under MSW the only way to associate data with a List
204 // item independent of its position in the list is to
205 // store a pointer to it in its lParam attribute. However
206 // user programs are already using this (via the
207 // SetItemData() GetItemData() calls).
209 // However what we can do is store a pointer to a
210 // structure which contains the attributes we want *and*
211 // a lParam -- and this is what wxMSWListItemData does.
213 // To conserve memory, a wxMSWListItemData is
214 // only allocated for a LV_ITEM if text attributes or
215 // user data(lparam) are being set.
216 class wxMSWListItemData
219 wxMSWListItemData() : attr(NULL
), lParam(0) {}
220 ~wxMSWListItemData() { delete attr
; }
222 wxListItemAttr
*attr
;
223 LPARAM lParam
; // real user data
225 wxDECLARE_NO_COPY_CLASS(wxMSWListItemData
);
228 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
229 EVT_PAINT(wxListCtrl::OnPaint
)
232 // ============================================================================
234 // ============================================================================
236 // ----------------------------------------------------------------------------
237 // wxListCtrl construction
238 // ----------------------------------------------------------------------------
240 void wxListCtrl::Init()
244 m_imageListState
= NULL
;
245 m_ownsImageListNormal
=
246 m_ownsImageListSmall
=
247 m_ownsImageListState
= false;
253 m_hasAnyAttr
= false;
256 bool wxListCtrl::Create(wxWindow
*parent
,
261 const wxValidator
& validator
,
262 const wxString
& name
)
264 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
267 if ( !MSWCreateControl(WC_LISTVIEW
, wxEmptyString
, pos
, size
) )
270 // explicitly say that we want to use Unicode because otherwise we get ANSI
271 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
272 wxSetCCUnicodeFormat(GetHwnd());
274 // We must set the default text colour to the system/theme color, otherwise
275 // GetTextColour will always return black
276 SetTextColour(GetDefaultAttributes().colFg
);
278 if ( InReportView() )
279 MSWSetExListStyles();
284 void wxListCtrl::MSWSetExListStyles()
286 // for comctl32.dll v 4.70+ we want to have some non default extended
287 // styles because it's prettier (and also because wxGTK does it like this)
288 if ( wxApp::GetComCtl32Version() >= 470 )
292 GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 0,
293 // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's
294 // not defined in the SDK headers
295 #ifdef LVS_EX_LABELTIP
298 LVS_EX_FULLROWSELECT
|
299 LVS_EX_SUBITEMIMAGES
|
300 // normally this should be governed by a style as it's probably not
301 // always appropriate, but we don't have any free styles left and
302 // it seems better to enable it by default than disable
303 LVS_EX_HEADERDRAGDROP
308 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
310 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
312 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
317 #define MAP_MODE_STYLE(wx, ms) \
318 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
319 #else // !wxDEBUG_LEVEL
320 #define MAP_MODE_STYLE(wx, ms) \
321 if ( style & (wx) ) wstyle |= (ms);
322 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
324 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
325 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
326 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
327 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
329 wxASSERT_MSG( nModes
== 1,
330 wxT("wxListCtrl style should have exactly one mode bit set") );
332 #undef MAP_MODE_STYLE
334 if ( style
& wxLC_ALIGN_LEFT
)
335 wstyle
|= LVS_ALIGNLEFT
;
337 if ( style
& wxLC_ALIGN_TOP
)
338 wstyle
|= LVS_ALIGNTOP
;
340 if ( style
& wxLC_AUTOARRANGE
)
341 wstyle
|= LVS_AUTOARRANGE
;
343 if ( style
& wxLC_NO_SORT_HEADER
)
344 wstyle
|= LVS_NOSORTHEADER
;
346 if ( style
& wxLC_NO_HEADER
)
347 wstyle
|= LVS_NOCOLUMNHEADER
;
349 if ( style
& wxLC_EDIT_LABELS
)
350 wstyle
|= LVS_EDITLABELS
;
352 if ( style
& wxLC_SINGLE_SEL
)
353 wstyle
|= LVS_SINGLESEL
;
355 if ( style
& wxLC_SORT_ASCENDING
)
357 wstyle
|= LVS_SORTASCENDING
;
359 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
360 wxT("can't sort in ascending and descending orders at once") );
362 else if ( style
& wxLC_SORT_DESCENDING
)
363 wstyle
|= LVS_SORTDESCENDING
;
365 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
366 if ( style
& wxLC_VIRTUAL
)
368 int ver
= wxApp::GetComCtl32Version();
371 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."),
372 ver
/ 100, ver
% 100);
375 wstyle
|= LVS_OWNERDATA
;
377 #endif // ancient cygwin
382 void wxListCtrl::UpdateStyle()
386 // The new window view style
387 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
389 // some styles are not returned by MSWGetStyle()
391 dwStyleNew
|= WS_VISIBLE
;
393 // Get the current window style.
394 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
396 // we don't have wxVSCROLL style, but the list control may have it,
397 // don't change it then
398 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
400 // Only set the window style if the view bits have changed.
401 if ( dwStyleOld
!= dwStyleNew
)
403 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
405 // if we switched to the report view, set the extended styles for
407 if ( !(dwStyleOld
& LVS_REPORT
) && (dwStyleNew
& LVS_REPORT
) )
408 MSWSetExListStyles();
413 void wxListCtrl::FreeAllInternalData()
415 const unsigned count
= m_internalData
.size();
416 for ( unsigned n
= 0; n
< count
; n
++ )
417 delete m_internalData
[n
];
419 m_internalData
.clear();
422 void wxListCtrl::DeleteEditControl()
426 m_textCtrl
->UnsubclassWin();
427 m_textCtrl
->SetHWND(0);
428 wxDELETE(m_textCtrl
);
432 wxListCtrl::~wxListCtrl()
434 FreeAllInternalData();
438 if (m_ownsImageListNormal
)
439 delete m_imageListNormal
;
440 if (m_ownsImageListSmall
)
441 delete m_imageListSmall
;
442 if (m_ownsImageListState
)
443 delete m_imageListState
;
446 // ----------------------------------------------------------------------------
447 // set/get/change style
448 // ----------------------------------------------------------------------------
450 // Add or remove a single window style
451 void wxListCtrl::SetSingleStyle(long style
, bool add
)
453 long flag
= GetWindowStyleFlag();
455 // Get rid of conflicting styles
458 if ( style
& wxLC_MASK_TYPE
)
459 flag
= flag
& ~wxLC_MASK_TYPE
;
460 if ( style
& wxLC_MASK_ALIGN
)
461 flag
= flag
& ~wxLC_MASK_ALIGN
;
462 if ( style
& wxLC_MASK_SORT
)
463 flag
= flag
& ~wxLC_MASK_SORT
;
471 SetWindowStyleFlag(flag
);
474 // Set the whole window style
475 void wxListCtrl::SetWindowStyleFlag(long flag
)
477 if ( flag
!= m_windowStyle
)
479 wxControl::SetWindowStyleFlag(flag
);
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 /* static */ wxVisualAttributes
492 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
494 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
496 // common controls have their own default font
497 attrs
.font
= wxGetCCDefaultFont();
502 // Sets the foreground, i.e. text, colour
503 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
505 if ( !wxWindow::SetForegroundColour(col
) )
508 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
513 // Sets the background colour
514 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
516 if ( !wxWindow::SetBackgroundColour(col
) )
519 // we set the same colour for both the "empty" background and the items
521 COLORREF color
= wxColourToRGB(col
);
522 ListView_SetBkColor(GetHwnd(), color
);
523 ListView_SetTextBkColor(GetHwnd(), color
);
528 // Gets information about this column
529 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
534 lvCol
.mask
= LVCF_WIDTH
;
536 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
538 lvCol
.mask
|= LVCF_TEXT
;
539 lvCol
.pszText
= new wxChar
[513];
540 lvCol
.cchTextMax
= 512;
543 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
545 lvCol
.mask
|= LVCF_FMT
;
548 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
550 lvCol
.mask
|= LVCF_IMAGE
;
553 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
555 // item.m_subItem = lvCol.iSubItem;
556 item
.m_width
= lvCol
.cx
;
558 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
560 item
.m_text
= lvCol
.pszText
;
561 delete[] lvCol
.pszText
;
564 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
566 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
568 item
.m_format
= wxLIST_FORMAT_LEFT
;
571 item
.m_format
= wxLIST_FORMAT_RIGHT
;
574 item
.m_format
= wxLIST_FORMAT_CENTRE
;
577 item
.m_format
= -1; // Unknown?
582 // the column images were not supported in older versions but how to check
583 // for this? we can't use _WIN32_IE because we always define it to a very
584 // high value, so see if another symbol which is only defined starting from
585 // comctl32.dll 4.70 is available
586 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
587 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
589 item
.m_image
= lvCol
.iImage
;
591 #endif // LVCOLUMN::iImage exists
596 // Sets information about this column
597 bool wxListCtrl::SetColumn(int col
, const wxListItem
& item
)
600 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
602 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
605 // Gets the column width
606 int wxListCtrl::GetColumnWidth(int col
) const
608 return ListView_GetColumnWidth(GetHwnd(), col
);
611 // Sets the column width
612 bool wxListCtrl::SetColumnWidth(int col
, int width
)
614 if ( m_windowStyle
& wxLC_LIST
)
617 if ( width
== wxLIST_AUTOSIZE
)
618 width
= LVSCW_AUTOSIZE
;
619 else if ( width
== wxLIST_AUTOSIZE_USEHEADER
)
620 width
= LVSCW_AUTOSIZE_USEHEADER
;
622 return ListView_SetColumnWidth(GetHwnd(), col
, width
) != 0;
625 // ----------------------------------------------------------------------------
627 // ----------------------------------------------------------------------------
629 int wxListCtrl::GetColumnIndexFromOrder(int order
) const
631 const int numCols
= GetColumnCount();
632 wxCHECK_MSG( order
>= 0 && order
< numCols
, -1,
633 wxT("Column position out of bounds") );
635 wxArrayInt
indexArray(numCols
);
636 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) )
639 return indexArray
[order
];
642 int wxListCtrl::GetColumnOrder(int col
) const
644 const int numCols
= GetColumnCount();
645 wxASSERT_MSG( col
>= 0 && col
< numCols
, wxT("Column index out of bounds") );
647 wxArrayInt
indexArray(numCols
);
648 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) )
651 for ( int pos
= 0; pos
< numCols
; pos
++ )
653 if ( indexArray
[pos
] == col
)
657 wxFAIL_MSG( wxT("no column with with given order?") );
662 // Gets the column order for all columns
663 wxArrayInt
wxListCtrl::GetColumnsOrder() const
665 const int numCols
= GetColumnCount();
667 wxArrayInt
orders(numCols
);
668 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) )
674 // Sets the column order for all columns
675 bool wxListCtrl::SetColumnsOrder(const wxArrayInt
& orders
)
677 const int numCols
= GetColumnCount();
679 wxCHECK_MSG( orders
.size() == (size_t)numCols
, false,
680 wxT("wrong number of elements in column orders array") );
682 return ListView_SetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) != 0;
686 // Gets the number of items that can fit vertically in the
687 // visible area of the list control (list or report view)
688 // or the total number of items in the list control (icon
689 // or small icon view)
690 int wxListCtrl::GetCountPerPage() const
692 return ListView_GetCountPerPage(GetHwnd());
695 // Gets the edit control for editing labels.
696 wxTextCtrl
* wxListCtrl::GetEditControl() const
698 // first check corresponds to the case when the label editing was started
699 // by user and hence m_textCtrl wasn't created by EditLabel() at all, while
700 // the second case corresponds to us being called from inside EditLabel()
701 // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this
702 // case EditLabel() did create the control but it didn't have an HWND to
703 // initialize it with yet
704 if ( !m_textCtrl
|| !m_textCtrl
->GetHWND() )
706 HWND hwndEdit
= ListView_GetEditControl(GetHwnd());
709 wxListCtrl
* const self
= const_cast<wxListCtrl
*>(this);
712 self
->m_textCtrl
= new wxTextCtrl
;
713 self
->InitEditControl((WXHWND
)hwndEdit
);
720 // Gets information about the item
721 bool wxListCtrl::GetItem(wxListItem
& info
) const
724 wxZeroMemory(lvItem
);
726 lvItem
.iItem
= info
.m_itemId
;
727 lvItem
.iSubItem
= info
.m_col
;
729 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
731 lvItem
.mask
|= LVIF_TEXT
;
732 lvItem
.pszText
= new wxChar
[513];
733 lvItem
.cchTextMax
= 512;
737 lvItem
.pszText
= NULL
;
740 if (info
.m_mask
& wxLIST_MASK_DATA
)
741 lvItem
.mask
|= LVIF_PARAM
;
743 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
744 lvItem
.mask
|= LVIF_IMAGE
;
746 if ( info
.m_mask
& wxLIST_MASK_STATE
)
748 lvItem
.mask
|= LVIF_STATE
;
749 wxConvertToMSWFlags(0, info
.m_stateMask
, lvItem
);
752 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
755 wxLogError(_("Couldn't retrieve information about list control item %d."),
760 // give NULL as hwnd as we already have everything we need
761 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
765 delete[] lvItem
.pszText
;
770 // Sets information about the item
771 bool wxListCtrl::SetItem(wxListItem
& info
)
773 const long id
= info
.GetId();
774 wxCHECK_MSG( id
>= 0 && id
< GetItemCount(), false,
775 wxT("invalid item index in SetItem") );
778 wxConvertToMSWListItem(this, info
, item
);
780 // we never update the lParam if it contains our pointer
781 // to the wxMSWListItemData structure
782 item
.mask
&= ~LVIF_PARAM
;
784 // check if setting attributes or lParam
785 if ( info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
) )
787 // get internal item data
788 wxMSWListItemData
*data
= MSWGetItemData(id
);
792 // need to allocate the internal data object
793 data
= new wxMSWListItemData
;
794 m_internalData
.push_back(data
);
795 item
.lParam
= (LPARAM
) data
;
796 item
.mask
|= LVIF_PARAM
;
801 if ( info
.m_mask
& wxLIST_MASK_DATA
)
802 data
->lParam
= info
.m_data
;
805 if ( info
.HasAttributes() )
807 const wxListItemAttr
& attrNew
= *info
.GetAttributes();
809 // don't overwrite the already set attributes if we have them
811 data
->attr
->AssignFrom(attrNew
);
813 data
->attr
= new wxListItemAttr(attrNew
);
818 // we could be changing only the attribute in which case we don't need to
819 // call ListView_SetItem() at all
822 if ( !ListView_SetItem(GetHwnd(), &item
) )
824 wxLogDebug(wxT("ListView_SetItem() failed"));
830 // we need to update the item immediately to show the new image
831 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
833 // check whether it has any custom attributes
834 if ( info
.HasAttributes() )
838 // if the colour has changed, we must redraw the item
844 // we need this to make the change visible right now
845 RefreshItem(item
.iItem
);
851 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
855 info
.m_mask
= wxLIST_MASK_TEXT
;
856 info
.m_itemId
= index
;
860 info
.m_image
= imageId
;
861 info
.m_mask
|= wxLIST_MASK_IMAGE
;
863 return SetItem(info
);
867 // Gets the item state
868 int wxListCtrl::GetItemState(long item
, long stateMask
) const
872 info
.m_mask
= wxLIST_MASK_STATE
;
873 info
.m_stateMask
= stateMask
;
874 info
.m_itemId
= item
;
882 // Sets the item state
883 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
885 // NB: don't use SetItem() here as it doesn't work with the virtual list
888 wxZeroMemory(lvItem
);
890 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
892 const bool changingFocus
= (stateMask
& wxLIST_STATE_FOCUSED
) &&
893 (state
& wxLIST_STATE_FOCUSED
);
895 // for the virtual list controls we need to refresh the previously focused
896 // item manually when changing focus without changing selection
897 // programmatically because otherwise it keeps its focus rectangle until
898 // next repaint (yet another comctl32 bug)
900 if ( IsVirtual() && changingFocus
)
902 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
909 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
910 (WPARAM
)item
, (LPARAM
)&lvItem
) )
912 wxLogLastError(wxT("ListView_SetItemState"));
917 if ( focusOld
!= -1 )
919 // no need to refresh the item if it was previously selected, it would
920 // only result in annoying flicker
921 if ( !(GetItemState(focusOld
,
922 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
924 RefreshItem(focusOld
);
928 // we expect the selection anchor, i.e. the item from which multiple
929 // selection (such as performed with e.g. Shift-arrows) starts, to be the
930 // same as the currently focused item but the native control doesn't update
931 // it when we change focus and leaves at the last item it set itself focus
932 // to, so do it explicitly
933 if ( changingFocus
&& !HasFlag(wxLC_SINGLE_SEL
) )
935 ListView_SetSelectionMark(GetHwnd(), item
);
941 // Sets the item image
942 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
944 return SetItemColumnImage(item
, 0, image
);
947 // Sets the item image
948 bool wxListCtrl::SetItemColumnImage(long item
, long column
, int image
)
952 info
.m_mask
= wxLIST_MASK_IMAGE
;
953 info
.m_image
= image
;
954 info
.m_itemId
= item
;
957 return SetItem(info
);
960 // Gets the item text
961 wxString
wxListCtrl::GetItemText(long item
, int col
) const
965 info
.m_mask
= wxLIST_MASK_TEXT
;
966 info
.m_itemId
= item
;
970 return wxEmptyString
;
974 // Sets the item text
975 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
979 info
.m_mask
= wxLIST_MASK_TEXT
;
980 info
.m_itemId
= item
;
986 // Gets the internal item data
987 wxMSWListItemData
*wxListCtrl::MSWGetItemData(long itemId
) const
990 it
.mask
= LVIF_PARAM
;
993 if ( !ListView_GetItem(GetHwnd(), &it
) )
996 return (wxMSWListItemData
*) it
.lParam
;
999 // Gets the item data
1000 wxUIntPtr
wxListCtrl::GetItemData(long item
) const
1004 info
.m_mask
= wxLIST_MASK_DATA
;
1005 info
.m_itemId
= item
;
1012 // Sets the item data
1013 bool wxListCtrl::SetItemPtrData(long item
, wxUIntPtr data
)
1017 info
.m_mask
= wxLIST_MASK_DATA
;
1018 info
.m_itemId
= item
;
1021 return SetItem(info
);
1024 wxRect
wxListCtrl::GetViewRect() const
1028 // ListView_GetViewRect() can only be used in icon and small icon views
1029 // (this is documented in MSDN and, indeed, it returns bogus results in
1030 // report view, at least with comctl32.dll v6 under Windows 2003)
1031 if ( HasFlag(wxLC_ICON
| wxLC_SMALL_ICON
) )
1034 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1036 wxLogDebug(wxT("ListView_GetViewRect() failed."));
1041 wxCopyRECTToRect(rc
, rect
);
1043 else if ( HasFlag(wxLC_REPORT
) )
1045 const long count
= GetItemCount();
1048 GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count
- 1), rect
);
1050 // extend the rectangle to start at the top (we include the column
1051 // headers, if any, for compatibility with the generic version)
1052 rect
.height
+= rect
.y
;
1058 wxFAIL_MSG( wxT("not implemented in this mode") );
1064 // Gets the item rectangle
1065 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1067 return GetSubItemRect( item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
) ;
1070 bool wxListCtrl::GetSubItemRect(long item
, long subItem
, wxRect
& rect
, int code
) const
1072 // ListView_GetSubItemRect() doesn't do subItem error checking and returns
1073 // true even for the out of range values of it (even if the results are
1074 // completely bogus in this case), so we check item validity ourselves
1075 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
||
1076 (subItem
>= 0 && subItem
< GetColumnCount()),
1077 false, wxT("invalid sub item index") );
1079 // use wxCHECK_MSG against "item" too, for coherency with the generic implementation:
1080 wxCHECK_MSG( item
>= 0 && item
< GetItemCount(), false,
1081 wxT("invalid item in GetSubItemRect") );
1084 if ( code
== wxLIST_RECT_BOUNDS
)
1085 codeWin
= LVIR_BOUNDS
;
1086 else if ( code
== wxLIST_RECT_ICON
)
1087 codeWin
= LVIR_ICON
;
1088 else if ( code
== wxLIST_RECT_LABEL
)
1089 codeWin
= LVIR_LABEL
;
1092 wxFAIL_MSG( wxT("incorrect code in GetItemRect() / GetSubItemRect()") );
1093 codeWin
= LVIR_BOUNDS
;
1097 if ( !wxGetListCtrlSubItemRect
1101 subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
? 0 : subItem
,
1109 wxCopyRECTToRect(rectWin
, rect
);
1111 // there is no way to retrieve the first sub item bounding rectangle using
1112 // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to
1113 // truncate it at first column ourselves
1114 if ( subItem
== 0 && code
== wxLIST_RECT_BOUNDS
)
1115 rect
.width
= GetColumnWidth(0);
1123 // Gets the item position
1124 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1128 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1130 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1134 // Sets the item position.
1135 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1137 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1140 // Gets the number of items in the list control
1141 int wxListCtrl::GetItemCount() const
1146 wxSize
wxListCtrl::GetItemSpacing() const
1148 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1150 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1153 #if WXWIN_COMPATIBILITY_2_6
1155 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1157 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1160 #endif // WXWIN_COMPATIBILITY_2_6
1162 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1165 info
.m_itemId
= item
;
1166 info
.SetTextColour( col
);
1170 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1173 wxMSWListItemData
*data
= MSWGetItemData(item
);
1174 if ( data
&& data
->attr
)
1175 col
= data
->attr
->GetTextColour();
1180 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1183 info
.m_itemId
= item
;
1184 info
.SetBackgroundColour( col
);
1188 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1191 wxMSWListItemData
*data
= MSWGetItemData(item
);
1192 if ( data
&& data
->attr
)
1193 col
= data
->attr
->GetBackgroundColour();
1198 void wxListCtrl::SetItemFont( long item
, const wxFont
&f
)
1201 info
.m_itemId
= item
;
1206 wxFont
wxListCtrl::GetItemFont( long item
) const
1209 wxMSWListItemData
*data
= MSWGetItemData(item
);
1210 if ( data
&& data
->attr
)
1211 f
= data
->attr
->GetFont();
1216 // Gets the number of selected items in the list control
1217 int wxListCtrl::GetSelectedItemCount() const
1219 return ListView_GetSelectedCount(GetHwnd());
1222 // Gets the text colour of the listview
1223 wxColour
wxListCtrl::GetTextColour() const
1225 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1226 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1230 // Sets the text colour of the listview
1231 void wxListCtrl::SetTextColour(const wxColour
& col
)
1233 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1236 // Gets the index of the topmost visible item when in
1237 // list or report view
1238 long wxListCtrl::GetTopItem() const
1240 return (long) ListView_GetTopIndex(GetHwnd());
1243 // Searches for an item, starting from 'item'.
1244 // 'geometry' is one of
1245 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1246 // 'state' is a state bit flag, one or more of
1247 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1248 // item can be -1 to find the first item that matches the
1250 // Returns the item or -1 if unsuccessful.
1251 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1255 if ( geom
== wxLIST_NEXT_ABOVE
)
1256 flags
|= LVNI_ABOVE
;
1257 if ( geom
== wxLIST_NEXT_ALL
)
1259 if ( geom
== wxLIST_NEXT_BELOW
)
1260 flags
|= LVNI_BELOW
;
1261 if ( geom
== wxLIST_NEXT_LEFT
)
1262 flags
|= LVNI_TOLEFT
;
1263 if ( geom
== wxLIST_NEXT_RIGHT
)
1264 flags
|= LVNI_TORIGHT
;
1266 if ( state
& wxLIST_STATE_CUT
)
1268 if ( state
& wxLIST_STATE_DROPHILITED
)
1269 flags
|= LVNI_DROPHILITED
;
1270 if ( state
& wxLIST_STATE_FOCUSED
)
1271 flags
|= LVNI_FOCUSED
;
1272 if ( state
& wxLIST_STATE_SELECTED
)
1273 flags
|= LVNI_SELECTED
;
1275 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1279 wxImageList
*wxListCtrl::GetImageList(int which
) const
1281 if ( which
== wxIMAGE_LIST_NORMAL
)
1283 return m_imageListNormal
;
1285 else if ( which
== wxIMAGE_LIST_SMALL
)
1287 return m_imageListSmall
;
1289 else if ( which
== wxIMAGE_LIST_STATE
)
1291 return m_imageListState
;
1296 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1299 if ( which
== wxIMAGE_LIST_NORMAL
)
1301 flags
= LVSIL_NORMAL
;
1302 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1303 m_imageListNormal
= imageList
;
1304 m_ownsImageListNormal
= false;
1306 else if ( which
== wxIMAGE_LIST_SMALL
)
1308 flags
= LVSIL_SMALL
;
1309 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1310 m_imageListSmall
= imageList
;
1311 m_ownsImageListSmall
= false;
1313 else if ( which
== wxIMAGE_LIST_STATE
)
1315 flags
= LVSIL_STATE
;
1316 if (m_ownsImageListState
) delete m_imageListState
;
1317 m_imageListState
= imageList
;
1318 m_ownsImageListState
= false;
1320 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1323 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1325 SetImageList(imageList
, which
);
1326 if ( which
== wxIMAGE_LIST_NORMAL
)
1327 m_ownsImageListNormal
= true;
1328 else if ( which
== wxIMAGE_LIST_SMALL
)
1329 m_ownsImageListSmall
= true;
1330 else if ( which
== wxIMAGE_LIST_STATE
)
1331 m_ownsImageListState
= true;
1334 // ----------------------------------------------------------------------------
1336 // ----------------------------------------------------------------------------
1338 // Arranges the items
1339 bool wxListCtrl::Arrange(int flag
)
1342 if ( flag
== wxLIST_ALIGN_LEFT
)
1343 code
= LVA_ALIGNLEFT
;
1344 else if ( flag
== wxLIST_ALIGN_TOP
)
1345 code
= LVA_ALIGNTOP
;
1346 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1348 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1349 code
= LVA_SNAPTOGRID
;
1351 return (ListView_Arrange(GetHwnd(), code
) != 0);
1355 bool wxListCtrl::DeleteItem(long item
)
1357 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1359 wxLogLastError(wxT("ListView_DeleteItem"));
1364 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1365 wxT("m_count should match ListView_GetItemCount"));
1367 // the virtual list control doesn't refresh itself correctly, help it
1370 // we need to refresh all the lines below the one which was deleted
1372 if ( item
> 0 && GetItemCount() )
1374 GetItemRect(item
- 1, rectItem
);
1379 rectItem
.height
= 0;
1382 wxRect rectWin
= GetRect();
1383 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1384 rectWin
.y
= rectItem
.GetBottom();
1386 RefreshRect(rectWin
);
1392 // Deletes all items
1393 bool wxListCtrl::DeleteAllItems()
1395 // Calling ListView_DeleteAllItems() will always generate an event but we
1396 // shouldn't do it if the control is empty
1397 return !GetItemCount() || ListView_DeleteAllItems(GetHwnd()) != 0;
1400 // Deletes all items
1401 bool wxListCtrl::DeleteAllColumns()
1403 while ( m_colCount
> 0 )
1405 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1407 wxLogLastError(wxT("ListView_DeleteColumn"));
1415 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1421 bool wxListCtrl::DeleteColumn(int col
)
1423 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1425 if ( success
&& (m_colCount
> 0) )
1430 // Clears items, and columns if there are any.
1431 void wxListCtrl::ClearAll()
1434 if ( m_colCount
> 0 )
1438 void wxListCtrl::InitEditControl(WXHWND hWnd
)
1440 m_textCtrl
->SetHWND(hWnd
);
1441 m_textCtrl
->SubclassWin(hWnd
);
1442 m_textCtrl
->SetParent(this);
1444 // we must disallow TABbing away from the control while the edit contol is
1445 // shown because this leaves it in some strange state (just try removing
1446 // this line and then pressing TAB while editing an item in listctrl
1448 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1451 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1453 wxCHECK_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)), NULL
,
1454 "control used for label editing must be a wxTextCtrl" );
1456 // ListView_EditLabel requires that the list has focus.
1459 // create m_textCtrl here before calling ListView_EditLabel() because it
1460 // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and
1461 // the user handler for it can call GetEditControl() resulting in an on
1462 // demand creation of a stock wxTextCtrl instead of the control of a
1463 // (possibly) custom wxClassInfo
1464 DeleteEditControl();
1465 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1467 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1470 // failed to start editing
1471 wxDELETE(m_textCtrl
);
1476 // if GetEditControl() hasn't been called, we need to initialize the edit
1477 // control ourselves
1478 if ( !m_textCtrl
->GetHWND() )
1479 InitEditControl(hWnd
);
1484 // End label editing, optionally cancelling the edit
1485 bool wxListCtrl::EndEditLabel(bool cancel
)
1487 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1488 HWND hwnd
= ListView_GetEditControl(GetHwnd());
1492 // Newer versions of Windows have a special message for cancelling editing,
1493 // use it if available.
1494 #ifdef ListView_CancelEditLabel
1495 if ( cancel
&& (wxApp::GetComCtl32Version() >= 600) )
1497 ListView_CancelEditLabel(GetHwnd());
1500 #endif // ListView_CancelEditLabel
1502 // We shouldn't destroy the control ourselves according to MSDN, which
1503 // proposes WM_CANCELMODE to do this, but it doesn't seem to work so
1504 // emulate the corresponding user action instead.
1505 ::SendMessage(hwnd
, WM_KEYDOWN
, cancel
? VK_ESCAPE
: VK_RETURN
, 0);
1511 // Ensures this item is visible
1512 bool wxListCtrl::EnsureVisible(long item
)
1514 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1517 // Find an item whose label matches this string, starting from the item after 'start'
1518 // or the beginning if 'start' is -1.
1519 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1521 LV_FINDINFO findInfo
;
1523 findInfo
.flags
= LVFI_STRING
;
1525 findInfo
.flags
|= LVFI_PARTIAL
;
1526 findInfo
.psz
= str
.wx_str();
1528 // ListView_FindItem() excludes the first item from search and to look
1529 // through all the items you need to start from -1 which is unnatural and
1530 // inconsistent with the generic version - so we adjust the index
1533 return ListView_FindItem(GetHwnd(), start
, &findInfo
);
1536 // Find an item whose data matches this data, starting from the item after
1537 // 'start' or the beginning if 'start' is -1.
1538 long wxListCtrl::FindItem(long start
, wxUIntPtr data
)
1540 // we can't use ListView_FindItem() directly as we don't store the data
1541 // pointer itself in the control but rather our own internal data, so first
1542 // we need to find the right value to search for (and there can be several
1544 int idx
= wxNOT_FOUND
;
1545 const unsigned count
= m_internalData
.size();
1546 for ( unsigned n
= 0; n
< count
; n
++ )
1548 if ( m_internalData
[n
]->lParam
== (LPARAM
)data
)
1550 LV_FINDINFO findInfo
;
1551 findInfo
.flags
= LVFI_PARAM
;
1552 findInfo
.lParam
= (LPARAM
)wxPtrToUInt(m_internalData
[n
]);
1554 int rc
= ListView_FindItem(GetHwnd(), start
, &findInfo
);
1557 if ( idx
== wxNOT_FOUND
|| rc
< idx
)
1560 if ( idx
== start
+ 1 )
1562 // we can stop here, we don't risk finding a closer
1567 //else: this item is after the previously found one
1575 // Find an item nearest this position in the specified direction, starting from
1576 // the item after 'start' or the beginning if 'start' is -1.
1577 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1579 LV_FINDINFO findInfo
;
1581 findInfo
.flags
= LVFI_NEARESTXY
;
1582 findInfo
.pt
.x
= pt
.x
;
1583 findInfo
.pt
.y
= pt
.y
;
1584 findInfo
.vkDirection
= VK_RIGHT
;
1586 if ( direction
== wxLIST_FIND_UP
)
1587 findInfo
.vkDirection
= VK_UP
;
1588 else if ( direction
== wxLIST_FIND_DOWN
)
1589 findInfo
.vkDirection
= VK_DOWN
;
1590 else if ( direction
== wxLIST_FIND_LEFT
)
1591 findInfo
.vkDirection
= VK_LEFT
;
1592 else if ( direction
== wxLIST_FIND_RIGHT
)
1593 findInfo
.vkDirection
= VK_RIGHT
;
1595 return ListView_FindItem(GetHwnd(), start
, &findInfo
);
1598 // Determines which item (if any) is at the specified point,
1599 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1601 wxListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *ptrSubItem
) const
1603 LV_HITTESTINFO hitTestInfo
;
1604 hitTestInfo
.pt
.x
= (int) point
.x
;
1605 hitTestInfo
.pt
.y
= (int) point
.y
;
1608 #ifdef LVM_SUBITEMHITTEST
1609 if ( ptrSubItem
&& wxApp::GetComCtl32Version() >= 470 )
1611 item
= ListView_SubItemHitTest(GetHwnd(), &hitTestInfo
);
1612 *ptrSubItem
= hitTestInfo
.iSubItem
;
1615 #endif // LVM_SUBITEMHITTEST
1617 item
= ListView_HitTest(GetHwnd(), &hitTestInfo
);
1622 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1623 flags
|= wxLIST_HITTEST_ABOVE
;
1624 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1625 flags
|= wxLIST_HITTEST_BELOW
;
1626 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1627 flags
|= wxLIST_HITTEST_TOLEFT
;
1628 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1629 flags
|= wxLIST_HITTEST_TORIGHT
;
1631 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1632 flags
|= wxLIST_HITTEST_NOWHERE
;
1634 // note a bug or at least a very strange feature of comtl32.dll (tested
1635 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1636 // the right of the item label, ListView_HitTest() returns a combination of
1637 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1638 // the bits which don't make sense
1639 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1641 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1643 // do not translate LVHT_ONITEMICON here, as per above
1647 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1648 flags
|= wxLIST_HITTEST_ONITEMICON
;
1649 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1650 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1657 // Inserts an item, returning the index of the new item if successful,
1659 long wxListCtrl::InsertItem(const wxListItem
& info
)
1661 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual controls") );
1664 wxConvertToMSWListItem(this, info
, item
);
1665 item
.mask
&= ~LVIF_PARAM
;
1667 // check whether we need to allocate our internal data
1668 bool needInternalData
= (info
.m_mask
& wxLIST_MASK_DATA
) ||
1669 info
.HasAttributes();
1670 if ( needInternalData
)
1672 item
.mask
|= LVIF_PARAM
;
1674 wxMSWListItemData
* const data
= new wxMSWListItemData
;
1675 m_internalData
.push_back(data
);
1676 item
.lParam
= (LPARAM
)data
;
1678 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1679 data
->lParam
= info
.m_data
;
1681 // check whether it has any custom attributes
1682 if ( info
.HasAttributes() )
1684 // take copy of attributes
1685 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1687 // and remember that we have some now...
1688 m_hasAnyAttr
= true;
1692 const long rv
= ListView_InsertItem(GetHwnd(), & item
);
1694 // failing to insert the item is really unexpected
1695 wxCHECK_MSG( rv
!= -1, rv
, "failed to insert an item in wxListCtrl" );
1698 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1699 wxT("m_count should match ListView_GetItemCount"));
1704 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1707 info
.m_text
= label
;
1708 info
.m_mask
= wxLIST_MASK_TEXT
;
1709 info
.m_itemId
= index
;
1710 return InsertItem(info
);
1713 // Inserts an image item
1714 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1717 info
.m_image
= imageIndex
;
1718 info
.m_mask
= wxLIST_MASK_IMAGE
;
1719 info
.m_itemId
= index
;
1720 return InsertItem(info
);
1723 // Inserts an image/string item
1724 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1727 info
.m_image
= imageIndex
;
1728 info
.m_text
= label
;
1729 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1730 info
.m_itemId
= index
;
1731 return InsertItem(info
);
1734 // For list view mode (only), inserts a column.
1735 long wxListCtrl::InsertColumn(long col
, const wxListItem
& item
)
1738 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
1740 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1742 // always give some width to the new column: this one is compatible
1743 // with the generic version
1744 lvCol
.mask
|= LVCF_WIDTH
;
1748 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1753 else // failed to insert?
1755 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1762 long wxListCtrl::InsertColumn(long col
,
1763 const wxString
& heading
,
1768 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1769 item
.m_text
= heading
;
1772 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1773 item
.m_width
= width
;
1775 item
.m_format
= format
;
1777 return InsertColumn(col
, item
);
1780 // scroll the control by the given number of pixels (exception: in list view,
1781 // dx is interpreted as number of columns)
1782 bool wxListCtrl::ScrollList(int dx
, int dy
)
1784 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1786 wxLogDebug(wxT("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1796 // fn is a function which takes 3 long arguments: item1, item2, data.
1797 // item1 is the long data associated with a first item (NOT the index).
1798 // item2 is the long data associated with a second item (NOT the index).
1799 // data is the same value as passed to SortItems.
1800 // The return value is a negative number if the first item should precede the second
1801 // item, a positive number of the second item should precede the first,
1802 // or zero if the two items are equivalent.
1804 // data is arbitrary data to be passed to the sort function.
1806 // Internal structures for proxying the user compare function
1807 // so that we can pass it the *real* user data
1809 // translate lParam data and call user func
1810 struct wxInternalDataSort
1812 wxListCtrlCompare user_fn
;
1816 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1818 wxInternalDataSort
* const internalData
= (wxInternalDataSort
*) lParamSort
;
1820 wxMSWListItemData
*data1
= (wxMSWListItemData
*) lParam1
;
1821 wxMSWListItemData
*data2
= (wxMSWListItemData
*) lParam2
;
1823 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1824 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1826 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1830 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, wxIntPtr data
)
1832 wxInternalDataSort internalData
;
1833 internalData
.user_fn
= fn
;
1834 internalData
.data
= data
;
1836 // WPARAM cast is needed for mingw/cygwin
1837 if ( !ListView_SortItems(GetHwnd(),
1838 wxInternalDataCompareFunc
,
1839 (WPARAM
) &internalData
) )
1841 wxLogDebug(wxT("ListView_SortItems() failed"));
1851 // ----------------------------------------------------------------------------
1852 // message processing
1853 // ----------------------------------------------------------------------------
1855 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1857 if ( msg
->message
== WM_KEYDOWN
)
1859 // Only eat VK_RETURN if not being used by the application in
1860 // conjunction with modifiers
1861 if ( msg
->wParam
== VK_RETURN
&& !wxIsAnyModifierDown() )
1863 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
1867 return wxControl::MSWShouldPreProcessMessage(msg
);
1870 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
1872 const int id
= (signed short)id_
;
1873 if (cmd
== EN_UPDATE
)
1875 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1876 event
.SetEventObject( this );
1877 ProcessCommand(event
);
1880 else if (cmd
== EN_KILLFOCUS
)
1882 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1883 event
.SetEventObject( this );
1884 ProcessCommand(event
);
1891 // utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify
1892 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
)
1894 // find the column clicked: we have to search for it ourselves as the
1895 // notification message doesn't provide this info
1897 // where did the click occur?
1898 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1899 if ( nmhdr
->code
== GN_CONTEXTMENU
)
1901 *ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1904 #endif //__WXWINCE__
1905 if ( !::GetCursorPos(ptClick
) )
1907 wxLogLastError(wxT("GetCursorPos"));
1910 // we need to use listctrl coordinates for the event point so this is what
1911 // we return in ptClick, but for comparison with Header_GetItemRect()
1912 // result below we need to use header window coordinates
1913 POINT ptClickHeader
= *ptClick
;
1914 if ( !::ScreenToClient(nmhdr
->hwndFrom
, &ptClickHeader
) )
1916 wxLogLastError(wxT("ScreenToClient(listctrl header)"));
1919 if ( !::ScreenToClient(::GetParent(nmhdr
->hwndFrom
), ptClick
) )
1921 wxLogLastError(wxT("ScreenToClient(listctrl)"));
1924 const int colCount
= Header_GetItemCount(nmhdr
->hwndFrom
);
1925 for ( int col
= 0; col
< colCount
; col
++ )
1928 if ( Header_GetItemRect(nmhdr
->hwndFrom
, col
, &rect
) )
1930 if ( ::PtInRect(&rect
, ptClickHeader
) )
1940 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1943 // prepare the event
1944 // -----------------
1946 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1947 event
.SetEventObject(this);
1949 wxEventType eventType
= wxEVT_NULL
;
1951 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1953 // if your compiler is as broken as this, you should really change it: this
1954 // code is needed for normal operation! #ifdef below is only useful for
1955 // automatic rebuilds which are done with a very old compiler version
1956 #ifdef HDN_BEGINTRACKA
1958 // check for messages from the header (in report view)
1959 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1961 // is it a message from the header?
1962 if ( nmhdr
->hwndFrom
== hwndHdr
)
1964 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1966 event
.m_itemIndex
= -1;
1968 bool ignore
= false;
1969 switch ( nmhdr
->code
)
1971 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1972 // TRACK messages even to ANSI programs: on my system I get
1973 // HDN_BEGINTRACKW and HDN_ENDTRACKA!
1975 // work around is to simply catch both versions and hope that it
1976 // works (why should this message exist in ANSI and Unicode is
1977 // beyond me as it doesn't deal with strings at all...)
1979 // another problem is that HDN_TRACK is not sent at all by header
1980 // with HDS_FULLDRAG style which is used by default by wxListCtrl
1981 // under recent Windows versions (starting from at least XP) so we
1982 // need to use HDN_ITEMCHANGING instead of it
1983 case HDN_BEGINTRACKA
:
1984 case HDN_BEGINTRACKW
:
1985 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1988 case HDN_ITEMCHANGING
:
1989 if ( eventType
== wxEVT_NULL
)
1991 if ( !nmHDR
->pitem
|| !(nmHDR
->pitem
->mask
& HDI_WIDTH
) )
1993 // something other than the width is being changed,
1999 // also ignore the events sent when the width didn't really
2000 // change: this is not just an optimization but also gets
2001 // rid of a useless and unexpected DRAGGING event which
2002 // would otherwise be sent after the END_DRAG one as we get
2003 // an HDN_ITEMCHANGING after HDN_ENDTRACK for some reason
2004 if ( nmHDR
->pitem
->cxy
== GetColumnWidth(nmHDR
->iItem
) )
2010 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
2016 if ( eventType
== wxEVT_NULL
)
2017 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
2019 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
2020 event
.m_col
= nmHDR
->iItem
;
2023 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2024 case GN_CONTEXTMENU
:
2025 #endif //__WXWINCE__
2030 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
2031 event
.m_col
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
2032 event
.m_pointDrag
.x
= ptClick
.x
;
2033 event
.m_pointDrag
.y
= ptClick
.y
;
2037 case HDN_GETDISPINFOW
:
2038 // letting Windows XP handle this message results in mysterious
2039 // crashes in comctl32.dll seemingly because of bad message
2042 // I have no idea what is the real cause of the bug (which is,
2043 // just to make things interesting, impossible to reproduce
2044 // reliably) but ignoring all these messages does fix it and
2045 // doesn't seem to have any negative consequences
2053 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2056 #endif // defined(HDN_BEGINTRACKA)
2057 if ( nmhdr
->hwndFrom
== GetHwnd() )
2059 // almost all messages use NM_LISTVIEW
2060 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
2062 const int iItem
= nmLV
->iItem
;
2065 // If we have a valid item then check if there is a data value
2066 // associated with it and put it in the event.
2067 if ( iItem
>= 0 && iItem
< GetItemCount() )
2069 wxMSWListItemData
*internaldata
=
2070 MSWGetItemData(iItem
);
2073 event
.m_item
.m_data
= internaldata
->lParam
;
2076 bool processed
= true;
2077 switch ( nmhdr
->code
)
2079 case LVN_BEGINRDRAG
:
2080 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
2084 if ( eventType
== wxEVT_NULL
)
2086 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2089 event
.m_itemIndex
= iItem
;
2090 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
2091 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
2094 // NB: we have to handle both *A and *W versions here because some
2095 // versions of comctl32.dll send ANSI messages even to the
2097 case LVN_BEGINLABELEDITA
:
2098 case LVN_BEGINLABELEDITW
:
2101 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
2103 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
2105 else // LVN_BEGINLABELEDITW
2107 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
2110 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
2111 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
2112 event
.m_itemIndex
= event
.m_item
.m_itemId
;
2116 case LVN_ENDLABELEDITA
:
2117 case LVN_ENDLABELEDITW
:
2120 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
2122 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
2124 else // LVN_ENDLABELEDITW
2126 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
2129 // was editing cancelled?
2130 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
2131 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
2133 // EDIT control will be deleted by the list control
2134 // itself so prevent us from deleting it as well
2135 DeleteEditControl();
2137 event
.SetEditCanceled(true);
2140 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
2141 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
2142 event
.m_itemIndex
= event
.m_item
.m_itemId
;
2146 case LVN_COLUMNCLICK
:
2147 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
2148 event
.m_itemIndex
= -1;
2149 event
.m_col
= nmLV
->iSubItem
;
2152 case LVN_DELETEALLITEMS
:
2153 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
2154 event
.m_itemIndex
= -1;
2157 case LVN_DELETEITEM
:
2160 // this should be prevented by the post-processing code
2161 // below, but "just in case"
2165 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2166 event
.m_itemIndex
= iItem
;
2170 case LVN_INSERTITEM
:
2171 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2172 event
.m_itemIndex
= iItem
;
2175 case LVN_ITEMCHANGED
:
2176 // we translate this catch all message into more interesting
2177 // (and more easy to process) wxWidgets events
2179 // first of all, we deal with the state change events only and
2180 // only for valid items (item == -1 for the virtual list
2182 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2184 // temp vars for readability
2185 const UINT stOld
= nmLV
->uOldState
;
2186 const UINT stNew
= nmLV
->uNewState
;
2188 event
.m_item
.SetId(iItem
);
2189 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2192 GetItem(event
.m_item
);
2194 // has the focus changed?
2195 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2197 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2198 event
.m_itemIndex
= iItem
;
2201 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2203 if ( eventType
!= wxEVT_NULL
)
2205 // focus and selection have both changed: send the
2206 // focus event from here and the selection one
2208 event
.SetEventType(eventType
);
2209 (void)HandleWindowEvent(event
);
2211 else // no focus event to send
2213 // then need to set m_itemIndex as it wasn't done
2215 event
.m_itemIndex
= iItem
;
2218 eventType
= stNew
& LVIS_SELECTED
2219 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2220 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2224 if ( eventType
== wxEVT_NULL
)
2226 // not an interesting event for us
2234 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2235 WORD wVKey
= info
->wVKey
;
2237 // get the current selection
2238 long lItem
= GetNextItem(-1,
2240 wxLIST_STATE_SELECTED
);
2242 // <Enter> or <Space> activate the selected item if any (but
2243 // not with any modifiers as they have a predefined meaning
2246 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2247 !wxIsAnyModifierDown() )
2249 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2253 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2255 event
.m_code
= wxMSWKeyboard::VKToWX(wVKey
);
2257 if ( event
.m_code
== WXK_NONE
)
2259 // We can't translate this to a standard key code,
2260 // until support for Unicode key codes is added to
2261 // wxListEvent we just ignore them.
2267 event
.m_item
.m_itemId
= lItem
;
2271 // fill the other fields too
2272 event
.m_item
.m_text
= GetItemText(lItem
);
2273 event
.m_item
.m_data
= GetItemData(lItem
);
2279 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2281 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2286 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2287 // if it happened on an item (and not on empty place)
2294 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2295 event
.m_itemIndex
= iItem
;
2296 event
.m_item
.m_text
= GetItemText(iItem
);
2297 event
.m_item
.m_data
= GetItemData(iItem
);
2300 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2301 case GN_CONTEXTMENU
:
2302 #endif //__WXWINCE__
2304 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2305 // don't do anything else
2306 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2311 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2312 LV_HITTESTINFO lvhti
;
2313 wxZeroMemory(lvhti
);
2315 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2316 if ( nmhdr
->code
== GN_CONTEXTMENU
)
2318 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2321 #endif //__WXWINCE__
2323 ::GetCursorPos(&(lvhti
.pt
));
2326 ::ScreenToClient(GetHwnd(), &lvhti
.pt
);
2327 if ( ListView_HitTest(GetHwnd(), &lvhti
) != -1 )
2329 if ( lvhti
.flags
& LVHT_ONITEM
)
2331 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2332 event
.m_itemIndex
= lvhti
.iItem
;
2333 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2334 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2339 #ifdef NM_CUSTOMDRAW
2341 *result
= OnCustomDraw(lParam
);
2343 return *result
!= CDRF_DODEFAULT
;
2344 #endif // _WIN32_IE >= 0x300
2346 case LVN_ODCACHEHINT
:
2348 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2350 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2352 // we get some really stupid cache hints like ones for
2353 // items in range 0..0 for an empty control or, after
2354 // deleting an item, for items in invalid range -- filter
2356 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2359 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2361 const long iMax
= GetItemCount();
2362 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2367 #ifdef HAVE_NMLVFINDITEM
2368 case LVN_ODFINDITEM
:
2369 // this message is only used with the virtual list control but
2370 // even there we don't want to always use it: in a control with
2371 // sufficiently big number of items (defined as > 1000 here),
2372 // accidentally pressing a key could result in hanging an
2373 // application waiting while it performs linear search
2374 if ( IsVirtual() && GetItemCount() <= 1000 )
2376 NMLVFINDITEM
* pFindInfo
= (NMLVFINDITEM
*)lParam
;
2378 // no match by default
2381 // we only handle string-based searches here
2383 // TODO: what about LVFI_PARTIAL, should we handle this?
2384 if ( !(pFindInfo
->lvfi
.flags
& LVFI_STRING
) )
2389 const wxChar
* const searchstr
= pFindInfo
->lvfi
.psz
;
2390 const size_t len
= wxStrlen(searchstr
);
2392 // this is the first item we should examine, search from it
2393 // wrapping if necessary
2394 const int startPos
= pFindInfo
->iStart
;
2395 const int maxPos
= GetItemCount();
2396 wxCHECK_MSG( startPos
<= maxPos
, false,
2397 wxT("bad starting position in LVN_ODFINDITEM") );
2399 int currentPos
= startPos
;
2402 // wrap to the beginning if necessary
2403 if ( currentPos
== maxPos
)
2405 // somewhat surprisingly, LVFI_WRAP isn't set in
2406 // flags but we still should wrap
2410 // does this item begin with searchstr?
2411 if ( wxStrnicmp(searchstr
,
2412 GetItemText(currentPos
), len
) == 0 )
2414 *result
= currentPos
;
2418 while ( ++currentPos
!= startPos
);
2420 if ( *result
== -1 )
2426 SetItemState(*result
,
2427 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
,
2428 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
);
2429 EnsureVisible(*result
);
2437 #endif // HAVE_NMLVFINDITEM
2439 case LVN_GETDISPINFO
:
2442 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2444 LV_ITEM
& lvi
= info
->item
;
2445 long item
= lvi
.iItem
;
2447 if ( lvi
.mask
& LVIF_TEXT
)
2449 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2450 wxStrlcpy(lvi
.pszText
, text
.c_str(), lvi
.cchTextMax
);
2453 // see comment at the end of wxListCtrl::GetColumn()
2454 #ifdef NM_CUSTOMDRAW
2455 if ( lvi
.mask
& LVIF_IMAGE
)
2457 lvi
.iImage
= OnGetItemColumnImage(item
, lvi
.iSubItem
);
2459 #endif // NM_CUSTOMDRAW
2461 // even though we never use LVM_SETCALLBACKMASK, we still
2462 // can get messages with LVIF_STATE in lvi.mask under Vista
2463 if ( lvi
.mask
& LVIF_STATE
)
2465 // we don't have anything to return from here...
2478 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2482 // where did this one come from?
2486 // process the event
2487 // -----------------
2489 event
.SetEventType(eventType
);
2491 // fill in the item before passing it to the event handler if we do have a
2492 // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED)
2493 if ( event
.m_itemIndex
!= -1 && !event
.m_item
.GetMask() )
2495 wxListItem
& item
= event
.m_item
;
2497 item
.SetId(event
.m_itemIndex
);
2498 item
.SetMask(wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
| wxLIST_MASK_DATA
);
2502 bool processed
= HandleWindowEvent(event
);
2506 switch ( nmhdr
->code
)
2508 case LVN_DELETEALLITEMS
:
2509 // always return true to suppress all additional LVN_DELETEITEM
2510 // notifications - this makes deleting all items from a list ctrl
2514 // also, we may free all user data now (couldn't do it before as
2515 // the user should have access to it in OnDeleteAllItems() handler)
2516 FreeAllInternalData();
2518 // the control is empty now, synchronize the cached number of items
2519 // with the real one
2523 case LVN_DELETEITEM
:
2524 // Delete the associated internal data. Notice that this can be
2525 // done only after the event has been handled as the data could be
2526 // accessed during the handling of the event.
2527 if ( wxMSWListItemData
*data
= MSWGetItemData(event
.m_itemIndex
) )
2529 const unsigned count
= m_internalData
.size();
2530 for ( unsigned n
= 0; n
< count
; n
++ )
2532 if ( m_internalData
[n
] == data
)
2534 m_internalData
.erase(m_internalData
.begin() + n
);
2540 wxASSERT_MSG( !data
, "invalid internal data pointer?" );
2544 case LVN_ENDLABELEDITA
:
2545 case LVN_ENDLABELEDITW
:
2546 // logic here is inverted compared to all the other messages
2547 *result
= event
.IsAllowed();
2549 // EDIT control will be deleted by the list control itself so
2550 // prevent us from deleting it as well
2551 DeleteEditControl();
2557 *result
= !event
.IsAllowed();
2562 // ----------------------------------------------------------------------------
2563 // custom draw stuff
2564 // ----------------------------------------------------------------------------
2566 // see comment at the end of wxListCtrl::GetColumn()
2567 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2569 static RECT
GetCustomDrawnItemRect(const NMCUSTOMDRAW
& nmcd
)
2572 wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_BOUNDS
, rc
);
2575 wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_ICON
, rcIcon
);
2577 // exclude the icon part, neither the selection background nor focus rect
2579 rc
.left
= rcIcon
.right
;
2585 bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
, int colCount
)
2587 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2590 HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2591 const int col
= pLVCD
->iSubItem
;
2592 const DWORD item
= nmcd
.dwItemSpec
;
2594 // the font must be valid, otherwise we wouldn't be painting the item at all
2595 SelectInHDC
selFont(hdc
, hfont
);
2597 // get the rectangle to paint
2598 int subitem
= colCount
? col
+ 1 : col
;
2600 wxGetListCtrlSubItemRect(hwndList
, item
, subitem
, LVIR_BOUNDS
, rc
);
2603 // get the image and text to draw
2607 it
.mask
= LVIF_TEXT
| LVIF_IMAGE
;
2611 it
.cchTextMax
= WXSIZEOF(text
);
2612 ListView_GetItem(hwndList
, &it
);
2614 HIMAGELIST himl
= ListView_GetImageList(hwndList
, LVSIL_SMALL
);
2615 if ( himl
&& ImageList_GetImageCount(himl
) )
2617 if ( it
.iImage
!= -1 )
2619 ImageList_Draw(himl
, it
.iImage
, hdc
, rc
.left
, rc
.top
,
2620 nmcd
.uItemState
& CDIS_SELECTED
? ILD_SELECTED
2624 // notice that even if this item doesn't have any image, the list
2625 // control still leaves space for the image in the first column if the
2626 // image list is not empty (presumably so that items with and without
2628 if ( it
.iImage
!= -1 || it
.iSubItem
== 0 )
2631 ImageList_GetIconSize(himl
, &wImage
, &hImage
);
2633 rc
.left
+= wImage
+ 2;
2637 ::SetBkMode(hdc
, TRANSPARENT
);
2639 UINT fmt
= DT_SINGLELINE
|
2642 #endif // __WXWINCE__
2647 wxZeroMemory(lvCol
);
2648 lvCol
.mask
= LVCF_FMT
;
2649 if ( ListView_GetColumn(hwndList
, col
, &lvCol
) )
2651 switch ( lvCol
.fmt
& LVCFMT_JUSTIFYMASK
)
2666 //else: failed to get alignment, assume it's DT_LEFT (default)
2668 DrawText(hdc
, text
, -1, &rc
, fmt
);
2673 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd
)
2675 if ( nmcd
.uItemState
& CDIS_FOCUS
)
2677 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2679 // don't use the provided HDC, it's in some strange state by now
2680 ::DrawFocusRect(WindowHDC(nmcd
.hdr
.hwndFrom
), &rc
);
2684 // pLVCD->clrText and clrTextBk should contain the colours to use
2685 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
)
2687 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
; // just a shortcut
2689 const HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2690 const int item
= nmcd
.dwItemSpec
;
2692 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2693 // the item is not at all selected for some reason (comctl32 6), but we
2694 // also can't always trust ListView_GetItem() as it could return the old
2695 // item status if we're called just after the (de)selection, so remember
2696 // the last item to gain selection and also check for it here
2697 for ( int i
= -1;; )
2699 i
= ListView_GetNextItem(hwndList
, i
, LVNI_SELECTED
);
2702 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2708 nmcd
.uItemState
|= CDIS_SELECTED
;
2713 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2715 // NB: cast is needed to work around the bug in mingw32 headers which don't
2716 // have it inside ListView_GetNextItem() itself (unlike SDK ones)
2717 if ( ::GetFocus() == hwndList
&&
2718 ListView_GetNextItem(
2719 hwndList
, static_cast<WPARAM
>(-1), LVNI_FOCUSED
) == item
)
2721 nmcd
.uItemState
|= CDIS_FOCUS
;
2725 nmcd
.uItemState
&= ~CDIS_FOCUS
;
2728 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2730 int syscolFg
, syscolBg
;
2731 if ( ::GetFocus() == hwndList
)
2733 syscolFg
= COLOR_HIGHLIGHTTEXT
;
2734 syscolBg
= COLOR_HIGHLIGHT
;
2736 else // selected but unfocused
2738 syscolFg
= COLOR_WINDOWTEXT
;
2739 syscolBg
= COLOR_BTNFACE
;
2741 // don't grey out the icon in this case neither
2742 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2745 pLVCD
->clrText
= ::GetSysColor(syscolFg
);
2746 pLVCD
->clrTextBk
= ::GetSysColor(syscolBg
);
2748 //else: not selected, use normal colours from pLVCD
2751 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2753 ::SetTextColor(hdc
, pLVCD
->clrText
);
2754 ::FillRect(hdc
, &rc
, AutoHBRUSH(pLVCD
->clrTextBk
));
2756 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2757 // problems so just draw everything except the focus rect from here instead
2758 const int colCount
= Header_GetItemCount(ListView_GetHeader(hwndList
));
2759 for ( int col
= 0; col
< colCount
; col
++ )
2761 pLVCD
->iSubItem
= col
;
2762 HandleSubItemPrepaint(pLVCD
, hfont
, colCount
);
2765 HandleItemPostpaint(nmcd
);
2768 static WXLPARAM
HandleItemPrepaint(wxListCtrl
*listctrl
,
2769 LPNMLVCUSTOMDRAW pLVCD
,
2770 wxListItemAttr
*attr
)
2774 // nothing to do for this item
2775 return CDRF_DODEFAULT
;
2779 // set the colours to use for text drawing
2780 pLVCD
->clrText
= attr
->HasTextColour()
2781 ? wxColourToRGB(attr
->GetTextColour())
2782 : wxColourToRGB(listctrl
->GetTextColour());
2783 pLVCD
->clrTextBk
= attr
->HasBackgroundColour()
2784 ? wxColourToRGB(attr
->GetBackgroundColour())
2785 : wxColourToRGB(listctrl
->GetBackgroundColour());
2787 // select the font if non default one is specified
2788 if ( attr
->HasFont() )
2790 wxFont font
= attr
->GetFont();
2791 if ( font
.GetEncoding() != wxFONTENCODING_SYSTEM
)
2793 // the standard control ignores the font encoding/charset, at least
2794 // with recent comctl32.dll versions (5 and 6, it uses to work with
2795 // 4.something) so we have to draw the item entirely ourselves in
2797 HandleItemPaint(pLVCD
, GetHfontOf(font
));
2798 return CDRF_SKIPDEFAULT
;
2801 ::SelectObject(pLVCD
->nmcd
.hdc
, GetHfontOf(font
));
2803 return CDRF_NEWFONT
;
2806 return CDRF_DODEFAULT
;
2809 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2811 LPNMLVCUSTOMDRAW pLVCD
= (LPNMLVCUSTOMDRAW
)lParam
;
2812 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2813 switch ( nmcd
.dwDrawStage
)
2816 // if we've got any items with non standard attributes,
2817 // notify us before painting each item
2819 // for virtual controls, always suppose that we have attributes as
2820 // there is no way to check for this
2821 if ( IsVirtual() || m_hasAnyAttr
)
2822 return CDRF_NOTIFYITEMDRAW
;
2825 case CDDS_ITEMPREPAINT
:
2826 // get a message for each subitem
2827 return CDRF_NOTIFYITEMDRAW
;
2829 case CDDS_SUBITEM
| CDDS_ITEMPREPAINT
:
2830 const int item
= nmcd
.dwItemSpec
;
2831 const int column
= pLVCD
->iSubItem
;
2833 // we get this message with item == 0 for an empty control, we
2834 // must ignore it as calling OnGetItemAttr() would be wrong
2835 if ( item
< 0 || item
>= GetItemCount() )
2838 if ( column
< 0 || column
>= GetColumnCount() )
2841 return HandleItemPrepaint(this, pLVCD
, DoGetItemColumnAttr(item
, column
));
2844 return CDRF_DODEFAULT
;
2847 #endif // NM_CUSTOMDRAW supported
2849 // Necessary for drawing hrules and vrules, if specified
2850 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2852 const int itemCount
= GetItemCount();
2853 const bool drawHRules
= HasFlag(wxLC_HRULES
);
2854 const bool drawVRules
= HasFlag(wxLC_VRULES
);
2856 if (!InReportView() || !(drawHRules
|| drawVRules
) || !itemCount
)
2864 wxControl::OnPaint(event
);
2866 // Reset the device origin since it may have been set
2867 dc
.SetDeviceOrigin(0, 0);
2869 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
));
2871 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2873 wxSize clientSize
= GetClientSize();
2878 const long top
= GetTopItem();
2879 for ( int i
= top
; i
< top
+ GetCountPerPage() + 1; i
++ )
2881 if (GetItemRect(i
, itemRect
))
2883 int cy
= itemRect
.GetTop();
2884 if (i
!= 0) // Don't draw the first one
2886 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2889 if (i
== itemCount
- 1)
2891 cy
= itemRect
.GetBottom();
2892 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2901 wxRect firstItemRect
;
2902 GetItemRect(0, firstItemRect
);
2904 if (GetItemRect(itemCount
- 1, itemRect
))
2906 // this is a fix for bug 673394: erase the pixels which we would
2907 // otherwise leave on the screen
2908 static const int gap
= 2;
2909 dc
.SetPen(*wxTRANSPARENT_PEN
);
2910 dc
.SetBrush(wxBrush(GetBackgroundColour()));
2911 dc
.DrawRectangle(0, firstItemRect
.GetY() - gap
,
2912 clientSize
.GetWidth(), gap
);
2915 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2917 const int numCols
= GetColumnCount();
2918 wxVector
<int> indexArray(numCols
);
2919 if ( !ListView_GetColumnOrderArray(GetHwnd(),
2923 wxFAIL_MSG( wxT("invalid column index array in OnPaint()") );
2927 int x
= itemRect
.GetX();
2928 for (int col
= 0; col
< numCols
; col
++)
2930 int colWidth
= GetColumnWidth(indexArray
[col
]);
2932 dc
.DrawLine(x
-1, firstItemRect
.GetY() - gap
,
2933 x
-1, itemRect
.GetBottom());
2940 wxListCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2946 // we should bypass our own WM_PRINT handling as we don't handle
2947 // PRF_CHILDREN flag, so leave it to the native control itself
2948 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
2951 case WM_CONTEXTMENU
:
2952 // because this message is propagated upwards the child-parent
2953 // chain, we get it for the right clicks on the header window but
2954 // this is confusing in wx as right clicking there already
2955 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2956 // so just ignore them
2957 if ( (HWND
)wParam
== ListView_GetHeader(GetHwnd()) )
2962 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2965 // ----------------------------------------------------------------------------
2966 // virtual list controls
2967 // ----------------------------------------------------------------------------
2969 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2971 // this is a pure virtual function, in fact - which is not really pure
2972 // because the controls which are not virtual don't need to implement it
2973 wxFAIL_MSG( wxT("wxListCtrl::OnGetItemText not supposed to be called") );
2975 return wxEmptyString
;
2978 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2980 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
2982 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2986 int wxListCtrl::OnGetItemColumnImage(long item
, long column
) const
2989 return OnGetItemImage(item
);
2994 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2996 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2997 wxT("invalid item index in OnGetItemAttr()") );
2999 // no attributes by default
3003 wxListItemAttr
*wxListCtrl::DoGetItemColumnAttr(long item
, long column
) const
3006 return OnGetItemColumnAttr(item
, column
);
3008 wxMSWListItemData
* const data
= MSWGetItemData(item
);
3009 return data
? data
->attr
: NULL
;
3012 void wxListCtrl::SetItemCount(long count
)
3014 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
3016 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
3017 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
3019 wxLogLastError(wxT("ListView_SetItemCount"));
3022 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
3023 wxT("m_count should match ListView_GetItemCount"));
3026 void wxListCtrl::RefreshItem(long item
)
3028 RefreshItems(item
, item
);
3031 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
3033 ListView_RedrawItems(GetHwnd(), itemFrom
, itemTo
);
3036 // ----------------------------------------------------------------------------
3037 // wxWin <-> MSW items conversions
3038 // ----------------------------------------------------------------------------
3040 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
3044 wxMSWListItemData
*internaldata
=
3045 (wxMSWListItemData
*) lvItem
.lParam
;
3048 info
.m_data
= internaldata
->lParam
;
3052 info
.m_stateMask
= 0;
3053 info
.m_itemId
= lvItem
.iItem
;
3055 long oldMask
= lvItem
.mask
;
3057 bool needText
= false;
3058 if (hwndListCtrl
!= 0)
3060 if ( lvItem
.mask
& LVIF_TEXT
)
3067 lvItem
.pszText
= new wxChar
[513];
3068 lvItem
.cchTextMax
= 512;
3070 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
3071 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
3074 if ( lvItem
.mask
& LVIF_STATE
)
3076 info
.m_mask
|= wxLIST_MASK_STATE
;
3078 if ( lvItem
.stateMask
& LVIS_CUT
)
3080 info
.m_stateMask
|= wxLIST_STATE_CUT
;
3081 if ( lvItem
.state
& LVIS_CUT
)
3082 info
.m_state
|= wxLIST_STATE_CUT
;
3084 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
3086 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
3087 if ( lvItem
.state
& LVIS_DROPHILITED
)
3088 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
3090 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
3092 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
3093 if ( lvItem
.state
& LVIS_FOCUSED
)
3094 info
.m_state
|= wxLIST_STATE_FOCUSED
;
3096 if ( lvItem
.stateMask
& LVIS_SELECTED
)
3098 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
3099 if ( lvItem
.state
& LVIS_SELECTED
)
3100 info
.m_state
|= wxLIST_STATE_SELECTED
;
3104 if ( lvItem
.mask
& LVIF_TEXT
)
3106 info
.m_mask
|= wxLIST_MASK_TEXT
;
3107 info
.m_text
= lvItem
.pszText
;
3109 if ( lvItem
.mask
& LVIF_IMAGE
)
3111 info
.m_mask
|= wxLIST_MASK_IMAGE
;
3112 info
.m_image
= lvItem
.iImage
;
3114 if ( lvItem
.mask
& LVIF_PARAM
)
3115 info
.m_mask
|= wxLIST_MASK_DATA
;
3116 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
3117 info
.m_mask
|= wxLIST_SET_ITEM
;
3118 info
.m_col
= lvItem
.iSubItem
;
3123 delete[] lvItem
.pszText
;
3125 lvItem
.mask
= oldMask
;
3128 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
3130 if (stateMask
& wxLIST_STATE_CUT
)
3132 lvItem
.stateMask
|= LVIS_CUT
;
3133 if (state
& wxLIST_STATE_CUT
)
3134 lvItem
.state
|= LVIS_CUT
;
3136 if (stateMask
& wxLIST_STATE_DROPHILITED
)
3138 lvItem
.stateMask
|= LVIS_DROPHILITED
;
3139 if (state
& wxLIST_STATE_DROPHILITED
)
3140 lvItem
.state
|= LVIS_DROPHILITED
;
3142 if (stateMask
& wxLIST_STATE_FOCUSED
)
3144 lvItem
.stateMask
|= LVIS_FOCUSED
;
3145 if (state
& wxLIST_STATE_FOCUSED
)
3146 lvItem
.state
|= LVIS_FOCUSED
;
3148 if (stateMask
& wxLIST_STATE_SELECTED
)
3150 lvItem
.stateMask
|= LVIS_SELECTED
;
3151 if (state
& wxLIST_STATE_SELECTED
)
3152 lvItem
.state
|= LVIS_SELECTED
;
3156 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
3157 const wxListItem
& info
,
3160 if ( ctrl
->InReportView() )
3162 wxASSERT_MSG( 0 <= info
.m_col
&& info
.m_col
< ctrl
->GetColumnCount(),
3163 "wxListCtrl column index out of bounds" );
3165 else // not in report view
3167 wxASSERT_MSG( info
.m_col
== 0, "columns only exist in report view" );
3170 lvItem
.iItem
= (int) info
.m_itemId
;
3172 lvItem
.iImage
= info
.m_image
;
3173 lvItem
.stateMask
= 0;
3176 lvItem
.iSubItem
= info
.m_col
;
3178 if (info
.m_mask
& wxLIST_MASK_STATE
)
3180 lvItem
.mask
|= LVIF_STATE
;
3182 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
3185 if (info
.m_mask
& wxLIST_MASK_TEXT
)
3187 lvItem
.mask
|= LVIF_TEXT
;
3188 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
3190 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
3194 // pszText is not const, hence the cast
3195 lvItem
.pszText
= (wxChar
*)info
.m_text
.wx_str();
3196 if ( lvItem
.pszText
)
3197 lvItem
.cchTextMax
= info
.m_text
.length();
3199 lvItem
.cchTextMax
= 0;
3202 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
3203 lvItem
.mask
|= LVIF_IMAGE
;
3206 static void wxConvertToMSWListCol(HWND hwndList
,
3208 const wxListItem
& item
,
3211 wxZeroMemory(lvCol
);
3213 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
3215 lvCol
.mask
|= LVCF_TEXT
;
3216 lvCol
.pszText
= (wxChar
*)item
.m_text
.wx_str(); // cast is safe
3219 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
3221 lvCol
.mask
|= LVCF_FMT
;
3223 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
3224 lvCol
.fmt
= LVCFMT_LEFT
;
3225 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
3226 lvCol
.fmt
= LVCFMT_RIGHT
;
3227 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
3228 lvCol
.fmt
= LVCFMT_CENTER
;
3231 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
3233 lvCol
.mask
|= LVCF_WIDTH
;
3234 if ( item
.m_width
== wxLIST_AUTOSIZE
)
3235 lvCol
.cx
= LVSCW_AUTOSIZE
;
3236 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3237 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
3239 lvCol
.cx
= item
.m_width
;
3242 // see comment at the end of wxListCtrl::GetColumn()
3243 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3244 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
3246 if ( wxApp::GetComCtl32Version() >= 470 )
3248 lvCol
.mask
|= LVCF_IMAGE
;
3250 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3251 // seem to be generally nicer than on the left and the generic
3252 // version only draws them on the right (we don't have a flag to
3253 // specify the image location anyhow)
3255 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3256 // make any difference in my tests -- but maybe we should?
3257 if ( item
.m_image
!= -1 )
3259 // as we're going to overwrite the format field, get its
3260 // current value first -- unless we want to overwrite it anyhow
3261 if ( !(lvCol
.mask
& LVCF_FMT
) )
3264 wxZeroMemory(lvColOld
);
3265 lvColOld
.mask
= LVCF_FMT
;
3266 if ( ListView_GetColumn(hwndList
, col
, &lvColOld
) )
3268 lvCol
.fmt
= lvColOld
.fmt
;
3271 lvCol
.mask
|= LVCF_FMT
;
3274 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
3277 lvCol
.iImage
= item
.m_image
;
3279 //else: it doesn't support item images anyhow
3281 #endif // _WIN32_IE >= 0x0300
3284 #endif // wxUSE_LISTCTRL