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"
46 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
54 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
55 // it by its old name NM_FINDTIEM.
57 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
58 #define HAVE_NMLVFINDITEM 1
59 #elif defined(__DMC__) || defined(NM_FINDITEM)
60 #define HAVE_NMLVFINDITEM 1
61 #define NMLVFINDITEM NM_FINDITEM
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // convert our state and mask flags to LV_ITEM constants
69 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
71 // convert wxListItem to LV_ITEM
72 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
73 const wxListItem
& info
, LV_ITEM
& lvItem
);
75 // convert LV_ITEM to wxListItem
76 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
78 /* const */ LV_ITEM
& lvItem
);
80 // convert our wxListItem to LV_COLUMN
81 static void wxConvertToMSWListCol(HWND hwndList
,
83 const wxListItem
& item
,
89 // replacement for ListView_GetSubItemRect() which provokes warnings like
90 // "the address of 'rc' will always evaluate as 'true'" when used with mingw32
93 // this function does no error checking on item and subitem parameters, notice
94 // that subitem 0 means the whole item so there is no way to retrieve the
95 // rectangle of the first subitem using this function, in particular notice
96 // that the index is *not* 1-based, in spite of what MSDN says
98 wxGetListCtrlSubItemRect(HWND hwnd
, int item
, int subitem
, int flags
, RECT
& rect
)
102 return ::SendMessage(hwnd
, LVM_GETSUBITEMRECT
, item
, (LPARAM
)&rect
) != 0;
106 wxGetListCtrlItemRect(HWND hwnd
, int item
, int flags
, RECT
& rect
)
108 return wxGetListCtrlSubItemRect(hwnd
, item
, 0, flags
, rect
);
111 } // anonymous namespace
113 // ----------------------------------------------------------------------------
114 // private helper classes
115 // ----------------------------------------------------------------------------
117 // We have to handle both fooW and fooA notifications in several cases
118 // because of broken comctl32.dll and/or unicows.dll. This class is used to
119 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
120 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
121 // by wxConvertToMSWListItem().
123 #define LV_ITEM_NATIVE LV_ITEMW
124 #define LV_ITEM_OTHER LV_ITEMA
126 #define LV_CONV_TO_WX cMB2WX
127 #define LV_CONV_BUF wxMB2WXbuf
129 #define LV_ITEM_NATIVE LV_ITEMA
130 #define LV_ITEM_OTHER LV_ITEMW
132 #define LV_CONV_TO_WX cWC2WX
133 #define LV_CONV_BUF wxWC2WXbuf
134 #endif // Unicode/ANSI
139 // default ctor, use Init() later
140 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
142 // init without conversion
143 void Init(LV_ITEM_NATIVE
& item
)
145 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
150 // init with conversion
151 void Init(const LV_ITEM_OTHER
& item
)
153 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
154 // point to our own m_item
156 // memcpy() can't work if the struct sizes are different
157 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
158 CodeCantWorkIfDiffSizes
);
160 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
162 // convert text from ANSI to Unicod if necessary
163 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
165 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
166 m_item
.pszText
= (wxChar
*)m_buf
->data();
170 // ctor without conversion
171 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
173 // ctor with conversion
174 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
179 ~wxLV_ITEM() { delete m_buf
; }
181 // conversion to the real LV_ITEM
182 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
187 LV_ITEM_NATIVE
*m_pItem
;
188 LV_ITEM_NATIVE m_item
;
190 wxDECLARE_NO_COPY_CLASS(wxLV_ITEM
);
193 ///////////////////////////////////////////////////////
195 // The MSW version had problems with SetTextColour() et
196 // al as the wxListItemAttr's were stored keyed on the
197 // item index. If a item was inserted anywhere but the end
198 // of the list the text attributes (colour etc) for
199 // the following items were out of sync.
202 // Under MSW the only way to associate data with a List
203 // item independent of its position in the list is to
204 // store a pointer to it in its lParam attribute. However
205 // user programs are already using this (via the
206 // SetItemData() GetItemData() calls).
208 // However what we can do is store a pointer to a
209 // structure which contains the attributes we want *and*
210 // a lParam -- and this is what wxMSWListItemData does.
212 // To conserve memory, a wxMSWListItemData is
213 // only allocated for a LV_ITEM if text attributes or
214 // user data(lparam) are being set.
215 class wxMSWListItemData
218 wxMSWListItemData() : attr(NULL
), lParam(0) {}
219 ~wxMSWListItemData() { delete attr
; }
221 wxListItemAttr
*attr
;
222 LPARAM lParam
; // real user data
224 wxDECLARE_NO_COPY_CLASS(wxMSWListItemData
);
227 // Get the internal data structure
228 static wxMSWListItemData
*wxGetInternalData(HWND hwnd
, long itemId
);
229 static wxMSWListItemData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
);
232 #if wxUSE_EXTENDED_RTTI
233 WX_DEFINE_FLAGS( wxListCtrlStyle
)
235 wxBEGIN_FLAGS( wxListCtrlStyle
)
236 // new style border flags, we put them first to
237 // use them for streaming out
238 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
239 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
240 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
241 wxFLAGS_MEMBER(wxBORDER_RAISED
)
242 wxFLAGS_MEMBER(wxBORDER_STATIC
)
243 wxFLAGS_MEMBER(wxBORDER_NONE
)
245 // old style border flags
246 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
247 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
248 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
249 wxFLAGS_MEMBER(wxRAISED_BORDER
)
250 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
251 wxFLAGS_MEMBER(wxBORDER
)
253 // standard window styles
254 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
255 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
256 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
257 wxFLAGS_MEMBER(wxWANTS_CHARS
)
258 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
259 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
260 wxFLAGS_MEMBER(wxVSCROLL
)
261 wxFLAGS_MEMBER(wxHSCROLL
)
263 wxFLAGS_MEMBER(wxLC_LIST
)
264 wxFLAGS_MEMBER(wxLC_REPORT
)
265 wxFLAGS_MEMBER(wxLC_ICON
)
266 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
267 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
268 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
269 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
270 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
271 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
272 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
273 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
274 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
275 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
276 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
278 wxEND_FLAGS( wxListCtrlStyle
)
280 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
282 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
283 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
285 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
286 wxEND_PROPERTIES_TABLE()
288 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
289 wxEND_HANDLERS_TABLE()
291 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
294 TODO : Expose more information of a list's layout etc. via appropriate objects (a la NotebookPageInfo)
297 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
300 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
301 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
303 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
305 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
306 EVT_PAINT(wxListCtrl::OnPaint
)
309 // ============================================================================
311 // ============================================================================
313 // ----------------------------------------------------------------------------
314 // wxListCtrl construction
315 // ----------------------------------------------------------------------------
317 void wxListCtrl::Init()
321 m_imageListState
= NULL
;
322 m_ownsImageListNormal
=
323 m_ownsImageListSmall
=
324 m_ownsImageListState
= false;
330 m_hasAnyAttr
= false;
333 bool wxListCtrl::Create(wxWindow
*parent
,
338 const wxValidator
& validator
,
339 const wxString
& name
)
341 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
344 if ( !MSWCreateControl(WC_LISTVIEW
, wxEmptyString
, pos
, size
) )
347 // explicitly say that we want to use Unicode because otherwise we get ANSI
348 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
349 wxSetCCUnicodeFormat(GetHwnd());
351 // We must set the default text colour to the system/theme color, otherwise
352 // GetTextColour will always return black
353 SetTextColour(GetDefaultAttributes().colFg
);
355 if ( InReportView() )
356 MSWSetExListStyles();
361 void wxListCtrl::MSWSetExListStyles()
363 // for comctl32.dll v 4.70+ we want to have some non default extended
364 // styles because it's prettier (and also because wxGTK does it like this)
365 if ( wxApp::GetComCtl32Version() >= 470 )
369 GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
, 0,
370 // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's
371 // not defined in the SDK headers
372 #ifdef LVS_EX_LABELTIP
375 LVS_EX_FULLROWSELECT
|
376 LVS_EX_SUBITEMIMAGES
|
377 // normally this should be governed by a style as it's probably not
378 // always appropriate, but we don't have any free styles left and
379 // it seems better to enable it by default than disable
380 LVS_EX_HEADERDRAGDROP
385 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
387 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
389 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
394 #define MAP_MODE_STYLE(wx, ms) \
395 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
396 #else // !wxDEBUG_LEVEL
397 #define MAP_MODE_STYLE(wx, ms) \
398 if ( style & (wx) ) wstyle |= (ms);
399 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
401 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
402 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
403 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
404 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
406 wxASSERT_MSG( nModes
== 1,
407 _T("wxListCtrl style should have exactly one mode bit set") );
409 #undef MAP_MODE_STYLE
411 if ( style
& wxLC_ALIGN_LEFT
)
412 wstyle
|= LVS_ALIGNLEFT
;
414 if ( style
& wxLC_ALIGN_TOP
)
415 wstyle
|= LVS_ALIGNTOP
;
417 if ( style
& wxLC_AUTOARRANGE
)
418 wstyle
|= LVS_AUTOARRANGE
;
420 if ( style
& wxLC_NO_SORT_HEADER
)
421 wstyle
|= LVS_NOSORTHEADER
;
423 if ( style
& wxLC_NO_HEADER
)
424 wstyle
|= LVS_NOCOLUMNHEADER
;
426 if ( style
& wxLC_EDIT_LABELS
)
427 wstyle
|= LVS_EDITLABELS
;
429 if ( style
& wxLC_SINGLE_SEL
)
430 wstyle
|= LVS_SINGLESEL
;
432 if ( style
& wxLC_SORT_ASCENDING
)
434 wstyle
|= LVS_SORTASCENDING
;
436 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
437 _T("can't sort in ascending and descending orders at once") );
439 else if ( style
& wxLC_SORT_DESCENDING
)
440 wstyle
|= LVS_SORTDESCENDING
;
442 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
443 if ( style
& wxLC_VIRTUAL
)
445 int ver
= wxApp::GetComCtl32Version();
448 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."),
449 ver
/ 100, ver
% 100);
452 wstyle
|= LVS_OWNERDATA
;
454 #endif // ancient cygwin
459 void wxListCtrl::UpdateStyle()
463 // The new window view style
464 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
466 // some styles are not returned by MSWGetStyle()
468 dwStyleNew
|= WS_VISIBLE
;
470 // Get the current window style.
471 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
473 // we don't have wxVSCROLL style, but the list control may have it,
474 // don't change it then
475 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
477 // Only set the window style if the view bits have changed.
478 if ( dwStyleOld
!= dwStyleNew
)
480 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
482 // if we switched to the report view, set the extended styles for
484 if ( !(dwStyleOld
& LVS_REPORT
) && (dwStyleNew
& LVS_REPORT
) )
485 MSWSetExListStyles();
490 void wxListCtrl::FreeAllInternalData()
492 const unsigned count
= m_internalData
.size();
493 for ( unsigned n
= 0; n
< count
; n
++ )
494 delete m_internalData
[n
];
496 m_internalData
.clear();
499 void wxListCtrl::DeleteEditControl()
503 m_textCtrl
->UnsubclassWin();
504 m_textCtrl
->SetHWND(0);
510 wxListCtrl::~wxListCtrl()
512 FreeAllInternalData();
516 if (m_ownsImageListNormal
)
517 delete m_imageListNormal
;
518 if (m_ownsImageListSmall
)
519 delete m_imageListSmall
;
520 if (m_ownsImageListState
)
521 delete m_imageListState
;
524 // ----------------------------------------------------------------------------
525 // set/get/change style
526 // ----------------------------------------------------------------------------
528 // Add or remove a single window style
529 void wxListCtrl::SetSingleStyle(long style
, bool add
)
531 long flag
= GetWindowStyleFlag();
533 // Get rid of conflicting styles
536 if ( style
& wxLC_MASK_TYPE
)
537 flag
= flag
& ~wxLC_MASK_TYPE
;
538 if ( style
& wxLC_MASK_ALIGN
)
539 flag
= flag
& ~wxLC_MASK_ALIGN
;
540 if ( style
& wxLC_MASK_SORT
)
541 flag
= flag
& ~wxLC_MASK_SORT
;
549 SetWindowStyleFlag(flag
);
552 // Set the whole window style
553 void wxListCtrl::SetWindowStyleFlag(long flag
)
555 if ( flag
!= m_windowStyle
)
557 wxControl::SetWindowStyleFlag(flag
);
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 /* static */ wxVisualAttributes
570 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
572 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
574 // common controls have their own default font
575 attrs
.font
= wxGetCCDefaultFont();
580 // Sets the foreground, i.e. text, colour
581 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
583 if ( !wxWindow::SetForegroundColour(col
) )
586 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
591 // Sets the background colour
592 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
594 if ( !wxWindow::SetBackgroundColour(col
) )
597 // we set the same colour for both the "empty" background and the items
599 COLORREF color
= wxColourToRGB(col
);
600 ListView_SetBkColor(GetHwnd(), color
);
601 ListView_SetTextBkColor(GetHwnd(), color
);
606 // Gets information about this column
607 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
612 lvCol
.mask
= LVCF_WIDTH
;
614 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
616 lvCol
.mask
|= LVCF_TEXT
;
617 lvCol
.pszText
= new wxChar
[513];
618 lvCol
.cchTextMax
= 512;
621 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
623 lvCol
.mask
|= LVCF_FMT
;
626 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
628 lvCol
.mask
|= LVCF_IMAGE
;
631 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
633 // item.m_subItem = lvCol.iSubItem;
634 item
.m_width
= lvCol
.cx
;
636 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
638 item
.m_text
= lvCol
.pszText
;
639 delete[] lvCol
.pszText
;
642 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
644 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
646 item
.m_format
= wxLIST_FORMAT_LEFT
;
649 item
.m_format
= wxLIST_FORMAT_RIGHT
;
652 item
.m_format
= wxLIST_FORMAT_CENTRE
;
655 item
.m_format
= -1; // Unknown?
660 // the column images were not supported in older versions but how to check
661 // for this? we can't use _WIN32_IE because we always define it to a very
662 // high value, so see if another symbol which is only defined starting from
663 // comctl32.dll 4.70 is available
664 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
665 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
667 item
.m_image
= lvCol
.iImage
;
669 #endif // LVCOLUMN::iImage exists
674 // Sets information about this column
675 bool wxListCtrl::SetColumn(int col
, const wxListItem
& item
)
678 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
680 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
683 // Gets the column width
684 int wxListCtrl::GetColumnWidth(int col
) const
686 return ListView_GetColumnWidth(GetHwnd(), col
);
689 // Sets the column width
690 bool wxListCtrl::SetColumnWidth(int col
, int width
)
692 if ( m_windowStyle
& wxLC_LIST
)
695 if ( width
== wxLIST_AUTOSIZE
)
696 width
= LVSCW_AUTOSIZE
;
697 else if ( width
== wxLIST_AUTOSIZE_USEHEADER
)
698 width
= LVSCW_AUTOSIZE_USEHEADER
;
700 return ListView_SetColumnWidth(GetHwnd(), col
, width
) != 0;
703 // ----------------------------------------------------------------------------
705 // ----------------------------------------------------------------------------
707 int wxListCtrl::GetColumnIndexFromOrder(int order
) const
709 const int numCols
= GetColumnCount();
710 wxCHECK_MSG( order
>= 0 && order
< numCols
, -1,
711 _T("Column position out of bounds") );
713 wxArrayInt
indexArray(numCols
);
714 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) )
717 return indexArray
[order
];
720 int wxListCtrl::GetColumnOrder(int col
) const
722 const int numCols
= GetColumnCount();
723 wxASSERT_MSG( col
>= 0 && col
< numCols
, _T("Column index out of bounds") );
725 wxArrayInt
indexArray(numCols
);
726 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &indexArray
[0]) )
729 for ( int pos
= 0; pos
< numCols
; pos
++ )
731 if ( indexArray
[pos
] == col
)
735 wxFAIL_MSG( _T("no column with with given order?") );
740 // Gets the column order for all columns
741 wxArrayInt
wxListCtrl::GetColumnsOrder() const
743 const int numCols
= GetColumnCount();
745 wxArrayInt
orders(numCols
);
746 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) )
752 // Sets the column order for all columns
753 bool wxListCtrl::SetColumnsOrder(const wxArrayInt
& orders
)
755 const int numCols
= GetColumnCount();
757 wxCHECK_MSG( orders
.size() == (size_t)numCols
, false,
758 _T("wrong number of elements in column orders array") );
760 return ListView_SetColumnOrderArray(GetHwnd(), numCols
, &orders
[0]) != 0;
764 // Gets the number of items that can fit vertically in the
765 // visible area of the list control (list or report view)
766 // or the total number of items in the list control (icon
767 // or small icon view)
768 int wxListCtrl::GetCountPerPage() const
770 return ListView_GetCountPerPage(GetHwnd());
773 // Gets the edit control for editing labels.
774 wxTextCtrl
* wxListCtrl::GetEditControl() const
776 // first check corresponds to the case when the label editing was started
777 // by user and hence m_textCtrl wasn't created by EditLabel() at all, while
778 // the second case corresponds to us being called from inside EditLabel()
779 // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this
780 // case EditLabel() did create the control but it didn't have an HWND to
781 // initialize it with yet
782 if ( !m_textCtrl
|| !m_textCtrl
->GetHWND() )
784 HWND hwndEdit
= ListView_GetEditControl(GetHwnd());
787 wxListCtrl
* const self
= const_cast<wxListCtrl
*>(this);
790 self
->m_textCtrl
= new wxTextCtrl
;
791 self
->InitEditControl((WXHWND
)hwndEdit
);
798 // Gets information about the item
799 bool wxListCtrl::GetItem(wxListItem
& info
) const
802 wxZeroMemory(lvItem
);
804 lvItem
.iItem
= info
.m_itemId
;
805 lvItem
.iSubItem
= info
.m_col
;
807 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
809 lvItem
.mask
|= LVIF_TEXT
;
810 lvItem
.pszText
= new wxChar
[513];
811 lvItem
.cchTextMax
= 512;
815 lvItem
.pszText
= NULL
;
818 if (info
.m_mask
& wxLIST_MASK_DATA
)
819 lvItem
.mask
|= LVIF_PARAM
;
821 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
822 lvItem
.mask
|= LVIF_IMAGE
;
824 if ( info
.m_mask
& wxLIST_MASK_STATE
)
826 lvItem
.mask
|= LVIF_STATE
;
827 wxConvertToMSWFlags(0, info
.m_stateMask
, lvItem
);
830 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
833 wxLogError(_("Couldn't retrieve information about list control item %d."),
838 // give NULL as hwnd as we already have everything we need
839 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
843 delete[] lvItem
.pszText
;
848 // Sets information about the item
849 bool wxListCtrl::SetItem(wxListItem
& info
)
851 const long id
= info
.GetId();
852 wxCHECK_MSG( id
>= 0 && id
< GetItemCount(), false,
853 _T("invalid item index in SetItem") );
856 wxConvertToMSWListItem(this, info
, item
);
858 // we never update the lParam if it contains our pointer
859 // to the wxMSWListItemData structure
860 item
.mask
&= ~LVIF_PARAM
;
862 // check if setting attributes or lParam
863 if ( info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
) )
865 // get internal item data
866 wxMSWListItemData
*data
= MSWGetItemData(id
);
870 // need to allocate the internal data object
871 data
= new wxMSWListItemData
;
872 m_internalData
.push_back(data
);
873 item
.lParam
= (LPARAM
) data
;
874 item
.mask
|= LVIF_PARAM
;
879 if ( info
.m_mask
& wxLIST_MASK_DATA
)
880 data
->lParam
= info
.m_data
;
883 if ( info
.HasAttributes() )
885 const wxListItemAttr
& attrNew
= *info
.GetAttributes();
887 // don't overwrite the already set attributes if we have them
889 data
->attr
->AssignFrom(attrNew
);
891 data
->attr
= new wxListItemAttr(attrNew
);
896 // we could be changing only the attribute in which case we don't need to
897 // call ListView_SetItem() at all
900 if ( !ListView_SetItem(GetHwnd(), &item
) )
902 wxLogDebug(_T("ListView_SetItem() failed"));
908 // we need to update the item immediately to show the new image
909 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
911 // check whether it has any custom attributes
912 if ( info
.HasAttributes() )
916 // if the colour has changed, we must redraw the item
922 // we need this to make the change visible right now
923 RefreshItem(item
.iItem
);
929 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
933 info
.m_mask
= wxLIST_MASK_TEXT
;
934 info
.m_itemId
= index
;
938 info
.m_image
= imageId
;
939 info
.m_mask
|= wxLIST_MASK_IMAGE
;
941 return SetItem(info
);
945 // Gets the item state
946 int wxListCtrl::GetItemState(long item
, long stateMask
) const
950 info
.m_mask
= wxLIST_MASK_STATE
;
951 info
.m_stateMask
= stateMask
;
952 info
.m_itemId
= item
;
960 // Sets the item state
961 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
963 // NB: don't use SetItem() here as it doesn't work with the virtual list
966 wxZeroMemory(lvItem
);
968 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
970 const bool changingFocus
= (stateMask
& wxLIST_STATE_FOCUSED
) &&
971 (state
& wxLIST_STATE_FOCUSED
);
973 // for the virtual list controls we need to refresh the previously focused
974 // item manually when changing focus without changing selection
975 // programmatically because otherwise it keeps its focus rectangle until
976 // next repaint (yet another comctl32 bug)
978 if ( IsVirtual() && changingFocus
)
980 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
987 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
988 (WPARAM
)item
, (LPARAM
)&lvItem
) )
990 wxLogLastError(_T("ListView_SetItemState"));
995 if ( focusOld
!= -1 )
997 // no need to refresh the item if it was previously selected, it would
998 // only result in annoying flicker
999 if ( !(GetItemState(focusOld
,
1000 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
1002 RefreshItem(focusOld
);
1006 // we expect the selection anchor, i.e. the item from which multiple
1007 // selection (such as performed with e.g. Shift-arrows) starts, to be the
1008 // same as the currently focused item but the native control doesn't update
1009 // it when we change focus and leaves at the last item it set itself focus
1010 // to, so do it explicitly
1011 if ( changingFocus
&& !HasFlag(wxLC_SINGLE_SEL
) )
1013 ListView_SetSelectionMark(GetHwnd(), item
);
1019 // Sets the item image
1020 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
1022 return SetItemColumnImage(item
, 0, image
);
1025 // Sets the item image
1026 bool wxListCtrl::SetItemColumnImage(long item
, long column
, int image
)
1030 info
.m_mask
= wxLIST_MASK_IMAGE
;
1031 info
.m_image
= image
;
1032 info
.m_itemId
= item
;
1033 info
.m_col
= column
;
1035 return SetItem(info
);
1038 // Gets the item text
1039 wxString
wxListCtrl::GetItemText(long item
) const
1043 info
.m_mask
= wxLIST_MASK_TEXT
;
1044 info
.m_itemId
= item
;
1047 return wxEmptyString
;
1051 // Sets the item text
1052 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
1056 info
.m_mask
= wxLIST_MASK_TEXT
;
1057 info
.m_itemId
= item
;
1063 // Gets the internal item data
1064 wxMSWListItemData
*wxListCtrl::MSWGetItemData(long itemId
) const
1067 it
.mask
= LVIF_PARAM
;
1070 if ( !ListView_GetItem(GetHwnd(), &it
) )
1073 return (wxMSWListItemData
*) it
.lParam
;
1076 // Gets the item data
1077 wxUIntPtr
wxListCtrl::GetItemData(long item
) const
1081 info
.m_mask
= wxLIST_MASK_DATA
;
1082 info
.m_itemId
= item
;
1089 // Sets the item data
1090 bool wxListCtrl::SetItemPtrData(long item
, wxUIntPtr data
)
1094 info
.m_mask
= wxLIST_MASK_DATA
;
1095 info
.m_itemId
= item
;
1098 return SetItem(info
);
1101 wxRect
wxListCtrl::GetViewRect() const
1105 // ListView_GetViewRect() can only be used in icon and small icon views
1106 // (this is documented in MSDN and, indeed, it returns bogus results in
1107 // report view, at least with comctl32.dll v6 under Windows 2003)
1108 if ( HasFlag(wxLC_ICON
| wxLC_SMALL_ICON
) )
1111 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1113 wxLogDebug(_T("ListView_GetViewRect() failed."));
1118 wxCopyRECTToRect(rc
, rect
);
1120 else if ( HasFlag(wxLC_REPORT
) )
1122 const long count
= GetItemCount();
1125 GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count
- 1), rect
);
1127 // extend the rectangle to start at the top (we include the column
1128 // headers, if any, for compatibility with the generic version)
1129 rect
.height
+= rect
.y
;
1135 wxFAIL_MSG( _T("not implemented in this mode") );
1141 // Gets the item rectangle
1142 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1144 return GetSubItemRect( item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
) ;
1147 bool wxListCtrl::GetSubItemRect(long item
, long subItem
, wxRect
& rect
, int code
) const
1149 // ListView_GetSubItemRect() doesn't do subItem error checking and returns
1150 // true even for the out of range values of it (even if the results are
1151 // completely bogus in this case), so we check item validity ourselves
1152 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
||
1153 (subItem
>= 0 && subItem
< GetColumnCount()),
1154 false, _T("invalid sub item index") );
1156 // use wxCHECK_MSG against "item" too, for coherency with the generic implementation:
1157 wxCHECK_MSG( item
>= 0 && item
< GetItemCount(), false,
1158 _T("invalid item in GetSubItemRect") );
1161 if ( code
== wxLIST_RECT_BOUNDS
)
1162 codeWin
= LVIR_BOUNDS
;
1163 else if ( code
== wxLIST_RECT_ICON
)
1164 codeWin
= LVIR_ICON
;
1165 else if ( code
== wxLIST_RECT_LABEL
)
1166 codeWin
= LVIR_LABEL
;
1169 wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") );
1170 codeWin
= LVIR_BOUNDS
;
1174 if ( !wxGetListCtrlSubItemRect
1178 subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
? 0 : subItem
,
1186 wxCopyRECTToRect(rectWin
, rect
);
1188 // there is no way to retrieve the first sub item bounding rectangle using
1189 // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to
1190 // truncate it at first column ourselves
1192 rect
.width
= GetColumnWidth(0);
1200 // Gets the item position
1201 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1205 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1207 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1211 // Sets the item position.
1212 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1214 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1217 // Gets the number of items in the list control
1218 int wxListCtrl::GetItemCount() const
1223 wxSize
wxListCtrl::GetItemSpacing() const
1225 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1227 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1230 #if WXWIN_COMPATIBILITY_2_6
1232 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1234 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1237 #endif // WXWIN_COMPATIBILITY_2_6
1239 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1242 info
.m_itemId
= item
;
1243 info
.SetTextColour( col
);
1247 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1250 wxMSWListItemData
*data
= MSWGetItemData(item
);
1251 if ( data
&& data
->attr
)
1252 col
= data
->attr
->GetTextColour();
1257 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1260 info
.m_itemId
= item
;
1261 info
.SetBackgroundColour( col
);
1265 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1268 wxMSWListItemData
*data
= MSWGetItemData(item
);
1269 if ( data
&& data
->attr
)
1270 col
= data
->attr
->GetBackgroundColour();
1275 void wxListCtrl::SetItemFont( long item
, const wxFont
&f
)
1278 info
.m_itemId
= item
;
1283 wxFont
wxListCtrl::GetItemFont( long item
) const
1286 wxMSWListItemData
*data
= MSWGetItemData(item
);
1287 if ( data
&& data
->attr
)
1288 f
= data
->attr
->GetFont();
1293 // Gets the number of selected items in the list control
1294 int wxListCtrl::GetSelectedItemCount() const
1296 return ListView_GetSelectedCount(GetHwnd());
1299 // Gets the text colour of the listview
1300 wxColour
wxListCtrl::GetTextColour() const
1302 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1303 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1307 // Sets the text colour of the listview
1308 void wxListCtrl::SetTextColour(const wxColour
& col
)
1310 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1313 // Gets the index of the topmost visible item when in
1314 // list or report view
1315 long wxListCtrl::GetTopItem() const
1317 return (long) ListView_GetTopIndex(GetHwnd());
1320 // Searches for an item, starting from 'item'.
1321 // 'geometry' is one of
1322 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1323 // 'state' is a state bit flag, one or more of
1324 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1325 // item can be -1 to find the first item that matches the
1327 // Returns the item or -1 if unsuccessful.
1328 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1332 if ( geom
== wxLIST_NEXT_ABOVE
)
1333 flags
|= LVNI_ABOVE
;
1334 if ( geom
== wxLIST_NEXT_ALL
)
1336 if ( geom
== wxLIST_NEXT_BELOW
)
1337 flags
|= LVNI_BELOW
;
1338 if ( geom
== wxLIST_NEXT_LEFT
)
1339 flags
|= LVNI_TOLEFT
;
1340 if ( geom
== wxLIST_NEXT_RIGHT
)
1341 flags
|= LVNI_TORIGHT
;
1343 if ( state
& wxLIST_STATE_CUT
)
1345 if ( state
& wxLIST_STATE_DROPHILITED
)
1346 flags
|= LVNI_DROPHILITED
;
1347 if ( state
& wxLIST_STATE_FOCUSED
)
1348 flags
|= LVNI_FOCUSED
;
1349 if ( state
& wxLIST_STATE_SELECTED
)
1350 flags
|= LVNI_SELECTED
;
1352 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1356 wxImageList
*wxListCtrl::GetImageList(int which
) const
1358 if ( which
== wxIMAGE_LIST_NORMAL
)
1360 return m_imageListNormal
;
1362 else if ( which
== wxIMAGE_LIST_SMALL
)
1364 return m_imageListSmall
;
1366 else if ( which
== wxIMAGE_LIST_STATE
)
1368 return m_imageListState
;
1373 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1376 if ( which
== wxIMAGE_LIST_NORMAL
)
1378 flags
= LVSIL_NORMAL
;
1379 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1380 m_imageListNormal
= imageList
;
1381 m_ownsImageListNormal
= false;
1383 else if ( which
== wxIMAGE_LIST_SMALL
)
1385 flags
= LVSIL_SMALL
;
1386 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1387 m_imageListSmall
= imageList
;
1388 m_ownsImageListSmall
= false;
1390 else if ( which
== wxIMAGE_LIST_STATE
)
1392 flags
= LVSIL_STATE
;
1393 if (m_ownsImageListState
) delete m_imageListState
;
1394 m_imageListState
= imageList
;
1395 m_ownsImageListState
= false;
1397 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1400 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1402 SetImageList(imageList
, which
);
1403 if ( which
== wxIMAGE_LIST_NORMAL
)
1404 m_ownsImageListNormal
= true;
1405 else if ( which
== wxIMAGE_LIST_SMALL
)
1406 m_ownsImageListSmall
= true;
1407 else if ( which
== wxIMAGE_LIST_STATE
)
1408 m_ownsImageListState
= true;
1411 // ----------------------------------------------------------------------------
1413 // ----------------------------------------------------------------------------
1415 // Arranges the items
1416 bool wxListCtrl::Arrange(int flag
)
1419 if ( flag
== wxLIST_ALIGN_LEFT
)
1420 code
= LVA_ALIGNLEFT
;
1421 else if ( flag
== wxLIST_ALIGN_TOP
)
1422 code
= LVA_ALIGNTOP
;
1423 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1425 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1426 code
= LVA_SNAPTOGRID
;
1428 return (ListView_Arrange(GetHwnd(), code
) != 0);
1432 bool wxListCtrl::DeleteItem(long item
)
1434 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1436 wxLogLastError(_T("ListView_DeleteItem"));
1441 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1442 wxT("m_count should match ListView_GetItemCount"));
1444 // the virtual list control doesn't refresh itself correctly, help it
1447 // we need to refresh all the lines below the one which was deleted
1449 if ( item
> 0 && GetItemCount() )
1451 GetItemRect(item
- 1, rectItem
);
1456 rectItem
.height
= 0;
1459 wxRect rectWin
= GetRect();
1460 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1461 rectWin
.y
= rectItem
.GetBottom();
1463 RefreshRect(rectWin
);
1469 // Deletes all items
1470 bool wxListCtrl::DeleteAllItems()
1472 return ListView_DeleteAllItems(GetHwnd()) != 0;
1475 // Deletes all items
1476 bool wxListCtrl::DeleteAllColumns()
1478 while ( m_colCount
> 0 )
1480 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1482 wxLogLastError(wxT("ListView_DeleteColumn"));
1490 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1496 bool wxListCtrl::DeleteColumn(int col
)
1498 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1500 if ( success
&& (m_colCount
> 0) )
1505 // Clears items, and columns if there are any.
1506 void wxListCtrl::ClearAll()
1509 if ( m_colCount
> 0 )
1513 void wxListCtrl::InitEditControl(WXHWND hWnd
)
1515 m_textCtrl
->SetHWND(hWnd
);
1516 m_textCtrl
->SubclassWin(hWnd
);
1517 m_textCtrl
->SetParent(this);
1519 // we must disallow TABbing away from the control while the edit contol is
1520 // shown because this leaves it in some strange state (just try removing
1521 // this line and then pressing TAB while editing an item in listctrl
1523 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1526 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1528 wxCHECK_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)), NULL
,
1529 "control used for label editing must be a wxTextCtrl" );
1531 // ListView_EditLabel requires that the list has focus.
1534 // create m_textCtrl here before calling ListView_EditLabel() because it
1535 // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and
1536 // the user handler for it can call GetEditControl() resulting in an on
1537 // demand creation of a stock wxTextCtrl instead of the control of a
1538 // (possibly) custom wxClassInfo
1539 DeleteEditControl();
1540 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1542 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1545 // failed to start editing
1552 // if GetEditControl() hasn't been called, we need to initialize the edit
1553 // control ourselves
1554 if ( !m_textCtrl
->GetHWND() )
1555 InitEditControl(hWnd
);
1560 // End label editing, optionally cancelling the edit
1561 bool wxListCtrl::EndEditLabel(bool cancel
)
1563 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1564 HWND hwnd
= ListView_GetEditControl(GetHwnd());
1569 ::SetWindowText(hwnd
, wxEmptyString
); // dubious but better than nothing
1571 // we shouldn't destroy the control ourselves according to MSDN, which
1572 // proposes WM_CANCELMODE to do this, but it doesn't seem to work
1574 // posting WM_CLOSE to it does seem to work without any side effects
1575 ::PostMessage(hwnd
, WM_CLOSE
, 0, 0);
1580 // Ensures this item is visible
1581 bool wxListCtrl::EnsureVisible(long item
)
1583 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1586 // Find an item whose label matches this string, starting from the item after 'start'
1587 // or the beginning if 'start' is -1.
1588 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1590 LV_FINDINFO findInfo
;
1592 findInfo
.flags
= LVFI_STRING
;
1594 findInfo
.flags
|= LVFI_PARTIAL
;
1595 findInfo
.psz
= str
.wx_str();
1597 // ListView_FindItem() excludes the first item from search and to look
1598 // through all the items you need to start from -1 which is unnatural and
1599 // inconsistent with the generic version - so we adjust the index
1602 return ListView_FindItem(GetHwnd(), start
, &findInfo
);
1605 // Find an item whose data matches this data, starting from the item after
1606 // 'start' or the beginning if 'start' is -1.
1607 long wxListCtrl::FindItem(long start
, wxUIntPtr data
)
1609 // we can't use ListView_FindItem() directly as we don't store the data
1610 // pointer itself in the control but rather our own internal data, so first
1611 // we need to find the right value to search for (and there can be several
1613 int idx
= wxNOT_FOUND
;
1614 const unsigned count
= m_internalData
.size();
1615 for ( unsigned n
= 0; n
< count
; n
++ )
1617 if ( m_internalData
[n
]->lParam
== (LPARAM
)data
)
1619 LV_FINDINFO findInfo
;
1620 findInfo
.flags
= LVFI_PARAM
;
1621 findInfo
.lParam
= (LPARAM
)wxPtrToUInt(m_internalData
[n
]);
1623 int rc
= ListView_FindItem(GetHwnd(), start
, &findInfo
);
1626 if ( idx
== wxNOT_FOUND
|| rc
< idx
)
1629 if ( idx
== start
+ 1 )
1631 // we can stop here, we don't risk finding a closer
1636 //else: this item is after the previously found one
1644 // Find an item nearest this position in the specified direction, starting from
1645 // the item after 'start' or the beginning if 'start' is -1.
1646 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1648 LV_FINDINFO findInfo
;
1650 findInfo
.flags
= LVFI_NEARESTXY
;
1651 findInfo
.pt
.x
= pt
.x
;
1652 findInfo
.pt
.y
= pt
.y
;
1653 findInfo
.vkDirection
= VK_RIGHT
;
1655 if ( direction
== wxLIST_FIND_UP
)
1656 findInfo
.vkDirection
= VK_UP
;
1657 else if ( direction
== wxLIST_FIND_DOWN
)
1658 findInfo
.vkDirection
= VK_DOWN
;
1659 else if ( direction
== wxLIST_FIND_LEFT
)
1660 findInfo
.vkDirection
= VK_LEFT
;
1661 else if ( direction
== wxLIST_FIND_RIGHT
)
1662 findInfo
.vkDirection
= VK_RIGHT
;
1664 return ListView_FindItem(GetHwnd(), start
, &findInfo
);
1667 // Determines which item (if any) is at the specified point,
1668 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1670 wxListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *ptrSubItem
) const
1672 LV_HITTESTINFO hitTestInfo
;
1673 hitTestInfo
.pt
.x
= (int) point
.x
;
1674 hitTestInfo
.pt
.y
= (int) point
.y
;
1677 #ifdef LVM_SUBITEMHITTEST
1678 if ( ptrSubItem
&& wxApp::GetComCtl32Version() >= 470 )
1680 item
= ListView_SubItemHitTest(GetHwnd(), &hitTestInfo
);
1681 *ptrSubItem
= hitTestInfo
.iSubItem
;
1684 #endif // LVM_SUBITEMHITTEST
1686 item
= ListView_HitTest(GetHwnd(), &hitTestInfo
);
1691 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1692 flags
|= wxLIST_HITTEST_ABOVE
;
1693 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1694 flags
|= wxLIST_HITTEST_BELOW
;
1695 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1696 flags
|= wxLIST_HITTEST_TOLEFT
;
1697 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1698 flags
|= wxLIST_HITTEST_TORIGHT
;
1700 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1701 flags
|= wxLIST_HITTEST_NOWHERE
;
1703 // note a bug or at least a very strange feature of comtl32.dll (tested
1704 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1705 // the right of the item label, ListView_HitTest() returns a combination of
1706 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1707 // the bits which don't make sense
1708 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1710 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1712 // do not translate LVHT_ONITEMICON here, as per above
1716 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1717 flags
|= wxLIST_HITTEST_ONITEMICON
;
1718 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1719 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1726 // Inserts an item, returning the index of the new item if successful,
1728 long wxListCtrl::InsertItem(const wxListItem
& info
)
1730 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1733 wxConvertToMSWListItem(this, info
, item
);
1734 item
.mask
&= ~LVIF_PARAM
;
1736 // check whether we need to allocate our internal data
1737 bool needInternalData
= (info
.m_mask
& wxLIST_MASK_DATA
) ||
1738 info
.HasAttributes();
1739 if ( needInternalData
)
1741 item
.mask
|= LVIF_PARAM
;
1743 wxMSWListItemData
* const data
= new wxMSWListItemData
;
1744 m_internalData
.push_back(data
);
1745 item
.lParam
= (LPARAM
)data
;
1747 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1748 data
->lParam
= info
.m_data
;
1750 // check whether it has any custom attributes
1751 if ( info
.HasAttributes() )
1753 // take copy of attributes
1754 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1756 // and remember that we have some now...
1757 m_hasAnyAttr
= true;
1761 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1764 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1765 wxT("m_count should match ListView_GetItemCount"));
1770 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1773 info
.m_text
= label
;
1774 info
.m_mask
= wxLIST_MASK_TEXT
;
1775 info
.m_itemId
= index
;
1776 return InsertItem(info
);
1779 // Inserts an image item
1780 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1783 info
.m_image
= imageIndex
;
1784 info
.m_mask
= wxLIST_MASK_IMAGE
;
1785 info
.m_itemId
= index
;
1786 return InsertItem(info
);
1789 // Inserts an image/string item
1790 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1793 info
.m_image
= imageIndex
;
1794 info
.m_text
= label
;
1795 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1796 info
.m_itemId
= index
;
1797 return InsertItem(info
);
1800 // For list view mode (only), inserts a column.
1801 long wxListCtrl::InsertColumn(long col
, const wxListItem
& item
)
1804 wxConvertToMSWListCol(GetHwnd(), col
, item
, lvCol
);
1806 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1808 // always give some width to the new column: this one is compatible
1809 // with the generic version
1810 lvCol
.mask
|= LVCF_WIDTH
;
1814 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1819 else // failed to insert?
1821 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1828 long wxListCtrl::InsertColumn(long col
,
1829 const wxString
& heading
,
1834 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1835 item
.m_text
= heading
;
1838 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1839 item
.m_width
= width
;
1841 item
.m_format
= format
;
1843 return InsertColumn(col
, item
);
1846 // scroll the control by the given number of pixels (exception: in list view,
1847 // dx is interpreted as number of columns)
1848 bool wxListCtrl::ScrollList(int dx
, int dy
)
1850 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1852 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1862 // fn is a function which takes 3 long arguments: item1, item2, data.
1863 // item1 is the long data associated with a first item (NOT the index).
1864 // item2 is the long data associated with a second item (NOT the index).
1865 // data is the same value as passed to SortItems.
1866 // The return value is a negative number if the first item should precede the second
1867 // item, a positive number of the second item should precede the first,
1868 // or zero if the two items are equivalent.
1870 // data is arbitrary data to be passed to the sort function.
1872 // Internal structures for proxying the user compare function
1873 // so that we can pass it the *real* user data
1875 // translate lParam data and call user func
1876 struct wxInternalDataSort
1878 wxListCtrlCompare user_fn
;
1882 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1884 wxInternalDataSort
* const internalData
= (wxInternalDataSort
*) lParamSort
;
1886 wxMSWListItemData
*data1
= (wxMSWListItemData
*) lParam1
;
1887 wxMSWListItemData
*data2
= (wxMSWListItemData
*) lParam2
;
1889 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1890 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1892 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1896 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, wxIntPtr data
)
1898 wxInternalDataSort internalData
;
1899 internalData
.user_fn
= fn
;
1900 internalData
.data
= data
;
1902 // WPARAM cast is needed for mingw/cygwin
1903 if ( !ListView_SortItems(GetHwnd(),
1904 wxInternalDataCompareFunc
,
1905 (WPARAM
) &internalData
) )
1907 wxLogDebug(_T("ListView_SortItems() failed"));
1917 // ----------------------------------------------------------------------------
1918 // message processing
1919 // ----------------------------------------------------------------------------
1921 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1923 if ( msg
->message
== WM_KEYDOWN
)
1925 // Only eat VK_RETURN if not being used by the application in
1926 // conjunction with modifiers
1927 if ( msg
->wParam
== VK_RETURN
&& !wxIsAnyModifierDown() )
1929 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
1933 return wxControl::MSWShouldPreProcessMessage(msg
);
1936 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
1938 const int id
= (signed short)id_
;
1939 if (cmd
== EN_UPDATE
)
1941 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1942 event
.SetEventObject( this );
1943 ProcessCommand(event
);
1946 else if (cmd
== EN_KILLFOCUS
)
1948 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1949 event
.SetEventObject( this );
1950 ProcessCommand(event
);
1957 // utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify
1958 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
)
1960 // find the column clicked: we have to search for it ourselves as the
1961 // notification message doesn't provide this info
1963 // where did the click occur?
1964 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1965 if ( nmhdr
->code
== GN_CONTEXTMENU
)
1967 *ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1970 #endif //__WXWINCE__
1971 if ( !::GetCursorPos(ptClick
) )
1973 wxLogLastError(_T("GetCursorPos"));
1976 // we need to use listctrl coordinates for the event point so this is what
1977 // we return in ptClick, but for comparison with Header_GetItemRect()
1978 // result below we need to use header window coordinates
1979 POINT ptClickHeader
= *ptClick
;
1980 if ( !::ScreenToClient(nmhdr
->hwndFrom
, &ptClickHeader
) )
1982 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1985 if ( !::ScreenToClient(::GetParent(nmhdr
->hwndFrom
), ptClick
) )
1987 wxLogLastError(_T("ScreenToClient(listctrl)"));
1990 const int colCount
= Header_GetItemCount(nmhdr
->hwndFrom
);
1991 for ( int col
= 0; col
< colCount
; col
++ )
1994 if ( Header_GetItemRect(nmhdr
->hwndFrom
, col
, &rect
) )
1996 if ( ::PtInRect(&rect
, ptClickHeader
) )
2006 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2009 // prepare the event
2010 // -----------------
2012 wxListEvent
event(wxEVT_NULL
, m_windowId
);
2013 event
.SetEventObject(this);
2015 wxEventType eventType
= wxEVT_NULL
;
2017 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
2019 // if your compiler is as broken as this, you should really change it: this
2020 // code is needed for normal operation! #ifdef below is only useful for
2021 // automatic rebuilds which are done with a very old compiler version
2022 #ifdef HDN_BEGINTRACKA
2024 // check for messages from the header (in report view)
2025 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
2027 // is it a message from the header?
2028 if ( nmhdr
->hwndFrom
== hwndHdr
)
2030 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
2032 event
.m_itemIndex
= -1;
2034 switch ( nmhdr
->code
)
2036 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
2037 // TRACK messages even to ANSI programs: on my system I get
2038 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
2040 // work around is to simply catch both versions and hope that it
2041 // works (why should this message exist in ANSI and Unicode is
2042 // beyond me as it doesn't deal with strings at all...)
2044 // note that fr HDN_TRACK another possibility could be to use
2045 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
2046 // something other than the item width changes so we'd have to
2047 // filter out the unwanted events then
2048 case HDN_BEGINTRACKA
:
2049 case HDN_BEGINTRACKW
:
2050 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
2055 if ( eventType
== wxEVT_NULL
)
2056 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
2061 if ( eventType
== wxEVT_NULL
)
2062 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
2064 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
2065 event
.m_col
= nmHDR
->iItem
;
2068 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2069 case GN_CONTEXTMENU
:
2070 #endif //__WXWINCE__
2075 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
2076 event
.m_col
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
2077 event
.m_pointDrag
.x
= ptClick
.x
;
2078 event
.m_pointDrag
.y
= ptClick
.y
;
2082 case HDN_GETDISPINFOW
:
2083 // letting Windows XP handle this message results in mysterious
2084 // crashes in comctl32.dll seemingly because of bad message
2087 // I have no idea what is the real cause of the bug (which is,
2088 // just to make things interesting, impossible to reproduce
2089 // reliably) but ignoring all these messages does fix it and
2090 // doesn't seem to have any negative consequences
2094 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2098 #endif // defined(HDN_BEGINTRACKA)
2099 if ( nmhdr
->hwndFrom
== GetHwnd() )
2101 // almost all messages use NM_LISTVIEW
2102 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
2104 const int iItem
= nmLV
->iItem
;
2107 // If we have a valid item then check if there is a data value
2108 // associated with it and put it in the event.
2109 if ( iItem
>= 0 && iItem
< GetItemCount() )
2111 wxMSWListItemData
*internaldata
=
2112 MSWGetItemData(iItem
);
2115 event
.m_item
.m_data
= internaldata
->lParam
;
2118 bool processed
= true;
2119 switch ( nmhdr
->code
)
2121 case LVN_BEGINRDRAG
:
2122 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
2126 if ( eventType
== wxEVT_NULL
)
2128 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2131 event
.m_itemIndex
= iItem
;
2132 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
2133 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
2136 // NB: we have to handle both *A and *W versions here because some
2137 // versions of comctl32.dll send ANSI messages even to the
2139 case LVN_BEGINLABELEDITA
:
2140 case LVN_BEGINLABELEDITW
:
2143 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
2145 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
2147 else // LVN_BEGINLABELEDITW
2149 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
2152 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
2153 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
2154 event
.m_itemIndex
= event
.m_item
.m_itemId
;
2158 case LVN_ENDLABELEDITA
:
2159 case LVN_ENDLABELEDITW
:
2162 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
2164 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
2166 else // LVN_ENDLABELEDITW
2168 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
2171 // was editing cancelled?
2172 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
2173 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
2175 // EDIT control will be deleted by the list control
2176 // itself so prevent us from deleting it as well
2177 DeleteEditControl();
2179 event
.SetEditCanceled(true);
2182 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
2183 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
2184 event
.m_itemIndex
= event
.m_item
.m_itemId
;
2188 case LVN_COLUMNCLICK
:
2189 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
2190 event
.m_itemIndex
= -1;
2191 event
.m_col
= nmLV
->iSubItem
;
2194 case LVN_DELETEALLITEMS
:
2195 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
2196 event
.m_itemIndex
= -1;
2199 case LVN_DELETEITEM
:
2202 // this should be prevented by the post-processing code
2203 // below, but "just in case"
2207 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2208 event
.m_itemIndex
= iItem
;
2210 // delete the associated internal data
2211 if ( wxMSWListItemData
*data
= MSWGetItemData(iItem
) )
2213 const unsigned count
= m_internalData
.size();
2214 for ( unsigned n
= 0; n
< count
; n
++ )
2216 if ( m_internalData
[n
] == data
)
2218 m_internalData
.erase(m_internalData
.begin() + n
);
2225 wxASSERT_MSG( !data
, "invalid internal data pointer?" );
2229 case LVN_INSERTITEM
:
2230 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2231 event
.m_itemIndex
= iItem
;
2234 case LVN_ITEMCHANGED
:
2235 // we translate this catch all message into more interesting
2236 // (and more easy to process) wxWidgets events
2238 // first of all, we deal with the state change events only and
2239 // only for valid items (item == -1 for the virtual list
2241 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2243 // temp vars for readability
2244 const UINT stOld
= nmLV
->uOldState
;
2245 const UINT stNew
= nmLV
->uNewState
;
2247 event
.m_item
.SetId(iItem
);
2248 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2251 GetItem(event
.m_item
);
2253 // has the focus changed?
2254 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2256 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2257 event
.m_itemIndex
= iItem
;
2260 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2262 if ( eventType
!= wxEVT_NULL
)
2264 // focus and selection have both changed: send the
2265 // focus event from here and the selection one
2267 event
.SetEventType(eventType
);
2268 (void)HandleWindowEvent(event
);
2270 else // no focus event to send
2272 // then need to set m_itemIndex as it wasn't done
2274 event
.m_itemIndex
= iItem
;
2277 eventType
= stNew
& LVIS_SELECTED
2278 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2279 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2283 if ( eventType
== wxEVT_NULL
)
2285 // not an interesting event for us
2293 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2294 WORD wVKey
= info
->wVKey
;
2296 // get the current selection
2297 long lItem
= GetNextItem(-1,
2299 wxLIST_STATE_SELECTED
);
2301 // <Enter> or <Space> activate the selected item if any (but
2302 // not with any modifiers as they have a predefined meaning
2305 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2306 !wxIsAnyModifierDown() )
2308 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2312 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2314 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2315 // value which should be used as is
2316 int code
= wxCharCodeMSWToWX(wVKey
);
2317 event
.m_code
= code
? code
: wVKey
;
2321 event
.m_item
.m_itemId
= lItem
;
2325 // fill the other fields too
2326 event
.m_item
.m_text
= GetItemText(lItem
);
2327 event
.m_item
.m_data
= GetItemData(lItem
);
2333 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2335 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2340 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2341 // if it happened on an item (and not on empty place)
2348 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2349 event
.m_itemIndex
= iItem
;
2350 event
.m_item
.m_text
= GetItemText(iItem
);
2351 event
.m_item
.m_data
= GetItemData(iItem
);
2354 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2355 case GN_CONTEXTMENU
:
2356 #endif //__WXWINCE__
2358 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2359 // don't do anything else
2360 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2365 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2366 LV_HITTESTINFO lvhti
;
2367 wxZeroMemory(lvhti
);
2369 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2370 if ( nmhdr
->code
== GN_CONTEXTMENU
)
2372 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2375 #endif //__WXWINCE__
2377 ::GetCursorPos(&(lvhti
.pt
));
2380 ::ScreenToClient(GetHwnd(), &lvhti
.pt
);
2381 if ( ListView_HitTest(GetHwnd(), &lvhti
) != -1 )
2383 if ( lvhti
.flags
& LVHT_ONITEM
)
2385 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2386 event
.m_itemIndex
= lvhti
.iItem
;
2387 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2388 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2393 #ifdef NM_CUSTOMDRAW
2395 *result
= OnCustomDraw(lParam
);
2397 return *result
!= CDRF_DODEFAULT
;
2398 #endif // _WIN32_IE >= 0x300
2400 case LVN_ODCACHEHINT
:
2402 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2404 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2406 // we get some really stupid cache hints like ones for
2407 // items in range 0..0 for an empty control or, after
2408 // deleting an item, for items in invalid range -- filter
2410 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2413 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2415 const long iMax
= GetItemCount();
2416 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2421 #ifdef HAVE_NMLVFINDITEM
2422 case LVN_ODFINDITEM
:
2423 // this message is only used with the virtual list control but
2424 // even there we don't want to always use it: in a control with
2425 // sufficiently big number of items (defined as > 1000 here),
2426 // accidentally pressing a key could result in hanging an
2427 // application waiting while it performs linear search
2428 if ( IsVirtual() && GetItemCount() <= 1000 )
2430 NMLVFINDITEM
* pFindInfo
= (NMLVFINDITEM
*)lParam
;
2432 // no match by default
2435 // we only handle string-based searches here
2437 // TODO: what about LVFI_PARTIAL, should we handle this?
2438 if ( !(pFindInfo
->lvfi
.flags
& LVFI_STRING
) )
2443 const wxChar
* const searchstr
= pFindInfo
->lvfi
.psz
;
2444 const size_t len
= wxStrlen(searchstr
);
2446 // this is the first item we should examine, search from it
2447 // wrapping if necessary
2448 const int startPos
= pFindInfo
->iStart
;
2449 const int maxPos
= GetItemCount();
2450 wxCHECK_MSG( startPos
<= maxPos
, false,
2451 _T("bad starting position in LVN_ODFINDITEM") );
2453 int currentPos
= startPos
;
2456 // wrap to the beginning if necessary
2457 if ( currentPos
== maxPos
)
2459 // somewhat surprizingly, LVFI_WRAP isn't set in
2460 // flags but we still should wrap
2464 // does this item begin with searchstr?
2465 if ( wxStrnicmp(searchstr
,
2466 GetItemText(currentPos
), len
) == 0 )
2468 *result
= currentPos
;
2472 while ( ++currentPos
!= startPos
);
2474 if ( *result
== -1 )
2480 SetItemState(*result
,
2481 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
,
2482 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
);
2483 EnsureVisible(*result
);
2491 #endif // HAVE_NMLVFINDITEM
2493 case LVN_GETDISPINFO
:
2496 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2498 LV_ITEM
& lvi
= info
->item
;
2499 long item
= lvi
.iItem
;
2501 if ( lvi
.mask
& LVIF_TEXT
)
2503 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2504 wxStrlcpy(lvi
.pszText
, text
.c_str(), lvi
.cchTextMax
);
2507 // see comment at the end of wxListCtrl::GetColumn()
2508 #ifdef NM_CUSTOMDRAW
2509 if ( lvi
.mask
& LVIF_IMAGE
)
2511 lvi
.iImage
= OnGetItemColumnImage(item
, lvi
.iSubItem
);
2513 #endif // NM_CUSTOMDRAW
2515 // even though we never use LVM_SETCALLBACKMASK, we still
2516 // can get messages with LVIF_STATE in lvi.mask under Vista
2517 if ( lvi
.mask
& LVIF_STATE
)
2519 // we don't have anything to return from here...
2532 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2536 // where did this one come from?
2540 // process the event
2541 // -----------------
2543 event
.SetEventType(eventType
);
2545 // fill in the item before passing it to the event handler if we do have a
2546 // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED)
2547 if ( event
.m_itemIndex
!= -1 && !event
.m_item
.GetMask() )
2549 wxListItem
& item
= event
.m_item
;
2551 item
.SetId(event
.m_itemIndex
);
2552 item
.SetMask(wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
| wxLIST_MASK_DATA
);
2556 bool processed
= HandleWindowEvent(event
);
2560 switch ( nmhdr
->code
)
2562 case LVN_DELETEALLITEMS
:
2563 // always return true to suppress all additional LVN_DELETEITEM
2564 // notifications - this makes deleting all items from a list ctrl
2568 // also, we may free all user data now (couldn't do it before as
2569 // the user should have access to it in OnDeleteAllItems() handler)
2570 FreeAllInternalData();
2572 // the control is empty now, synchronize the cached number of items
2573 // with the real one
2577 case LVN_ENDLABELEDITA
:
2578 case LVN_ENDLABELEDITW
:
2579 // logic here is inverted compared to all the other messages
2580 *result
= event
.IsAllowed();
2582 // EDIT control will be deleted by the list control itself so
2583 // prevent us from deleting it as well
2584 DeleteEditControl();
2590 *result
= !event
.IsAllowed();
2595 // ----------------------------------------------------------------------------
2596 // custom draw stuff
2597 // ----------------------------------------------------------------------------
2599 // see comment at the end of wxListCtrl::GetColumn()
2600 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2602 static RECT
GetCustomDrawnItemRect(const NMCUSTOMDRAW
& nmcd
)
2605 wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_BOUNDS
, rc
);
2608 wxGetListCtrlItemRect(nmcd
.hdr
.hwndFrom
, nmcd
.dwItemSpec
, LVIR_ICON
, rcIcon
);
2610 // exclude the icon part, neither the selection background nor focus rect
2612 rc
.left
= rcIcon
.right
;
2618 bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
, int colCount
)
2620 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2623 HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2624 const int col
= pLVCD
->iSubItem
;
2625 const DWORD item
= nmcd
.dwItemSpec
;
2627 // the font must be valid, otherwise we wouldn't be painting the item at all
2628 SelectInHDC
selFont(hdc
, hfont
);
2630 // get the rectangle to paint
2631 int subitem
= colCount
? col
+ 1 : col
;
2633 wxGetListCtrlSubItemRect(hwndList
, item
, subitem
, LVIR_BOUNDS
, rc
);
2636 // get the image and text to draw
2640 it
.mask
= LVIF_TEXT
| LVIF_IMAGE
;
2644 it
.cchTextMax
= WXSIZEOF(text
);
2645 ListView_GetItem(hwndList
, &it
);
2647 HIMAGELIST himl
= ListView_GetImageList(hwndList
, LVSIL_SMALL
);
2648 if ( himl
&& ImageList_GetImageCount(himl
) )
2650 if ( it
.iImage
!= -1 )
2652 ImageList_Draw(himl
, it
.iImage
, hdc
, rc
.left
, rc
.top
,
2653 nmcd
.uItemState
& CDIS_SELECTED
? ILD_SELECTED
2657 // notice that even if this item doesn't have any image, the list
2658 // control still leaves space for the image in the first column if the
2659 // image list is not empty (presumably so that items with and without
2661 if ( it
.iImage
!= -1 || it
.iSubItem
== 0 )
2664 ImageList_GetIconSize(himl
, &wImage
, &hImage
);
2666 rc
.left
+= wImage
+ 2;
2670 ::SetBkMode(hdc
, TRANSPARENT
);
2672 UINT fmt
= DT_SINGLELINE
|
2675 #endif // __WXWINCE__
2680 wxZeroMemory(lvCol
);
2681 lvCol
.mask
= LVCF_FMT
;
2682 if ( ListView_GetColumn(hwndList
, col
, &lvCol
) )
2684 switch ( lvCol
.fmt
& LVCFMT_JUSTIFYMASK
)
2699 //else: failed to get alignment, assume it's DT_LEFT (default)
2701 DrawText(hdc
, text
, -1, &rc
, fmt
);
2706 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd
)
2708 if ( nmcd
.uItemState
& CDIS_FOCUS
)
2710 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2712 // don't use the provided HDC, it's in some strange state by now
2713 ::DrawFocusRect(WindowHDC(nmcd
.hdr
.hwndFrom
), &rc
);
2717 // pLVCD->clrText and clrTextBk should contain the colours to use
2718 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD
, HFONT hfont
)
2720 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
; // just a shortcut
2722 const HWND hwndList
= nmcd
.hdr
.hwndFrom
;
2723 const int item
= nmcd
.dwItemSpec
;
2725 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2726 // the item is not at all selected for some reason (comctl32 6), but we
2727 // also can't always trust ListView_GetItem() as it could return the old
2728 // item status if we're called just after the (de)selection, so remember
2729 // the last item to gain selection and also check for it here
2730 for ( int i
= -1;; )
2732 i
= ListView_GetNextItem(hwndList
, i
, LVNI_SELECTED
);
2735 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2741 nmcd
.uItemState
|= CDIS_SELECTED
;
2746 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2748 // NB: cast is needed to work around the bug in mingw32 headers which don't
2749 // have it inside ListView_GetNextItem() itself (unlike SDK ones)
2750 if ( ::GetFocus() == hwndList
&&
2751 ListView_GetNextItem(
2752 hwndList
, static_cast<WPARAM
>(-1), LVNI_FOCUSED
) == item
)
2754 nmcd
.uItemState
|= CDIS_FOCUS
;
2758 nmcd
.uItemState
&= ~CDIS_FOCUS
;
2761 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2763 int syscolFg
, syscolBg
;
2764 if ( ::GetFocus() == hwndList
)
2766 syscolFg
= COLOR_HIGHLIGHTTEXT
;
2767 syscolBg
= COLOR_HIGHLIGHT
;
2769 else // selected but unfocused
2771 syscolFg
= COLOR_WINDOWTEXT
;
2772 syscolBg
= COLOR_BTNFACE
;
2774 // don't grey out the icon in this case neither
2775 nmcd
.uItemState
&= ~CDIS_SELECTED
;
2778 pLVCD
->clrText
= ::GetSysColor(syscolFg
);
2779 pLVCD
->clrTextBk
= ::GetSysColor(syscolBg
);
2781 //else: not selected, use normal colours from pLVCD
2784 RECT rc
= GetCustomDrawnItemRect(nmcd
);
2786 ::SetTextColor(hdc
, pLVCD
->clrText
);
2787 ::FillRect(hdc
, &rc
, AutoHBRUSH(pLVCD
->clrTextBk
));
2789 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2790 // problems so just draw everything except the focus rect from here instead
2791 const int colCount
= Header_GetItemCount(ListView_GetHeader(hwndList
));
2792 for ( int col
= 0; col
< colCount
; col
++ )
2794 pLVCD
->iSubItem
= col
;
2795 HandleSubItemPrepaint(pLVCD
, hfont
, colCount
);
2798 HandleItemPostpaint(nmcd
);
2801 static WXLPARAM
HandleItemPrepaint(wxListCtrl
*listctrl
,
2802 LPNMLVCUSTOMDRAW pLVCD
,
2803 wxListItemAttr
*attr
)
2807 // nothing to do for this item
2808 return CDRF_DODEFAULT
;
2812 // set the colours to use for text drawing
2813 pLVCD
->clrText
= attr
->HasTextColour()
2814 ? wxColourToRGB(attr
->GetTextColour())
2815 : wxColourToRGB(listctrl
->GetTextColour());
2816 pLVCD
->clrTextBk
= attr
->HasBackgroundColour()
2817 ? wxColourToRGB(attr
->GetBackgroundColour())
2818 : wxColourToRGB(listctrl
->GetBackgroundColour());
2820 // select the font if non default one is specified
2821 if ( attr
->HasFont() )
2823 wxFont font
= attr
->GetFont();
2824 if ( font
.GetEncoding() != wxFONTENCODING_SYSTEM
)
2826 // the standard control ignores the font encoding/charset, at least
2827 // with recent comctl32.dll versions (5 and 6, it uses to work with
2828 // 4.something) so we have to draw the item entirely ourselves in
2830 HandleItemPaint(pLVCD
, GetHfontOf(font
));
2831 return CDRF_SKIPDEFAULT
;
2834 ::SelectObject(pLVCD
->nmcd
.hdc
, GetHfontOf(font
));
2836 return CDRF_NEWFONT
;
2839 return CDRF_DODEFAULT
;
2842 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2844 LPNMLVCUSTOMDRAW pLVCD
= (LPNMLVCUSTOMDRAW
)lParam
;
2845 NMCUSTOMDRAW
& nmcd
= pLVCD
->nmcd
;
2846 switch ( nmcd
.dwDrawStage
)
2849 // if we've got any items with non standard attributes,
2850 // notify us before painting each item
2852 // for virtual controls, always suppose that we have attributes as
2853 // there is no way to check for this
2854 if ( IsVirtual() || m_hasAnyAttr
)
2855 return CDRF_NOTIFYITEMDRAW
;
2858 case CDDS_ITEMPREPAINT
:
2859 // get a message for each subitem
2860 return CDRF_NOTIFYITEMDRAW
;
2862 case CDDS_SUBITEM
| CDDS_ITEMPREPAINT
:
2863 const int item
= nmcd
.dwItemSpec
;
2864 const int column
= pLVCD
->iSubItem
;
2866 // we get this message with item == 0 for an empty control, we
2867 // must ignore it as calling OnGetItemAttr() would be wrong
2868 if ( item
< 0 || item
>= GetItemCount() )
2871 if ( column
< 0 || column
>= GetColumnCount() )
2874 return HandleItemPrepaint(this, pLVCD
, DoGetItemColumnAttr(item
, column
));
2877 return CDRF_DODEFAULT
;
2880 #endif // NM_CUSTOMDRAW supported
2882 // Necessary for drawing hrules and vrules, if specified
2883 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2885 const int itemCount
= GetItemCount();
2886 const bool drawHRules
= HasFlag(wxLC_HRULES
);
2887 const bool drawVRules
= HasFlag(wxLC_VRULES
);
2889 if (!InReportView() || !(drawHRules
|| drawVRules
) || !itemCount
)
2897 wxControl::OnPaint(event
);
2899 // Reset the device origin since it may have been set
2900 dc
.SetDeviceOrigin(0, 0);
2902 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
));
2904 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2906 wxSize clientSize
= GetClientSize();
2911 const long top
= GetTopItem();
2912 for ( int i
= top
; i
< top
+ GetCountPerPage() + 1; i
++ )
2914 if (GetItemRect(i
, itemRect
))
2916 int cy
= itemRect
.GetTop();
2917 if (i
!= 0) // Don't draw the first one
2919 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2922 if (i
== itemCount
- 1)
2924 cy
= itemRect
.GetBottom();
2925 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2934 wxRect firstItemRect
;
2935 GetItemRect(0, firstItemRect
);
2937 if (GetItemRect(itemCount
- 1, itemRect
))
2939 // this is a fix for bug 673394: erase the pixels which we would
2940 // otherwise leave on the screen
2941 static const int gap
= 2;
2942 dc
.SetPen(*wxTRANSPARENT_PEN
);
2943 dc
.SetBrush(wxBrush(GetBackgroundColour()));
2944 dc
.DrawRectangle(0, firstItemRect
.GetY() - gap
,
2945 clientSize
.GetWidth(), gap
);
2948 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2950 const int numCols
= GetColumnCount();
2951 wxVector
<int> indexArray(numCols
);
2952 if ( !ListView_GetColumnOrderArray(GetHwnd(),
2956 wxFAIL_MSG( _T("invalid column index array in OnPaint()") );
2960 int x
= itemRect
.GetX();
2961 for (int col
= 0; col
< numCols
; col
++)
2963 int colWidth
= GetColumnWidth(indexArray
[col
]);
2965 dc
.DrawLine(x
-1, firstItemRect
.GetY() - gap
,
2966 x
-1, itemRect
.GetBottom());
2973 wxListCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2979 // we should bypass our own WM_PRINT handling as we don't handle
2980 // PRF_CHILDREN flag, so leave it to the native control itself
2981 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
2984 case WM_CONTEXTMENU
:
2985 // because this message is propagated upwards the child-parent
2986 // chain, we get it for the right clicks on the header window but
2987 // this is confusing in wx as right clicking there already
2988 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2989 // so just ignore them
2990 if ( (HWND
)wParam
== ListView_GetHeader(GetHwnd()) )
2995 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2998 // ----------------------------------------------------------------------------
2999 // virtual list controls
3000 // ----------------------------------------------------------------------------
3002 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
3004 // this is a pure virtual function, in fact - which is not really pure
3005 // because the controls which are not virtual don't need to implement it
3006 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
3008 return wxEmptyString
;
3011 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
3013 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
3015 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
3019 int wxListCtrl::OnGetItemColumnImage(long item
, long column
) const
3022 return OnGetItemImage(item
);
3027 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
3029 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
3030 _T("invalid item index in OnGetItemAttr()") );
3032 // no attributes by default
3036 wxListItemAttr
*wxListCtrl::DoGetItemColumnAttr(long item
, long column
) const
3039 return OnGetItemColumnAttr(item
, column
);
3041 wxMSWListItemData
* const data
= MSWGetItemData(item
);
3042 return data
? data
->attr
: NULL
;
3045 void wxListCtrl::SetItemCount(long count
)
3047 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
3049 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
3050 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
3052 wxLogLastError(_T("ListView_SetItemCount"));
3055 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
3056 wxT("m_count should match ListView_GetItemCount"));
3059 void wxListCtrl::RefreshItem(long item
)
3061 RefreshItems(item
, item
);
3064 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
3066 ListView_RedrawItems(GetHwnd(), itemFrom
, itemTo
);
3069 // ----------------------------------------------------------------------------
3070 // wxWin <-> MSW items conversions
3071 // ----------------------------------------------------------------------------
3073 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
3077 wxMSWListItemData
*internaldata
=
3078 (wxMSWListItemData
*) lvItem
.lParam
;
3081 info
.m_data
= internaldata
->lParam
;
3085 info
.m_stateMask
= 0;
3086 info
.m_itemId
= lvItem
.iItem
;
3088 long oldMask
= lvItem
.mask
;
3090 bool needText
= false;
3091 if (hwndListCtrl
!= 0)
3093 if ( lvItem
.mask
& LVIF_TEXT
)
3100 lvItem
.pszText
= new wxChar
[513];
3101 lvItem
.cchTextMax
= 512;
3103 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
3104 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
3107 if ( lvItem
.mask
& LVIF_STATE
)
3109 info
.m_mask
|= wxLIST_MASK_STATE
;
3111 if ( lvItem
.stateMask
& LVIS_CUT
)
3113 info
.m_stateMask
|= wxLIST_STATE_CUT
;
3114 if ( lvItem
.state
& LVIS_CUT
)
3115 info
.m_state
|= wxLIST_STATE_CUT
;
3117 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
3119 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
3120 if ( lvItem
.state
& LVIS_DROPHILITED
)
3121 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
3123 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
3125 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
3126 if ( lvItem
.state
& LVIS_FOCUSED
)
3127 info
.m_state
|= wxLIST_STATE_FOCUSED
;
3129 if ( lvItem
.stateMask
& LVIS_SELECTED
)
3131 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
3132 if ( lvItem
.state
& LVIS_SELECTED
)
3133 info
.m_state
|= wxLIST_STATE_SELECTED
;
3137 if ( lvItem
.mask
& LVIF_TEXT
)
3139 info
.m_mask
|= wxLIST_MASK_TEXT
;
3140 info
.m_text
= lvItem
.pszText
;
3142 if ( lvItem
.mask
& LVIF_IMAGE
)
3144 info
.m_mask
|= wxLIST_MASK_IMAGE
;
3145 info
.m_image
= lvItem
.iImage
;
3147 if ( lvItem
.mask
& LVIF_PARAM
)
3148 info
.m_mask
|= wxLIST_MASK_DATA
;
3149 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
3150 info
.m_mask
|= wxLIST_SET_ITEM
;
3151 info
.m_col
= lvItem
.iSubItem
;
3156 delete[] lvItem
.pszText
;
3158 lvItem
.mask
= oldMask
;
3161 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
3163 if (stateMask
& wxLIST_STATE_CUT
)
3165 lvItem
.stateMask
|= LVIS_CUT
;
3166 if (state
& wxLIST_STATE_CUT
)
3167 lvItem
.state
|= LVIS_CUT
;
3169 if (stateMask
& wxLIST_STATE_DROPHILITED
)
3171 lvItem
.stateMask
|= LVIS_DROPHILITED
;
3172 if (state
& wxLIST_STATE_DROPHILITED
)
3173 lvItem
.state
|= LVIS_DROPHILITED
;
3175 if (stateMask
& wxLIST_STATE_FOCUSED
)
3177 lvItem
.stateMask
|= LVIS_FOCUSED
;
3178 if (state
& wxLIST_STATE_FOCUSED
)
3179 lvItem
.state
|= LVIS_FOCUSED
;
3181 if (stateMask
& wxLIST_STATE_SELECTED
)
3183 lvItem
.stateMask
|= LVIS_SELECTED
;
3184 if (state
& wxLIST_STATE_SELECTED
)
3185 lvItem
.state
|= LVIS_SELECTED
;
3189 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
3190 const wxListItem
& info
,
3193 if ( ctrl
->InReportView() )
3195 wxASSERT_MSG( 0 <= info
.m_col
&& info
.m_col
< ctrl
->GetColumnCount(),
3196 "wxListCtrl column index out of bounds" );
3198 else // not in report view
3200 wxASSERT_MSG( info
.m_col
== 0, "columns only exist in report view" );
3203 lvItem
.iItem
= (int) info
.m_itemId
;
3205 lvItem
.iImage
= info
.m_image
;
3206 lvItem
.stateMask
= 0;
3209 lvItem
.iSubItem
= info
.m_col
;
3211 if (info
.m_mask
& wxLIST_MASK_STATE
)
3213 lvItem
.mask
|= LVIF_STATE
;
3215 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
3218 if (info
.m_mask
& wxLIST_MASK_TEXT
)
3220 lvItem
.mask
|= LVIF_TEXT
;
3221 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
3223 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
3227 // pszText is not const, hence the cast
3228 lvItem
.pszText
= (wxChar
*)info
.m_text
.wx_str();
3229 if ( lvItem
.pszText
)
3230 lvItem
.cchTextMax
= info
.m_text
.length();
3232 lvItem
.cchTextMax
= 0;
3235 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
3236 lvItem
.mask
|= LVIF_IMAGE
;
3239 static void wxConvertToMSWListCol(HWND hwndList
,
3241 const wxListItem
& item
,
3244 wxZeroMemory(lvCol
);
3246 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
3248 lvCol
.mask
|= LVCF_TEXT
;
3249 lvCol
.pszText
= (wxChar
*)item
.m_text
.wx_str(); // cast is safe
3252 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
3254 lvCol
.mask
|= LVCF_FMT
;
3256 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
3257 lvCol
.fmt
= LVCFMT_LEFT
;
3258 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
3259 lvCol
.fmt
= LVCFMT_RIGHT
;
3260 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
3261 lvCol
.fmt
= LVCFMT_CENTER
;
3264 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
3266 lvCol
.mask
|= LVCF_WIDTH
;
3267 if ( item
.m_width
== wxLIST_AUTOSIZE
)
3268 lvCol
.cx
= LVSCW_AUTOSIZE
;
3269 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3270 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
3272 lvCol
.cx
= item
.m_width
;
3275 // see comment at the end of wxListCtrl::GetColumn()
3276 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3277 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
3279 if ( wxApp::GetComCtl32Version() >= 470 )
3281 lvCol
.mask
|= LVCF_IMAGE
;
3283 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3284 // seem to be generally nicer than on the left and the generic
3285 // version only draws them on the right (we don't have a flag to
3286 // specify the image location anyhow)
3288 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3289 // make any difference in my tests -- but maybe we should?
3290 if ( item
.m_image
!= -1 )
3292 // as we're going to overwrite the format field, get its
3293 // current value first -- unless we want to overwrite it anyhow
3294 if ( !(lvCol
.mask
& LVCF_FMT
) )
3297 wxZeroMemory(lvColOld
);
3298 lvColOld
.mask
= LVCF_FMT
;
3299 if ( ListView_GetColumn(hwndList
, col
, &lvColOld
) )
3301 lvCol
.fmt
= lvColOld
.fmt
;
3304 lvCol
.mask
|= LVCF_FMT
;
3307 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
3310 lvCol
.iImage
= item
.m_image
;
3312 //else: it doesn't support item images anyhow
3314 #endif // _WIN32_IE >= 0x0300
3317 #endif // wxUSE_LISTCTRL