1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
38 #include "wx/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
46 #include "wx/msw/private.h"
48 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
56 // include <commctrl.h> "properly"
57 #include "wx/msw/wrapcctl.h"
59 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
60 // it by its old name NM_FINDTIEM.
62 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
63 #define HAVE_NMLVFINDITEM 1
64 #elif defined(__DMC__) || defined(NM_FINDITEM)
65 #define HAVE_NMLVFINDITEM 1
66 #define NMLVFINDITEM NM_FINDITEM
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // convert our state and mask flags to LV_ITEM constants
74 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
76 // convert wxListItem to LV_ITEM
77 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
78 const wxListItem
& info
, LV_ITEM
& lvItem
);
80 // convert LV_ITEM to wxListItem
81 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
83 /* const */ LV_ITEM
& lvItem
);
85 // convert our wxListItem to LV_COLUMN
86 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
89 // ----------------------------------------------------------------------------
90 // private helper classes
91 // ----------------------------------------------------------------------------
93 // We have to handle both fooW and fooA notifications in several cases
94 // because of broken comctl32.dll and/or unicows.dll. This class is used to
95 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
96 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
97 // by wxConvertToMSWListItem().
99 #define LV_ITEM_NATIVE LV_ITEMW
100 #define LV_ITEM_OTHER LV_ITEMA
102 #define LV_CONV_TO_WX cMB2WX
103 #define LV_CONV_BUF wxMB2WXbuf
105 #define LV_ITEM_NATIVE LV_ITEMA
106 #define LV_ITEM_OTHER LV_ITEMW
108 #define LV_CONV_TO_WX cWC2WX
109 #define LV_CONV_BUF wxWC2WXbuf
110 #endif // Unicode/ANSI
115 // default ctor, use Init() later
116 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
118 // init without conversion
119 void Init(LV_ITEM_NATIVE
& item
)
121 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
126 // init with conversion
127 void Init(LV_ITEM_OTHER
& item
)
129 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
130 // point to our own m_item
132 // memcpy() can't work if the struct sizes are different
133 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
134 CodeCantWorkIfDiffSizes
);
136 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
138 // convert text from ANSI to Unicod if necessary
139 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
141 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
142 m_item
.pszText
= (wxChar
*)m_buf
->data();
146 // ctor without conversion
147 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
149 // ctor with conversion
150 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
155 ~wxLV_ITEM() { delete m_buf
; }
157 // conversion to the real LV_ITEM
158 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
163 LV_ITEM_NATIVE
*m_pItem
;
164 LV_ITEM_NATIVE m_item
;
166 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
169 ///////////////////////////////////////////////////////
171 // The MSW version had problems with SetTextColour() et
172 // al as the wxListItemAttr's were stored keyed on the
173 // item index. If a item was inserted anywhere but the end
174 // of the list the the text attributes (colour etc) for
175 // the following items were out of sync.
178 // Under MSW the only way to associate data with a List
179 // item independent of its position in the list is to
180 // store a pointer to it in its lParam attribute. However
181 // user programs are already using this (via the
182 // SetItemData() GetItemData() calls).
184 // However what we can do is store a pointer to a
185 // structure which contains the attributes we want *and*
186 // a lParam for the users data, e.g.
188 // class wxListItemInternalData
191 // wxListItemAttr *attr;
192 // long lParam; // user data
195 // To conserve memory, a wxListItemInternalData is
196 // only allocated for a LV_ITEM if text attributes or
197 // user data(lparam) are being set.
200 // class wxListItemInternalData
201 class wxListItemInternalData
204 wxListItemAttr
*attr
;
205 LPARAM lParam
; // user data
207 wxListItemInternalData() : attr(NULL
), lParam(0) {}
208 ~wxListItemInternalData()
214 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
217 // Get the internal data structure
218 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
219 static wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
);
220 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
221 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
234 #if WXWIN_COMPATIBILITY_2_4
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
238 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
239 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
240 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
241 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
242 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
243 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
244 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
245 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
246 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
247 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
248 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
249 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
250 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
251 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
253 #if wxUSE_EXTENDED_RTTI
254 WX_DEFINE_FLAGS( wxListCtrlStyle
)
256 wxBEGIN_FLAGS( wxListCtrlStyle
)
257 // new style border flags, we put them first to
258 // use them for streaming out
259 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
260 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
261 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
262 wxFLAGS_MEMBER(wxBORDER_RAISED
)
263 wxFLAGS_MEMBER(wxBORDER_STATIC
)
264 wxFLAGS_MEMBER(wxBORDER_NONE
)
266 // old style border flags
267 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
268 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
269 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
270 wxFLAGS_MEMBER(wxRAISED_BORDER
)
271 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
272 wxFLAGS_MEMBER(wxBORDER
)
274 // standard window styles
275 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
276 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
277 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
278 wxFLAGS_MEMBER(wxWANTS_CHARS
)
279 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
280 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
281 wxFLAGS_MEMBER(wxVSCROLL
)
282 wxFLAGS_MEMBER(wxHSCROLL
)
284 wxFLAGS_MEMBER(wxLC_LIST
)
285 wxFLAGS_MEMBER(wxLC_REPORT
)
286 wxFLAGS_MEMBER(wxLC_ICON
)
287 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
288 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
289 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
290 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
291 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
292 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
293 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
294 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
295 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
296 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
297 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
299 wxEND_FLAGS( wxListCtrlStyle
)
301 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
303 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
304 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
306 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
307 wxEND_PROPERTIES_TABLE()
309 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
310 wxEND_HANDLERS_TABLE()
312 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
315 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
318 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
321 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
322 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
324 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
326 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
327 EVT_PAINT(wxListCtrl::OnPaint
)
330 // ============================================================================
332 // ============================================================================
334 // ----------------------------------------------------------------------------
335 // wxListCtrl construction
336 // ----------------------------------------------------------------------------
338 void wxListCtrl::Init()
340 m_imageListNormal
= NULL
;
341 m_imageListSmall
= NULL
;
342 m_imageListState
= NULL
;
343 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= false;
346 m_ignoreChangeMessages
= false;
348 m_AnyInternalData
= false;
349 m_hasAnyAttr
= false;
352 bool wxListCtrl::Create(wxWindow
*parent
,
357 const wxValidator
& validator
,
358 const wxString
& name
)
360 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
363 if ( !MSWCreateControl(WC_LISTVIEW
, wxEmptyString
, pos
, size
) )
366 // explicitly say that we want to use Unicode because otherwise we get ANSI
367 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
368 wxSetCCUnicodeFormat(GetHwnd());
370 // for comctl32.dll v 4.70+ we want to have some non default extended
371 // styles because it's prettier (and also because wxGTK does it like this)
372 if ( InReportView() && wxApp::GetComCtl32Version() >= 470 )
374 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
375 0, LVS_EX_LABELTIP
| LVS_EX_FULLROWSELECT
);
381 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
383 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
385 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
390 #define MAP_MODE_STYLE(wx, ms) \
391 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
392 #else // !__WXDEBUG__
393 #define MAP_MODE_STYLE(wx, ms) \
394 if ( style & (wx) ) wstyle |= (ms);
395 #endif // __WXDEBUG__
397 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
398 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
399 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
400 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
402 wxASSERT_MSG( nModes
== 1,
403 _T("wxListCtrl style should have exactly one mode bit set") );
405 #undef MAP_MODE_STYLE
407 if ( style
& wxLC_ALIGN_LEFT
)
408 wstyle
|= LVS_ALIGNLEFT
;
410 if ( style
& wxLC_ALIGN_TOP
)
411 wstyle
|= LVS_ALIGNTOP
;
413 if ( style
& wxLC_AUTOARRANGE
)
414 wstyle
|= LVS_AUTOARRANGE
;
416 if ( style
& wxLC_NO_SORT_HEADER
)
417 wstyle
|= LVS_NOSORTHEADER
;
419 if ( style
& wxLC_NO_HEADER
)
420 wstyle
|= LVS_NOCOLUMNHEADER
;
422 if ( style
& wxLC_EDIT_LABELS
)
423 wstyle
|= LVS_EDITLABELS
;
425 if ( style
& wxLC_SINGLE_SEL
)
426 wstyle
|= LVS_SINGLESEL
;
428 if ( style
& wxLC_SORT_ASCENDING
)
430 wstyle
|= LVS_SORTASCENDING
;
432 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
433 _T("can't sort in ascending and descending orders at once") );
435 else if ( style
& wxLC_SORT_DESCENDING
)
436 wstyle
|= LVS_SORTDESCENDING
;
438 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
439 if ( style
& wxLC_VIRTUAL
)
441 int ver
= wxApp::GetComCtl32Version();
444 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."),
445 ver
/ 100, ver
% 100);
448 wstyle
|= LVS_OWNERDATA
;
450 #endif // ancient cygwin
455 void wxListCtrl::UpdateStyle()
459 // The new window view style
460 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
462 // some styles are not returned by MSWGetStyle()
464 dwStyleNew
|= WS_VISIBLE
;
466 // Get the current window style.
467 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
469 // we don't have wxVSCROLL style, but the list control may have it,
470 // don't change it then
471 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
473 // Only set the window style if the view bits have changed.
474 if ( dwStyleOld
!= dwStyleNew
)
476 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
481 void wxListCtrl::FreeAllInternalData()
483 if (m_AnyInternalData
)
485 int n
= GetItemCount();
487 m_ignoreChangeMessages
= true;
488 for (int i
= 0; i
< n
; i
++)
489 wxDeleteInternalData(this, i
);
490 m_ignoreChangeMessages
= false;
492 m_AnyInternalData
= false;
496 wxListCtrl::~wxListCtrl()
498 FreeAllInternalData();
502 m_textCtrl
->UnsubclassWin();
503 m_textCtrl
->SetHWND(0);
508 if (m_ownsImageListNormal
)
509 delete m_imageListNormal
;
510 if (m_ownsImageListSmall
)
511 delete m_imageListSmall
;
512 if (m_ownsImageListState
)
513 delete m_imageListState
;
516 // ----------------------------------------------------------------------------
517 // set/get/change style
518 // ----------------------------------------------------------------------------
520 // Add or remove a single window style
521 void wxListCtrl::SetSingleStyle(long style
, bool add
)
523 long flag
= GetWindowStyleFlag();
525 // Get rid of conflicting styles
528 if ( style
& wxLC_MASK_TYPE
)
529 flag
= flag
& ~wxLC_MASK_TYPE
;
530 if ( style
& wxLC_MASK_ALIGN
)
531 flag
= flag
& ~wxLC_MASK_ALIGN
;
532 if ( style
& wxLC_MASK_SORT
)
533 flag
= flag
& ~wxLC_MASK_SORT
;
541 SetWindowStyleFlag(flag
);
544 // Set the whole window style
545 void wxListCtrl::SetWindowStyleFlag(long flag
)
547 if ( flag
!= m_windowStyle
)
549 m_windowStyle
= flag
;
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
561 /* static */ wxVisualAttributes
562 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
564 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
566 // common controls have their own default font
567 attrs
.font
= wxGetCCDefaultFont();
572 // Sets the foreground, i.e. text, colour
573 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
575 if ( !wxWindow::SetForegroundColour(col
) )
578 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
583 // Sets the background colour
584 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
586 if ( !wxWindow::SetBackgroundColour(col
) )
589 // we set the same colour for both the "empty" background and the items
591 COLORREF color
= wxColourToRGB(col
);
592 ListView_SetBkColor(GetHwnd(), color
);
593 ListView_SetTextBkColor(GetHwnd(), color
);
598 // Gets information about this column
599 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
604 lvCol
.mask
= LVCF_WIDTH
;
606 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
608 lvCol
.mask
|= LVCF_TEXT
;
609 lvCol
.pszText
= new wxChar
[513];
610 lvCol
.cchTextMax
= 512;
613 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
615 lvCol
.mask
|= LVCF_FMT
;
618 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
620 lvCol
.mask
|= LVCF_IMAGE
;
623 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
625 // item.m_subItem = lvCol.iSubItem;
626 item
.m_width
= lvCol
.cx
;
628 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
630 item
.m_text
= lvCol
.pszText
;
631 delete[] lvCol
.pszText
;
634 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
636 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
638 item
.m_format
= wxLIST_FORMAT_LEFT
;
641 item
.m_format
= wxLIST_FORMAT_RIGHT
;
644 item
.m_format
= wxLIST_FORMAT_CENTRE
;
647 item
.m_format
= -1; // Unknown?
652 // the column images were not supported in older versions but how to check
653 // for this? we can't use _WIN32_IE because we always define it to a very
654 // high value, so see if another symbol which is only defined starting from
655 // comctl32.dll 4.70 is available
656 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
657 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
659 item
.m_image
= lvCol
.iImage
;
661 #endif // LVCOLUMN::iImage exists
666 // Sets information about this column
667 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
670 wxConvertToMSWListCol(col
, item
, lvCol
);
672 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
675 // Gets the column width
676 int wxListCtrl::GetColumnWidth(int col
) const
678 return ListView_GetColumnWidth(GetHwnd(), col
);
681 // Sets the column width
682 bool wxListCtrl::SetColumnWidth(int col
, int width
)
684 if ( m_windowStyle
& wxLC_LIST
)
687 if ( width
== wxLIST_AUTOSIZE
)
688 width
= LVSCW_AUTOSIZE
;
689 else if ( width
== wxLIST_AUTOSIZE_USEHEADER
)
690 width
= LVSCW_AUTOSIZE_USEHEADER
;
692 return ListView_SetColumnWidth(GetHwnd(), col
, width
) != 0;
695 // Gets the number of items that can fit vertically in the
696 // visible area of the list control (list or report view)
697 // or the total number of items in the list control (icon
698 // or small icon view)
699 int wxListCtrl::GetCountPerPage() const
701 return ListView_GetCountPerPage(GetHwnd());
704 // Gets the edit control for editing labels.
705 wxTextCtrl
* wxListCtrl::GetEditControl() const
710 // Gets information about the item
711 bool wxListCtrl::GetItem(wxListItem
& info
) const
714 wxZeroMemory(lvItem
);
716 lvItem
.iItem
= info
.m_itemId
;
717 lvItem
.iSubItem
= info
.m_col
;
719 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
721 lvItem
.mask
|= LVIF_TEXT
;
722 lvItem
.pszText
= new wxChar
[513];
723 lvItem
.cchTextMax
= 512;
727 lvItem
.pszText
= NULL
;
730 if (info
.m_mask
& wxLIST_MASK_DATA
)
731 lvItem
.mask
|= LVIF_PARAM
;
733 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
734 lvItem
.mask
|= LVIF_IMAGE
;
736 if ( info
.m_mask
& wxLIST_MASK_STATE
)
738 lvItem
.mask
|= LVIF_STATE
;
739 wxConvertToMSWFlags(0, info
.m_stateMask
, lvItem
);
742 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
745 wxLogError(_("Couldn't retrieve information about list control item %d."),
750 // give NULL as hwnd as we already have everything we need
751 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
755 delete[] lvItem
.pszText
;
760 // Sets information about the item
761 bool wxListCtrl::SetItem(wxListItem
& info
)
764 wxConvertToMSWListItem(this, info
, item
);
766 // we never update the lParam if it contains our pointer
767 // to the wxListItemInternalData structure
768 item
.mask
&= ~LVIF_PARAM
;
770 // check if setting attributes or lParam
771 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
773 // get internal item data
774 // perhaps a cache here ?
775 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
780 m_AnyInternalData
= true;
781 data
= new wxListItemInternalData();
782 item
.lParam
= (LPARAM
) data
;
783 item
.mask
|= LVIF_PARAM
;
788 if (info
.m_mask
& wxLIST_MASK_DATA
)
789 data
->lParam
= info
.m_data
;
792 if (info
.HasAttributes())
795 *data
->attr
= *info
.GetAttributes();
797 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
802 // we could be changing only the attribute in which case we don't need to
803 // call ListView_SetItem() at all
807 if ( !ListView_SetItem(GetHwnd(), &item
) )
809 wxLogDebug(_T("ListView_SetItem() failed"));
815 // we need to update the item immediately to show the new image
816 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
818 // check whether it has any custom attributes
819 if ( info
.HasAttributes() )
823 // if the colour has changed, we must redraw the item
829 // we need this to make the change visible right now
830 RefreshItem(item
.iItem
);
836 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
840 info
.m_mask
= wxLIST_MASK_TEXT
;
841 info
.m_itemId
= index
;
845 info
.m_image
= imageId
;
846 info
.m_mask
|= wxLIST_MASK_IMAGE
;
848 return SetItem(info
);
852 // Gets the item state
853 int wxListCtrl::GetItemState(long item
, long stateMask
) const
857 info
.m_mask
= wxLIST_MASK_STATE
;
858 info
.m_stateMask
= stateMask
;
859 info
.m_itemId
= item
;
867 // Sets the item state
868 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
870 // NB: don't use SetItem() here as it doesn't work with the virtual list
873 wxZeroMemory(lvItem
);
875 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
877 // for the virtual list controls we need to refresh the previously focused
878 // item manually when changing focus without changing selection
879 // programmatically because otherwise it keeps its focus rectangle until
880 // next repaint (yet another comctl32 bug)
883 (stateMask
& wxLIST_STATE_FOCUSED
) &&
884 (state
& wxLIST_STATE_FOCUSED
) )
886 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
893 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
894 (WPARAM
)item
, (LPARAM
)&lvItem
) )
896 wxLogLastError(_T("ListView_SetItemState"));
901 if ( focusOld
!= -1 )
903 // no need to refresh the item if it was previously selected, it would
904 // only result in annoying flicker
905 if ( !(GetItemState(focusOld
,
906 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
908 RefreshItem(focusOld
);
915 // Sets the item image
916 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
920 info
.m_mask
= wxLIST_MASK_IMAGE
;
921 info
.m_image
= image
;
922 info
.m_itemId
= item
;
924 return SetItem(info
);
927 // Gets the item text
928 wxString
wxListCtrl::GetItemText(long item
) const
932 info
.m_mask
= wxLIST_MASK_TEXT
;
933 info
.m_itemId
= item
;
936 return wxEmptyString
;
940 // Sets the item text
941 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
945 info
.m_mask
= wxLIST_MASK_TEXT
;
946 info
.m_itemId
= item
;
952 // Gets the item data
953 wxUIntPtr
wxListCtrl::GetItemData(long item
) const
957 info
.m_mask
= wxLIST_MASK_DATA
;
958 info
.m_itemId
= item
;
965 // Sets the item data
966 bool wxListCtrl::SetItemData(long item
, long data
)
970 info
.m_mask
= wxLIST_MASK_DATA
;
971 info
.m_itemId
= item
;
974 return SetItem(info
);
977 wxRect
wxListCtrl::GetViewRect() const
979 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
980 _T("wxListCtrl::GetViewRect() only works in icon mode") );
983 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
985 wxLogDebug(_T("ListView_GetViewRect() failed."));
991 wxCopyRECTToRect(rc
, rect
);
996 // Gets the item rectangle
997 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1002 if ( code
== wxLIST_RECT_BOUNDS
)
1003 codeWin
= LVIR_BOUNDS
;
1004 else if ( code
== wxLIST_RECT_ICON
)
1005 codeWin
= LVIR_ICON
;
1006 else if ( code
== wxLIST_RECT_LABEL
)
1007 codeWin
= LVIR_LABEL
;
1010 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1012 codeWin
= LVIR_BOUNDS
;
1015 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1017 rect
.x
= rectWin
.left
;
1018 rect
.y
= rectWin
.top
;
1019 rect
.width
= rectWin
.right
- rectWin
.left
;
1020 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1025 // Gets the item position
1026 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1030 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1032 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1036 // Sets the item position.
1037 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1039 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1042 // Gets the number of items in the list control
1043 int wxListCtrl::GetItemCount() const
1048 wxSize
wxListCtrl::GetItemSpacing() const
1050 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1052 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1055 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1057 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1060 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1063 info
.m_itemId
= item
;
1064 info
.SetTextColour( col
);
1068 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1071 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1072 if ( data
&& data
->attr
)
1073 col
= data
->attr
->GetTextColour();
1078 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1081 info
.m_itemId
= item
;
1082 info
.SetBackgroundColour( col
);
1086 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1089 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1090 if ( data
&& data
->attr
)
1091 col
= data
->attr
->GetBackgroundColour();
1096 void wxListCtrl::SetItemFont( long item
, const wxFont
&f
)
1099 info
.m_itemId
= item
;
1104 wxFont
wxListCtrl::GetItemFont( long item
) const
1107 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1108 if ( data
&& data
->attr
)
1109 f
= data
->attr
->GetFont();
1114 // Gets the number of selected items in the list control
1115 int wxListCtrl::GetSelectedItemCount() const
1117 return ListView_GetSelectedCount(GetHwnd());
1120 // Gets the text colour of the listview
1121 wxColour
wxListCtrl::GetTextColour() const
1123 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1124 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1128 // Sets the text colour of the listview
1129 void wxListCtrl::SetTextColour(const wxColour
& col
)
1131 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1134 // Gets the index of the topmost visible item when in
1135 // list or report view
1136 long wxListCtrl::GetTopItem() const
1138 return (long) ListView_GetTopIndex(GetHwnd());
1141 // Searches for an item, starting from 'item'.
1142 // 'geometry' is one of
1143 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1144 // 'state' is a state bit flag, one or more of
1145 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1146 // item can be -1 to find the first item that matches the
1148 // Returns the item or -1 if unsuccessful.
1149 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1153 if ( geom
== wxLIST_NEXT_ABOVE
)
1154 flags
|= LVNI_ABOVE
;
1155 if ( geom
== wxLIST_NEXT_ALL
)
1157 if ( geom
== wxLIST_NEXT_BELOW
)
1158 flags
|= LVNI_BELOW
;
1159 if ( geom
== wxLIST_NEXT_LEFT
)
1160 flags
|= LVNI_TOLEFT
;
1161 if ( geom
== wxLIST_NEXT_RIGHT
)
1162 flags
|= LVNI_TORIGHT
;
1164 if ( state
& wxLIST_STATE_CUT
)
1166 if ( state
& wxLIST_STATE_DROPHILITED
)
1167 flags
|= LVNI_DROPHILITED
;
1168 if ( state
& wxLIST_STATE_FOCUSED
)
1169 flags
|= LVNI_FOCUSED
;
1170 if ( state
& wxLIST_STATE_SELECTED
)
1171 flags
|= LVNI_SELECTED
;
1173 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1177 wxImageList
*wxListCtrl::GetImageList(int which
) const
1179 if ( which
== wxIMAGE_LIST_NORMAL
)
1181 return m_imageListNormal
;
1183 else if ( which
== wxIMAGE_LIST_SMALL
)
1185 return m_imageListSmall
;
1187 else if ( which
== wxIMAGE_LIST_STATE
)
1189 return m_imageListState
;
1194 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1197 if ( which
== wxIMAGE_LIST_NORMAL
)
1199 flags
= LVSIL_NORMAL
;
1200 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1201 m_imageListNormal
= imageList
;
1202 m_ownsImageListNormal
= false;
1204 else if ( which
== wxIMAGE_LIST_SMALL
)
1206 flags
= LVSIL_SMALL
;
1207 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1208 m_imageListSmall
= imageList
;
1209 m_ownsImageListSmall
= false;
1211 else if ( which
== wxIMAGE_LIST_STATE
)
1213 flags
= LVSIL_STATE
;
1214 if (m_ownsImageListState
) delete m_imageListState
;
1215 m_imageListState
= imageList
;
1216 m_ownsImageListState
= false;
1218 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1221 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1223 SetImageList(imageList
, which
);
1224 if ( which
== wxIMAGE_LIST_NORMAL
)
1225 m_ownsImageListNormal
= true;
1226 else if ( which
== wxIMAGE_LIST_SMALL
)
1227 m_ownsImageListSmall
= true;
1228 else if ( which
== wxIMAGE_LIST_STATE
)
1229 m_ownsImageListState
= true;
1232 // ----------------------------------------------------------------------------
1234 // ----------------------------------------------------------------------------
1236 // Arranges the items
1237 bool wxListCtrl::Arrange(int flag
)
1240 if ( flag
== wxLIST_ALIGN_LEFT
)
1241 code
= LVA_ALIGNLEFT
;
1242 else if ( flag
== wxLIST_ALIGN_TOP
)
1243 code
= LVA_ALIGNTOP
;
1244 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1246 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1247 code
= LVA_SNAPTOGRID
;
1249 return (ListView_Arrange(GetHwnd(), code
) != 0);
1253 bool wxListCtrl::DeleteItem(long item
)
1255 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1257 wxLogLastError(_T("ListView_DeleteItem"));
1262 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1263 wxT("m_count should match ListView_GetItemCount"));
1265 // the virtual list control doesn't refresh itself correctly, help it
1268 // we need to refresh all the lines below the one which was deleted
1270 if ( item
> 0 && GetItemCount() )
1272 GetItemRect(item
- 1, rectItem
);
1277 rectItem
.height
= 0;
1280 wxRect rectWin
= GetRect();
1281 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1282 rectWin
.y
= rectItem
.GetBottom();
1284 RefreshRect(rectWin
);
1290 // Deletes all items
1291 bool wxListCtrl::DeleteAllItems()
1293 return ListView_DeleteAllItems(GetHwnd()) != 0;
1296 // Deletes all items
1297 bool wxListCtrl::DeleteAllColumns()
1299 while ( m_colCount
> 0 )
1301 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1303 wxLogLastError(wxT("ListView_DeleteColumn"));
1311 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1317 bool wxListCtrl::DeleteColumn(int col
)
1319 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1321 if ( success
&& (m_colCount
> 0) )
1326 // Clears items, and columns if there are any.
1327 void wxListCtrl::ClearAll()
1330 if ( m_colCount
> 0 )
1334 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1336 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1338 // ListView_EditLabel requires that the list has focus.
1341 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1344 // failed to start editing
1348 // [re]create the text control wrapping the HWND we got
1351 m_textCtrl
->UnsubclassWin();
1352 m_textCtrl
->SetHWND(0);
1356 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1357 m_textCtrl
->SetHWND(hWnd
);
1358 m_textCtrl
->SubclassWin(hWnd
);
1359 m_textCtrl
->SetParent(this);
1361 // we must disallow TABbing away from the control while the edit contol is
1362 // shown because this leaves it in some strange state (just try removing
1363 // this line and then pressing TAB while editing an item in listctrl
1365 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1370 // End label editing, optionally cancelling the edit
1371 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1373 wxFAIL_MSG( _T("not implemented") );
1378 // Ensures this item is visible
1379 bool wxListCtrl::EnsureVisible(long item
)
1381 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1384 // Find an item whose label matches this string, starting from the item after 'start'
1385 // or the beginning if 'start' is -1.
1386 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1388 LV_FINDINFO findInfo
;
1390 findInfo
.flags
= LVFI_STRING
;
1392 findInfo
.flags
|= LVFI_PARTIAL
;
1395 // ListView_FindItem() excludes the first item from search and to look
1396 // through all the items you need to start from -1 which is unnatural and
1397 // inconsistent with the generic version - so we adjust the index
1400 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1403 // Find an item whose data matches this data, starting from the item after 'start'
1404 // or the beginning if 'start' is -1.
1405 // NOTE : Lindsay Mathieson - 14-July-2002
1406 // No longer use ListView_FindItem as the data attribute is now stored
1407 // in a wxListItemInternalData structure refernced by the actual lParam
1408 long wxListCtrl::FindItem(long start
, wxUIntPtr data
)
1410 long idx
= start
+ 1;
1411 long count
= GetItemCount();
1415 if (GetItemData(idx
) == data
)
1423 // Find an item nearest this position in the specified direction, starting from
1424 // the item after 'start' or the beginning if 'start' is -1.
1425 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1427 LV_FINDINFO findInfo
;
1429 findInfo
.flags
= LVFI_NEARESTXY
;
1430 findInfo
.pt
.x
= pt
.x
;
1431 findInfo
.pt
.y
= pt
.y
;
1432 findInfo
.vkDirection
= VK_RIGHT
;
1434 if ( direction
== wxLIST_FIND_UP
)
1435 findInfo
.vkDirection
= VK_UP
;
1436 else if ( direction
== wxLIST_FIND_DOWN
)
1437 findInfo
.vkDirection
= VK_DOWN
;
1438 else if ( direction
== wxLIST_FIND_LEFT
)
1439 findInfo
.vkDirection
= VK_LEFT
;
1440 else if ( direction
== wxLIST_FIND_RIGHT
)
1441 findInfo
.vkDirection
= VK_RIGHT
;
1443 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1446 // Determines which item (if any) is at the specified point,
1447 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1448 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1450 LV_HITTESTINFO hitTestInfo
;
1451 hitTestInfo
.pt
.x
= (int) point
.x
;
1452 hitTestInfo
.pt
.y
= (int) point
.y
;
1454 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1458 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1459 flags
|= wxLIST_HITTEST_ABOVE
;
1460 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1461 flags
|= wxLIST_HITTEST_BELOW
;
1462 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1463 flags
|= wxLIST_HITTEST_TOLEFT
;
1464 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1465 flags
|= wxLIST_HITTEST_TORIGHT
;
1467 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1468 flags
|= wxLIST_HITTEST_NOWHERE
;
1470 // note a bug or at least a very strange feature of comtl32.dll (tested
1471 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1472 // the right of the item label, ListView_HitTest() returns a combination of
1473 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1474 // the bits which don't make sense
1475 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1477 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1479 // do not translate LVHT_ONITEMICON here, as per above
1483 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1484 flags
|= wxLIST_HITTEST_ONITEMICON
;
1485 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1486 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1489 return (long) hitTestInfo
.iItem
;
1492 // Inserts an item, returning the index of the new item if successful,
1494 long wxListCtrl::InsertItem(wxListItem
& info
)
1496 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1499 wxConvertToMSWListItem(this, info
, item
);
1500 item
.mask
&= ~LVIF_PARAM
;
1502 // check wether we need to allocate our internal data
1503 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1504 if (needInternalData
)
1506 m_AnyInternalData
= true;
1507 item
.mask
|= LVIF_PARAM
;
1509 // internal stucture that manages data
1510 wxListItemInternalData
*data
= new wxListItemInternalData();
1511 item
.lParam
= (LPARAM
) data
;
1513 if (info
.m_mask
& wxLIST_MASK_DATA
)
1514 data
->lParam
= info
.m_data
;
1516 // check whether it has any custom attributes
1517 if ( info
.HasAttributes() )
1519 // take copy of attributes
1520 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1522 // and remember that we have some now...
1523 m_hasAnyAttr
= true;
1527 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1530 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1531 wxT("m_count should match ListView_GetItemCount"));
1536 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1539 info
.m_text
= label
;
1540 info
.m_mask
= wxLIST_MASK_TEXT
;
1541 info
.m_itemId
= index
;
1542 return InsertItem(info
);
1545 // Inserts an image item
1546 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1549 info
.m_image
= imageIndex
;
1550 info
.m_mask
= wxLIST_MASK_IMAGE
;
1551 info
.m_itemId
= index
;
1552 return InsertItem(info
);
1555 // Inserts an image/string item
1556 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1559 info
.m_image
= imageIndex
;
1560 info
.m_text
= label
;
1561 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1562 info
.m_itemId
= index
;
1563 return InsertItem(info
);
1566 // For list view mode (only), inserts a column.
1567 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1570 wxConvertToMSWListCol(col
, item
, lvCol
);
1572 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1574 // always give some width to the new column: this one is compatible
1575 // with the generic version
1576 lvCol
.mask
|= LVCF_WIDTH
;
1580 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1585 else // failed to insert?
1587 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1594 long wxListCtrl::InsertColumn(long col
,
1595 const wxString
& heading
,
1600 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1601 item
.m_text
= heading
;
1604 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1605 item
.m_width
= width
;
1607 item
.m_format
= format
;
1609 return InsertColumn(col
, item
);
1612 // scroll the control by the given number of pixels (exception: in list view,
1613 // dx is interpreted as number of columns)
1614 bool wxListCtrl::ScrollList(int dx
, int dy
)
1616 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1618 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1628 // fn is a function which takes 3 long arguments: item1, item2, data.
1629 // item1 is the long data associated with a first item (NOT the index).
1630 // item2 is the long data associated with a second item (NOT the index).
1631 // data is the same value as passed to SortItems.
1632 // The return value is a negative number if the first item should precede the second
1633 // item, a positive number of the second item should precede the first,
1634 // or zero if the two items are equivalent.
1636 // data is arbitrary data to be passed to the sort function.
1638 // Internal structures for proxying the user compare function
1639 // so that we can pass it the *real* user data
1641 // translate lParam data and call user func
1642 struct wxInternalDataSort
1644 wxListCtrlCompare user_fn
;
1648 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1650 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1652 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1653 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1655 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1656 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1658 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1662 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1664 struct wxInternalDataSort internalData
;
1665 internalData
.user_fn
= fn
;
1666 internalData
.data
= data
;
1668 // WPARAM cast is needed for mingw/cygwin
1669 if ( !ListView_SortItems(GetHwnd(),
1670 wxInternalDataCompareFunc
,
1671 (WPARAM
) &internalData
) )
1673 wxLogDebug(_T("ListView_SortItems() failed"));
1683 // ----------------------------------------------------------------------------
1684 // message processing
1685 // ----------------------------------------------------------------------------
1687 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1689 if (cmd
== EN_UPDATE
)
1691 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1692 event
.SetEventObject( this );
1693 ProcessCommand(event
);
1696 else if (cmd
== EN_KILLFOCUS
)
1698 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1699 event
.SetEventObject( this );
1700 ProcessCommand(event
);
1707 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1710 // prepare the event
1711 // -----------------
1713 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1714 event
.SetEventObject(this);
1716 wxEventType eventType
= wxEVT_NULL
;
1718 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1720 // if your compiler is as broken as this, you should really change it: this
1721 // code is needed for normal operation! #ifdef below is only useful for
1722 // automatic rebuilds which are done with a very old compiler version
1723 #ifdef HDN_BEGINTRACKA
1725 // check for messages from the header (in report view)
1726 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1728 // is it a message from the header?
1729 if ( nmhdr
->hwndFrom
== hwndHdr
)
1731 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1733 event
.m_itemIndex
= -1;
1735 switch ( nmhdr
->code
)
1737 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1738 // TRACK messages even to ANSI programs: on my system I get
1739 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1741 // work around is to simply catch both versions and hope that it
1742 // works (why should this message exist in ANSI and Unicode is
1743 // beyond me as it doesn't deal with strings at all...)
1745 // note that fr HDN_TRACK another possibility could be to use
1746 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1747 // something other than the item width changes so we'd have to
1748 // filter out the unwanted events then
1749 case HDN_BEGINTRACKA
:
1750 case HDN_BEGINTRACKW
:
1751 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1756 if ( eventType
== wxEVT_NULL
)
1757 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1762 if ( eventType
== wxEVT_NULL
)
1763 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1765 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1766 event
.m_col
= nmHDR
->iItem
;
1769 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1770 case GN_CONTEXTMENU
:
1771 #endif //__WXWINCE__
1774 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1777 // find the column clicked: we have to search for it
1778 // ourselves as the notification message doesn't provide
1781 // where did the click occur?
1783 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1784 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1785 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1787 #endif //__WXWINCE__
1788 if ( !::GetCursorPos(&ptClick
) )
1790 wxLogLastError(_T("GetCursorPos"));
1793 if ( !::ScreenToClient(GetHwnd(), &ptClick
) )
1795 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1798 event
.m_pointDrag
.x
= ptClick
.x
;
1799 event
.m_pointDrag
.y
= ptClick
.y
;
1801 int colCount
= Header_GetItemCount(hwndHdr
);
1804 for ( int col
= 0; col
< colCount
; col
++ )
1806 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1808 if ( ::PtInRect(&rect
, ptClick
) )
1818 case HDN_GETDISPINFOW
:
1819 // letting Windows XP handle this message results in mysterious
1820 // crashes in comctl32.dll seemingly because of bad message
1823 // I have no idea what is the real cause of the bug (which is,
1824 // just to make things interesting, is impossible to reproduce
1825 // reliably) but ignoring all these messages does fix it and
1826 // doesn't seem to have any negative consequences
1830 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1834 #endif // defined(HDN_BEGINTRACKA)
1835 if ( nmhdr
->hwndFrom
== GetHwnd() )
1837 // almost all messages use NM_LISTVIEW
1838 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1840 const int iItem
= nmLV
->iItem
;
1843 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1844 // ignored for efficiency. It is done here because the internal data is in the
1845 // process of being deleted so we don't want to try and access it below.
1846 if ( m_ignoreChangeMessages
&&
1847 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) ||
1848 (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)) )
1854 // If we have a valid item then check if there is a data value
1855 // associated with it and put it in the event.
1856 if ( iItem
>= 0 && iItem
< GetItemCount() )
1858 wxListItemInternalData
*internaldata
=
1859 wxGetInternalData(GetHwnd(), iItem
);
1862 event
.m_item
.m_data
= internaldata
->lParam
;
1865 bool processed
= true;
1866 switch ( nmhdr
->code
)
1868 case LVN_BEGINRDRAG
:
1869 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1873 if ( eventType
== wxEVT_NULL
)
1875 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1878 event
.m_itemIndex
= iItem
;
1879 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1880 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1883 // NB: we have to handle both *A and *W versions here because some
1884 // versions of comctl32.dll send ANSI messages even to the
1886 case LVN_BEGINLABELEDITA
:
1887 case LVN_BEGINLABELEDITW
:
1890 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1892 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1894 else // LVN_BEGINLABELEDITW
1896 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1899 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1900 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1901 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1905 case LVN_ENDLABELEDITA
:
1906 case LVN_ENDLABELEDITW
:
1909 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1911 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1913 else // LVN_ENDLABELEDITW
1915 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1918 // was editing cancelled?
1919 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1920 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1922 // don't keep a stale wxTextCtrl around
1925 // EDIT control will be deleted by the list control itself so
1926 // prevent us from deleting it as well
1927 m_textCtrl
->UnsubclassWin();
1928 m_textCtrl
->SetHWND(0);
1933 event
.SetEditCanceled(true);
1936 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1937 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1938 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1942 case LVN_COLUMNCLICK
:
1943 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1944 event
.m_itemIndex
= -1;
1945 event
.m_col
= nmLV
->iSubItem
;
1948 case LVN_DELETEALLITEMS
:
1949 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1950 event
.m_itemIndex
= -1;
1953 case LVN_DELETEITEM
:
1956 // this should be prevented by the post-processing code
1957 // below, but "just in case"
1961 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1962 event
.m_itemIndex
= iItem
;
1963 // delete the assoicated internal data
1964 wxDeleteInternalData(this, iItem
);
1967 #if WXWIN_COMPATIBILITY_2_4
1968 case LVN_SETDISPINFO
:
1970 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1971 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1972 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1977 case LVN_INSERTITEM
:
1978 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1979 event
.m_itemIndex
= iItem
;
1982 case LVN_ITEMCHANGED
:
1983 // we translate this catch all message into more interesting
1984 // (and more easy to process) wxWidgets events
1986 // first of all, we deal with the state change events only and
1987 // only for valid items (item == -1 for the virtual list
1989 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1991 // temp vars for readability
1992 const UINT stOld
= nmLV
->uOldState
;
1993 const UINT stNew
= nmLV
->uNewState
;
1995 event
.m_item
.SetId(iItem
);
1996 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1999 GetItem(event
.m_item
);
2001 // has the focus changed?
2002 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2004 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2005 event
.m_itemIndex
= iItem
;
2008 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2010 if ( eventType
!= wxEVT_NULL
)
2012 // focus and selection have both changed: send the
2013 // focus event from here and the selection one
2015 event
.SetEventType(eventType
);
2016 (void)GetEventHandler()->ProcessEvent(event
);
2018 else // no focus event to send
2020 // then need to set m_itemIndex as it wasn't done
2022 event
.m_itemIndex
= iItem
;
2025 eventType
= stNew
& LVIS_SELECTED
2026 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2027 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2031 if ( eventType
== wxEVT_NULL
)
2033 // not an interesting event for us
2041 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2042 WORD wVKey
= info
->wVKey
;
2044 // get the current selection
2045 long lItem
= GetNextItem(-1,
2047 wxLIST_STATE_SELECTED
);
2049 // <Enter> or <Space> activate the selected item if any (but
2050 // not with Shift and/or Ctrl as then they have a predefined
2051 // meaning for the list view)
2053 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2054 !(wxIsShiftDown() || wxIsCtrlDown()) )
2056 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2060 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2062 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2063 // value which should be used as is
2064 int code
= wxCharCodeMSWToWX(wVKey
);
2065 event
.m_code
= code
? code
: wVKey
;
2069 event
.m_item
.m_itemId
= lItem
;
2073 // fill the other fields too
2074 event
.m_item
.m_text
= GetItemText(lItem
);
2075 event
.m_item
.m_data
= GetItemData(lItem
);
2081 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2083 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2088 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2089 // if it happened on an item (and not on empty place)
2096 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2097 event
.m_itemIndex
= iItem
;
2098 event
.m_item
.m_text
= GetItemText(iItem
);
2099 event
.m_item
.m_data
= GetItemData(iItem
);
2102 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2103 case GN_CONTEXTMENU
:
2104 #endif //__WXWINCE__
2106 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2107 // don't do anything else
2108 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2113 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2114 LV_HITTESTINFO lvhti
;
2115 wxZeroMemory(lvhti
);
2117 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2118 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2119 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2121 #endif //__WXWINCE__
2122 ::GetCursorPos(&(lvhti
.pt
));
2123 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2124 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2126 if ( lvhti
.flags
& LVHT_ONITEM
)
2128 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2129 event
.m_itemIndex
= lvhti
.iItem
;
2130 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2131 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2136 #ifdef NM_CUSTOMDRAW
2138 *result
= OnCustomDraw(lParam
);
2141 #endif // _WIN32_IE >= 0x300
2143 case LVN_ODCACHEHINT
:
2145 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2147 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2149 // we get some really stupid cache hints like ones for
2150 // items in range 0..0 for an empty control or, after
2151 // deleting an item, for items in invalid range -- filter
2153 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2156 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2158 const long iMax
= GetItemCount();
2159 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2164 #ifdef HAVE_NMLVFINDITEM
2165 case LVN_ODFINDITEM
:
2166 // this message is only used with the virtual list control but
2167 // even there we don't want to always use it: in a control with
2168 // sufficiently big number of items (defined as > 1000 here),
2169 // accidentally pressing a key could result in hanging an
2170 // application waiting while it performs linear search
2171 if ( IsVirtual() && GetItemCount() <= 1000 )
2173 NMLVFINDITEM
* pFindInfo
= (NMLVFINDITEM
*)lParam
;
2175 // no match by default
2178 // we only handle string-based searches here
2180 // TODO: what about LVFI_PARTIAL, should we handle this?
2181 if ( !(pFindInfo
->lvfi
.flags
& LVFI_STRING
) )
2186 const wxChar
* const searchstr
= pFindInfo
->lvfi
.psz
;
2187 const size_t len
= wxStrlen(searchstr
);
2189 // this is the first item we should examine, search from it
2190 // wrapping if necessary
2191 const int startPos
= pFindInfo
->iStart
;
2192 const int maxPos
= GetItemCount();
2193 wxCHECK_MSG( startPos
<= maxPos
, false,
2194 _T("bad starting position in LVN_ODFINDITEM") );
2196 int currentPos
= startPos
;
2199 // wrap to the beginning if necessary
2200 if ( currentPos
== maxPos
)
2202 // somewhat surprizingly, LVFI_WRAP isn't set in
2203 // flags but we still should wrap
2207 // does this item begin with searchstr?
2208 if ( wxStrnicmp(searchstr
,
2209 GetItemText(currentPos
), len
) == 0 )
2211 *result
= currentPos
;
2215 while ( ++currentPos
!= startPos
);
2217 if ( *result
== -1 )
2223 SetItemState(*result
,
2224 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
,
2225 wxLIST_STATE_SELECTED
| wxLIST_STATE_FOCUSED
);
2226 EnsureVisible(*result
);
2234 #endif // HAVE_NMLVFINDITEM
2236 case LVN_GETDISPINFO
:
2239 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2241 LV_ITEM
& lvi
= info
->item
;
2242 long item
= lvi
.iItem
;
2244 if ( lvi
.mask
& LVIF_TEXT
)
2246 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2247 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2250 // see comment at the end of wxListCtrl::GetColumn()
2251 #ifdef NM_CUSTOMDRAW
2252 if ( lvi
.mask
& LVIF_IMAGE
)
2254 lvi
.iImage
= OnGetItemImage(item
);
2256 #endif // NM_CUSTOMDRAW
2258 // a little dose of healthy paranoia: as we never use
2259 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2260 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2261 _T("we don't support state callbacks yet!") );
2272 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2276 // where did this one come from?
2280 // process the event
2281 // -----------------
2283 event
.SetEventType(eventType
);
2285 bool processed
= GetEventHandler()->ProcessEvent(event
);
2289 switch ( nmhdr
->code
)
2291 case LVN_DELETEALLITEMS
:
2292 // always return true to suppress all additional LVN_DELETEITEM
2293 // notifications - this makes deleting all items from a list ctrl
2297 // also, we may free all user data now (couldn't do it before as
2298 // the user should have access to it in OnDeleteAllItems() handler)
2299 FreeAllInternalData();
2301 // the control is empty now, synchronize the cached number of items
2302 // with the real one
2306 case LVN_ENDLABELEDITA
:
2307 case LVN_ENDLABELEDITW
:
2308 // logic here is inverted compared to all the other messages
2309 *result
= event
.IsAllowed();
2311 // don't keep a stale wxTextCtrl around
2314 // EDIT control will be deleted by the list control itself so
2315 // prevent us from deleting it as well
2316 m_textCtrl
->UnsubclassWin();
2317 m_textCtrl
->SetHWND(0);
2326 *result
= !event
.IsAllowed();
2331 // see comment at the end of wxListCtrl::GetColumn()
2332 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2334 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2336 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2337 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2338 switch ( nmcd
.dwDrawStage
)
2341 // if we've got any items with non standard attributes,
2342 // notify us before painting each item
2344 // for virtual controls, always suppose that we have attributes as
2345 // there is no way to check for this
2346 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2349 case CDDS_ITEMPREPAINT
:
2351 size_t item
= (size_t)nmcd
.dwItemSpec
;
2352 if ( item
>= (size_t)GetItemCount() )
2354 // we get this message with item == 0 for an empty control,
2355 // we must ignore it as calling OnGetItemAttr() would be
2357 return CDRF_DODEFAULT
;
2360 wxListItemAttr
*attr
=
2361 IsVirtual() ? OnGetItemAttr(item
)
2362 : wxGetInternalDataAttr(this, item
);
2366 // nothing to do for this item
2367 return CDRF_DODEFAULT
;
2371 wxColour colText
, colBack
;
2372 if ( attr
->HasFont() )
2374 wxFont font
= attr
->GetFont();
2375 hFont
= (HFONT
)font
.GetResourceHandle();
2382 if ( attr
->HasTextColour() )
2384 colText
= attr
->GetTextColour();
2388 colText
= GetTextColour();
2391 if ( attr
->HasBackgroundColour() )
2393 colBack
= attr
->GetBackgroundColour();
2397 colBack
= GetBackgroundColour();
2400 lplvcd
->clrText
= wxColourToRGB(colText
);
2401 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2403 // note that if we wanted to set colours for
2404 // individual columns (subitems), we would have
2405 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2408 ::SelectObject(nmcd
.hdc
, hFont
);
2410 return CDRF_NEWFONT
;
2413 // fall through to return CDRF_DODEFAULT
2416 return CDRF_DODEFAULT
;
2420 #endif // NM_CUSTOMDRAW supported
2422 // Necessary for drawing hrules and vrules, if specified
2423 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2427 wxControl::OnPaint(event
);
2429 // Reset the device origin since it may have been set
2430 dc
.SetDeviceOrigin(0, 0);
2432 bool drawHRules
= HasFlag(wxLC_HRULES
);
2433 bool drawVRules
= HasFlag(wxLC_VRULES
);
2435 if (!InReportView() || !drawHRules
&& !drawVRules
)
2438 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2440 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2442 wxSize clientSize
= GetClientSize();
2445 int itemCount
= GetItemCount();
2449 long top
= GetTopItem();
2450 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2452 if (GetItemRect(i
, itemRect
))
2454 int cy
= itemRect
.GetTop();
2455 if (i
!= 0) // Don't draw the first one
2457 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2460 if (i
== itemCount
- 1)
2462 cy
= itemRect
.GetBottom();
2463 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2469 if (drawVRules
&& (i
> -1))
2471 wxRect firstItemRect
;
2472 GetItemRect(0, firstItemRect
);
2474 if (GetItemRect(i
, itemRect
))
2477 int x
= itemRect
.GetX();
2478 for (col
= 0; col
< GetColumnCount(); col
++)
2480 int colWidth
= GetColumnWidth(col
);
2482 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2489 wxListCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2492 if ( nMsg
== WM_PRINT
)
2494 // we should bypass our own WM_PRINT handling as we don't handle
2495 // PRF_CHILDREN flag, so leave it to the native control itself
2496 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
2500 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2503 // ----------------------------------------------------------------------------
2504 // virtual list controls
2505 // ----------------------------------------------------------------------------
2507 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2509 // this is a pure virtual function, in fact - which is not really pure
2510 // because the controls which are not virtual don't need to implement it
2511 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2513 return wxEmptyString
;
2516 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2518 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
2520 wxT("List control has an image list, OnGetItemImage should be overridden."));
2524 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2526 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2527 _T("invalid item index in OnGetItemAttr()") );
2529 // no attributes by default
2533 void wxListCtrl::SetItemCount(long count
)
2535 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2537 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2538 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2540 wxLogLastError(_T("ListView_SetItemCount"));
2543 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2544 wxT("m_count should match ListView_GetItemCount"));
2547 void wxListCtrl::RefreshItem(long item
)
2549 // strangely enough, ListView_Update() results in much more flicker here
2550 // than a dumb Refresh() -- why?
2552 if ( !ListView_Update(GetHwnd(), item
) )
2554 wxLogLastError(_T("ListView_Update"));
2558 GetItemRect(item
, rect
);
2563 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2565 wxRect rect1
, rect2
;
2566 GetItemRect(itemFrom
, rect1
);
2567 GetItemRect(itemTo
, rect2
);
2569 wxRect rect
= rect1
;
2570 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2575 // ----------------------------------------------------------------------------
2576 // internal data stuff
2577 // ----------------------------------------------------------------------------
2579 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2582 it
.mask
= LVIF_PARAM
;
2585 if ( !ListView_GetItem(hwnd
, &it
) )
2588 return (wxListItemInternalData
*) it
.lParam
;
2592 wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
)
2594 return wxGetInternalData(GetHwndOf(ctl
), itemId
);
2597 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2599 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2601 return data
? data
->attr
: NULL
;
2604 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2606 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2610 memset(&item
, 0, sizeof(item
));
2611 item
.iItem
= itemId
;
2612 item
.mask
= LVIF_PARAM
;
2613 item
.lParam
= (LPARAM
) 0;
2614 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2619 // ----------------------------------------------------------------------------
2620 // wxWin <-> MSW items conversions
2621 // ----------------------------------------------------------------------------
2623 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2627 wxListItemInternalData
*internaldata
=
2628 (wxListItemInternalData
*) lvItem
.lParam
;
2631 info
.m_data
= internaldata
->lParam
;
2635 info
.m_stateMask
= 0;
2636 info
.m_itemId
= lvItem
.iItem
;
2638 long oldMask
= lvItem
.mask
;
2640 bool needText
= false;
2641 if (hwndListCtrl
!= 0)
2643 if ( lvItem
.mask
& LVIF_TEXT
)
2650 lvItem
.pszText
= new wxChar
[513];
2651 lvItem
.cchTextMax
= 512;
2653 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2654 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2657 if ( lvItem
.mask
& LVIF_STATE
)
2659 info
.m_mask
|= wxLIST_MASK_STATE
;
2661 if ( lvItem
.stateMask
& LVIS_CUT
)
2663 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2664 if ( lvItem
.state
& LVIS_CUT
)
2665 info
.m_state
|= wxLIST_STATE_CUT
;
2667 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2669 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2670 if ( lvItem
.state
& LVIS_DROPHILITED
)
2671 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2673 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2675 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2676 if ( lvItem
.state
& LVIS_FOCUSED
)
2677 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2679 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2681 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2682 if ( lvItem
.state
& LVIS_SELECTED
)
2683 info
.m_state
|= wxLIST_STATE_SELECTED
;
2687 if ( lvItem
.mask
& LVIF_TEXT
)
2689 info
.m_mask
|= wxLIST_MASK_TEXT
;
2690 info
.m_text
= lvItem
.pszText
;
2692 if ( lvItem
.mask
& LVIF_IMAGE
)
2694 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2695 info
.m_image
= lvItem
.iImage
;
2697 if ( lvItem
.mask
& LVIF_PARAM
)
2698 info
.m_mask
|= wxLIST_MASK_DATA
;
2699 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2700 info
.m_mask
|= wxLIST_SET_ITEM
;
2701 info
.m_col
= lvItem
.iSubItem
;
2706 delete[] lvItem
.pszText
;
2708 lvItem
.mask
= oldMask
;
2711 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2713 if (stateMask
& wxLIST_STATE_CUT
)
2715 lvItem
.stateMask
|= LVIS_CUT
;
2716 if (state
& wxLIST_STATE_CUT
)
2717 lvItem
.state
|= LVIS_CUT
;
2719 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2721 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2722 if (state
& wxLIST_STATE_DROPHILITED
)
2723 lvItem
.state
|= LVIS_DROPHILITED
;
2725 if (stateMask
& wxLIST_STATE_FOCUSED
)
2727 lvItem
.stateMask
|= LVIS_FOCUSED
;
2728 if (state
& wxLIST_STATE_FOCUSED
)
2729 lvItem
.state
|= LVIS_FOCUSED
;
2731 if (stateMask
& wxLIST_STATE_SELECTED
)
2733 lvItem
.stateMask
|= LVIS_SELECTED
;
2734 if (state
& wxLIST_STATE_SELECTED
)
2735 lvItem
.state
|= LVIS_SELECTED
;
2739 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2740 const wxListItem
& info
,
2743 lvItem
.iItem
= (int) info
.m_itemId
;
2745 lvItem
.iImage
= info
.m_image
;
2746 lvItem
.stateMask
= 0;
2749 lvItem
.iSubItem
= info
.m_col
;
2751 if (info
.m_mask
& wxLIST_MASK_STATE
)
2753 lvItem
.mask
|= LVIF_STATE
;
2755 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2758 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2760 lvItem
.mask
|= LVIF_TEXT
;
2761 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
2763 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2767 // pszText is not const, hence the cast
2768 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2769 if ( lvItem
.pszText
)
2770 lvItem
.cchTextMax
= info
.m_text
.Length();
2772 lvItem
.cchTextMax
= 0;
2775 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2776 lvItem
.mask
|= LVIF_IMAGE
;
2779 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2782 wxZeroMemory(lvCol
);
2784 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2786 lvCol
.mask
|= LVCF_TEXT
;
2787 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2790 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2792 lvCol
.mask
|= LVCF_FMT
;
2794 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2795 lvCol
.fmt
= LVCFMT_LEFT
;
2796 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2797 lvCol
.fmt
= LVCFMT_RIGHT
;
2798 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2799 lvCol
.fmt
= LVCFMT_CENTER
;
2802 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2804 lvCol
.mask
|= LVCF_WIDTH
;
2805 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2806 lvCol
.cx
= LVSCW_AUTOSIZE
;
2807 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2808 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2810 lvCol
.cx
= item
.m_width
;
2813 // see comment at the end of wxListCtrl::GetColumn()
2814 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2815 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2817 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2819 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2821 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2822 // seem to be generally nicer than on the left and the generic
2823 // version only draws them on the right (we don't have a flag to
2824 // specify the image location anyhow)
2826 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2827 // make any difference in my tests -- but maybe we should?
2828 if ( item
.m_image
!= -1 )
2829 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2831 lvCol
.iImage
= item
.m_image
;
2833 //else: it doesn't support item images anyhow
2835 #endif // _WIN32_IE >= 0x0300
2838 #endif // wxUSE_LISTCTRL