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 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // convert our state and mask flags to LV_ITEM constants
64 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
66 // convert wxListItem to LV_ITEM
67 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
68 const wxListItem
& info
, LV_ITEM
& lvItem
);
70 // convert LV_ITEM to wxListItem
71 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
73 /* const */ LV_ITEM
& lvItem
);
75 // convert our wxListItem to LV_COLUMN
76 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
79 // ----------------------------------------------------------------------------
80 // private helper classes
81 // ----------------------------------------------------------------------------
83 // We have to handle both fooW and fooA notifications in several cases
84 // because of broken comctl32.dll and/or unicows.dll. This class is used to
85 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
86 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
87 // by wxConvertToMSWListItem().
89 #define LV_ITEM_NATIVE LV_ITEMW
90 #define LV_ITEM_OTHER LV_ITEMA
92 #define LV_CONV_TO_WX cMB2WX
93 #define LV_CONV_BUF wxMB2WXbuf
95 #define LV_ITEM_NATIVE LV_ITEMA
96 #define LV_ITEM_OTHER LV_ITEMW
98 #define LV_CONV_TO_WX cWC2WX
99 #define LV_CONV_BUF wxWC2WXbuf
100 #endif // Unicode/ANSI
105 // default ctor, use Init() later
106 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
108 // init without conversion
109 void Init(LV_ITEM_NATIVE
& item
)
111 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
116 // init with conversion
117 void Init(LV_ITEM_OTHER
& item
)
119 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
120 // point to our own m_item
122 // memcpy() can't work if the struct sizes are different
123 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
124 CodeCantWorkIfDiffSizes
);
126 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
128 // convert text from ANSI to Unicod if necessary
129 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
131 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
132 m_item
.pszText
= (wxChar
*)m_buf
->data();
136 // ctor without conversion
137 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
139 // ctor with conversion
140 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
145 ~wxLV_ITEM() { delete m_buf
; }
147 // conversion to the real LV_ITEM
148 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
153 LV_ITEM_NATIVE
*m_pItem
;
154 LV_ITEM_NATIVE m_item
;
156 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
159 ///////////////////////////////////////////////////////
161 // The MSW version had problems with SetTextColour() et
162 // al as the wxListItemAttr's were stored keyed on the
163 // item index. If a item was inserted anywhere but the end
164 // of the list the the text attributes (colour etc) for
165 // the following items were out of sync.
168 // Under MSW the only way to associate data with a List
169 // item independant of its position in the list is to
170 // store a pointer to it in its lParam attribute. However
171 // user programs are already using this (via the
172 // SetItemData() GetItemData() calls).
174 // However what we can do is store a pointer to a
175 // structure which contains the attributes we want *and*
176 // a lParam for the users data, e.g.
178 // class wxListItemInternalData
181 // wxListItemAttr *attr;
182 // long lParam; // user data
185 // To conserve memory, a wxListItemInternalData is
186 // only allocated for a LV_ITEM if text attributes or
187 // user data(lparam) are being set.
190 // class wxListItemInternalData
191 class wxListItemInternalData
194 wxListItemAttr
*attr
;
195 LPARAM lParam
; // user data
197 wxListItemInternalData() : attr(NULL
), lParam(0) {}
198 ~wxListItemInternalData()
204 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
207 // Get the internal data structure
208 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
209 static wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
);
210 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
211 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
237 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
238 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
239 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
241 #if wxUSE_EXTENDED_RTTI
242 WX_DEFINE_FLAGS( wxListCtrlStyle
)
244 wxBEGIN_FLAGS( wxListCtrlStyle
)
245 // new style border flags, we put them first to
246 // use them for streaming out
247 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
248 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
249 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
250 wxFLAGS_MEMBER(wxBORDER_RAISED
)
251 wxFLAGS_MEMBER(wxBORDER_STATIC
)
252 wxFLAGS_MEMBER(wxBORDER_NONE
)
254 // old style border flags
255 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
256 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
257 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
258 wxFLAGS_MEMBER(wxRAISED_BORDER
)
259 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
260 wxFLAGS_MEMBER(wxBORDER
)
262 // standard window styles
263 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
264 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
265 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
266 wxFLAGS_MEMBER(wxWANTS_CHARS
)
267 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
268 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
269 wxFLAGS_MEMBER(wxVSCROLL
)
270 wxFLAGS_MEMBER(wxHSCROLL
)
272 wxFLAGS_MEMBER(wxLC_LIST
)
273 wxFLAGS_MEMBER(wxLC_REPORT
)
274 wxFLAGS_MEMBER(wxLC_ICON
)
275 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
276 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
277 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
278 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
279 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
280 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
281 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
282 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
283 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
284 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
285 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
287 wxEND_FLAGS( wxListCtrlStyle
)
289 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
291 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
292 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
294 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
295 wxEND_PROPERTIES_TABLE()
297 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
298 wxEND_HANDLERS_TABLE()
300 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
303 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
306 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
309 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
310 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
312 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
314 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
315 EVT_PAINT(wxListCtrl::OnPaint
)
318 // ============================================================================
320 // ============================================================================
322 // ----------------------------------------------------------------------------
323 // wxListCtrl construction
324 // ----------------------------------------------------------------------------
326 void wxListCtrl::Init()
328 m_imageListNormal
= NULL
;
329 m_imageListSmall
= NULL
;
330 m_imageListState
= NULL
;
331 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= false;
334 m_ignoreChangeMessages
= false;
336 m_AnyInternalData
= false;
337 m_hasAnyAttr
= false;
340 bool wxListCtrl::Create(wxWindow
*parent
,
345 const wxValidator
& validator
,
346 const wxString
& name
)
348 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
351 if ( !MSWCreateControl(WC_LISTVIEW
, _T(""), pos
, size
) )
354 // explicitly say that we want to use Unicode because otherwise we get ANSI
355 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
356 wxSetCCUnicodeFormat(GetHwnd());
358 // for comctl32.dll v 4.70+ we want to have this attribute because it's
359 // prettier (and also because wxGTK does it like this)
360 if ( InReportView() && wxTheApp
->GetComCtl32Version() >= 470 )
362 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
363 0, LVS_EX_FULLROWSELECT
);
369 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
371 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
373 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
378 #define MAP_MODE_STYLE(wx, ms) \
379 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
380 #else // !__WXDEBUG__
381 #define MAP_MODE_STYLE(wx, ms) \
382 if ( style & (wx) ) wstyle |= (ms);
383 #endif // __WXDEBUG__
385 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
386 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
387 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
388 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
390 wxASSERT_MSG( nModes
== 1,
391 _T("wxListCtrl style should have exactly one mode bit set") );
393 #undef MAP_MODE_STYLE
395 if ( style
& wxLC_ALIGN_LEFT
)
396 wstyle
|= LVS_ALIGNLEFT
;
398 if ( style
& wxLC_ALIGN_TOP
)
399 wstyle
|= LVS_ALIGNTOP
;
401 if ( style
& wxLC_AUTOARRANGE
)
402 wstyle
|= LVS_AUTOARRANGE
;
404 if ( style
& wxLC_NO_SORT_HEADER
)
405 wstyle
|= LVS_NOSORTHEADER
;
407 if ( style
& wxLC_NO_HEADER
)
408 wstyle
|= LVS_NOCOLUMNHEADER
;
410 if ( style
& wxLC_EDIT_LABELS
)
411 wstyle
|= LVS_EDITLABELS
;
413 if ( style
& wxLC_SINGLE_SEL
)
414 wstyle
|= LVS_SINGLESEL
;
416 if ( style
& wxLC_SORT_ASCENDING
)
418 wstyle
|= LVS_SORTASCENDING
;
420 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
421 _T("can't sort in ascending and descending orders at once") );
423 else if ( style
& wxLC_SORT_DESCENDING
)
424 wstyle
|= LVS_SORTDESCENDING
;
426 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
427 if ( style
& wxLC_VIRTUAL
)
429 int ver
= wxTheApp
->GetComCtl32Version();
432 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."),
433 ver
/ 100, ver
% 100);
436 wstyle
|= LVS_OWNERDATA
;
438 #endif // ancient cygwin
443 void wxListCtrl::UpdateStyle()
447 // The new window view style
448 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
450 // some styles are not returned by MSWGetStyle()
452 dwStyleNew
|= WS_VISIBLE
;
454 // Get the current window style.
455 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
457 // we don't have wxVSCROLL style, but the list control may have it,
458 // don't change it then
459 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
461 // Only set the window style if the view bits have changed.
462 if ( dwStyleOld
!= dwStyleNew
)
464 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
469 void wxListCtrl::FreeAllInternalData()
471 if (m_AnyInternalData
)
473 int n
= GetItemCount();
475 m_ignoreChangeMessages
= true;
476 for (int i
= 0; i
< n
; i
++)
477 wxDeleteInternalData(this, i
);
478 m_ignoreChangeMessages
= false;
480 m_AnyInternalData
= false;
484 wxListCtrl::~wxListCtrl()
486 FreeAllInternalData();
490 m_textCtrl
->UnsubclassWin();
491 m_textCtrl
->SetHWND(0);
496 if (m_ownsImageListNormal
) delete m_imageListNormal
;
497 if (m_ownsImageListSmall
) delete m_imageListSmall
;
498 if (m_ownsImageListState
) delete m_imageListState
;
501 // ----------------------------------------------------------------------------
502 // set/get/change style
503 // ----------------------------------------------------------------------------
505 // Add or remove a single window style
506 void wxListCtrl::SetSingleStyle(long style
, bool add
)
508 long flag
= GetWindowStyleFlag();
510 // Get rid of conflicting styles
513 if ( style
& wxLC_MASK_TYPE
)
514 flag
= flag
& ~wxLC_MASK_TYPE
;
515 if ( style
& wxLC_MASK_ALIGN
)
516 flag
= flag
& ~wxLC_MASK_ALIGN
;
517 if ( style
& wxLC_MASK_SORT
)
518 flag
= flag
& ~wxLC_MASK_SORT
;
526 SetWindowStyleFlag(flag
);
529 // Set the whole window style
530 void wxListCtrl::SetWindowStyleFlag(long flag
)
532 if ( flag
!= m_windowStyle
)
534 m_windowStyle
= flag
;
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 /* static */ wxVisualAttributes
547 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
549 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
551 // common controls have their own default font
552 attrs
.font
= wxGetCCDefaultFont();
557 // Sets the foreground, i.e. text, colour
558 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
560 if ( !wxWindow::SetForegroundColour(col
) )
563 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
568 // Sets the background colour
569 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
571 if ( !wxWindow::SetBackgroundColour(col
) )
574 // we set the same colour for both the "empty" background and the items
576 COLORREF color
= wxColourToRGB(col
);
577 ListView_SetBkColor(GetHwnd(), color
);
578 ListView_SetTextBkColor(GetHwnd(), color
);
583 // Gets information about this column
584 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
589 lvCol
.mask
= LVCF_WIDTH
;
591 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
593 lvCol
.mask
|= LVCF_TEXT
;
594 lvCol
.pszText
= new wxChar
[513];
595 lvCol
.cchTextMax
= 512;
598 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
600 lvCol
.mask
|= LVCF_FMT
;
603 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
605 lvCol
.mask
|= LVCF_IMAGE
;
608 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
610 // item.m_subItem = lvCol.iSubItem;
611 item
.m_width
= lvCol
.cx
;
613 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
615 item
.m_text
= lvCol
.pszText
;
616 delete[] lvCol
.pszText
;
619 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
621 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
623 item
.m_format
= wxLIST_FORMAT_LEFT
;
626 item
.m_format
= wxLIST_FORMAT_RIGHT
;
629 item
.m_format
= wxLIST_FORMAT_CENTRE
;
632 item
.m_format
= -1; // Unknown?
637 // the column images were not supported in older versions but how to check
638 // for this? we can't use _WIN32_IE because we always define it to a very
639 // high value, so see if another symbol which is only defined starting from
640 // comctl32.dll 4.70 is available
641 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
642 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
644 item
.m_image
= lvCol
.iImage
;
646 #endif // LVCOLUMN::iImage exists
651 // Sets information about this column
652 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
655 wxConvertToMSWListCol(col
, item
, lvCol
);
657 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
660 // Gets the column width
661 int wxListCtrl::GetColumnWidth(int col
) const
663 return ListView_GetColumnWidth(GetHwnd(), col
);
666 // Sets the column width
667 bool wxListCtrl::SetColumnWidth(int col
, int width
)
670 if ( m_windowStyle
& wxLC_LIST
)
674 if ( width2
== wxLIST_AUTOSIZE
)
675 width2
= LVSCW_AUTOSIZE
;
676 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
677 width2
= LVSCW_AUTOSIZE_USEHEADER
;
679 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
682 // Gets the number of items that can fit vertically in the
683 // visible area of the list control (list or report view)
684 // or the total number of items in the list control (icon
685 // or small icon view)
686 int wxListCtrl::GetCountPerPage() const
688 return ListView_GetCountPerPage(GetHwnd());
691 // Gets the edit control for editing labels.
692 wxTextCtrl
* wxListCtrl::GetEditControl() const
697 // Gets information about the item
698 bool wxListCtrl::GetItem(wxListItem
& info
) const
701 wxZeroMemory(lvItem
);
703 lvItem
.iItem
= info
.m_itemId
;
704 lvItem
.iSubItem
= info
.m_col
;
706 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
708 lvItem
.mask
|= LVIF_TEXT
;
709 lvItem
.pszText
= new wxChar
[513];
710 lvItem
.cchTextMax
= 512;
714 lvItem
.pszText
= NULL
;
717 if (info
.m_mask
& wxLIST_MASK_DATA
)
718 lvItem
.mask
|= LVIF_PARAM
;
720 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
721 lvItem
.mask
|= LVIF_IMAGE
;
723 if ( info
.m_mask
& wxLIST_MASK_STATE
)
725 lvItem
.mask
|= LVIF_STATE
;
726 // the other bits are hardly interesting anyhow
727 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
730 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
733 wxLogError(_("Couldn't retrieve information about list control item %d."),
738 // give NULL as hwnd as we already have everything we need
739 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
743 delete[] lvItem
.pszText
;
748 // Sets information about the item
749 bool wxListCtrl::SetItem(wxListItem
& info
)
752 wxConvertToMSWListItem(this, info
, item
);
754 // we never update the lParam if it contains our pointer
755 // to the wxListItemInternalData structure
756 item
.mask
&= ~LVIF_PARAM
;
758 // check if setting attributes or lParam
759 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
761 // get internal item data
762 // perhaps a cache here ?
763 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
768 m_AnyInternalData
= true;
769 data
= new wxListItemInternalData();
770 item
.lParam
= (LPARAM
) data
;
771 item
.mask
|= LVIF_PARAM
;
776 if (info
.m_mask
& wxLIST_MASK_DATA
)
777 data
->lParam
= info
.m_data
;
780 if (info
.HasAttributes())
783 *data
->attr
= *info
.GetAttributes();
785 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
790 // we could be changing only the attribute in which case we don't need to
791 // call ListView_SetItem() at all
795 if ( !ListView_SetItem(GetHwnd(), &item
) )
797 wxLogDebug(_T("ListView_SetItem() failed"));
803 // we need to update the item immediately to show the new image
804 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
806 // check whether it has any custom attributes
807 if ( info
.HasAttributes() )
811 // if the colour has changed, we must redraw the item
817 // we need this to make the change visible right now
818 RefreshItem(item
.iItem
);
824 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
828 info
.m_mask
= wxLIST_MASK_TEXT
;
829 info
.m_itemId
= index
;
833 info
.m_image
= imageId
;
834 info
.m_mask
|= wxLIST_MASK_IMAGE
;
836 return SetItem(info
);
840 // Gets the item state
841 int wxListCtrl::GetItemState(long item
, long stateMask
) const
845 info
.m_mask
= wxLIST_MASK_STATE
;
846 info
.m_stateMask
= stateMask
;
847 info
.m_itemId
= item
;
855 // Sets the item state
856 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
858 // NB: don't use SetItem() here as it doesn't work with the virtual list
861 wxZeroMemory(lvItem
);
863 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
865 // for the virtual list controls we need to refresh the previously focused
866 // item manually when changing focus without changing selection
867 // programmatically because otherwise it keeps its focus rectangle until
868 // next repaint (yet another comctl32 bug)
871 (stateMask
& wxLIST_STATE_FOCUSED
) &&
872 (state
& wxLIST_STATE_FOCUSED
) )
874 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
881 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
882 (WPARAM
)item
, (LPARAM
)&lvItem
) )
884 wxLogLastError(_T("ListView_SetItemState"));
889 if ( focusOld
!= -1 )
891 // no need to refresh the item if it was previously selected, it would
892 // only result in annoying flicker
893 if ( !(GetItemState(focusOld
,
894 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
896 RefreshItem(focusOld
);
903 // Sets the item image
904 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
908 info
.m_mask
= wxLIST_MASK_IMAGE
;
909 info
.m_image
= image
;
910 info
.m_itemId
= item
;
912 return SetItem(info
);
915 // Gets the item text
916 wxString
wxListCtrl::GetItemText(long item
) const
920 info
.m_mask
= wxLIST_MASK_TEXT
;
921 info
.m_itemId
= item
;
924 return wxEmptyString
;
928 // Sets the item text
929 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
933 info
.m_mask
= wxLIST_MASK_TEXT
;
934 info
.m_itemId
= item
;
940 // Gets the item data
941 long wxListCtrl::GetItemData(long item
) const
945 info
.m_mask
= wxLIST_MASK_DATA
;
946 info
.m_itemId
= item
;
953 // Sets the item data
954 bool wxListCtrl::SetItemData(long item
, long data
)
958 info
.m_mask
= wxLIST_MASK_DATA
;
959 info
.m_itemId
= item
;
962 return SetItem(info
);
965 wxRect
wxListCtrl::GetViewRect() const
967 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
968 _T("wxListCtrl::GetViewRect() only works in icon mode") );
971 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
973 wxLogDebug(_T("ListView_GetViewRect() failed."));
979 wxCopyRECTToRect(rc
, rect
);
984 // Gets the item rectangle
985 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
990 if ( code
== wxLIST_RECT_BOUNDS
)
991 codeWin
= LVIR_BOUNDS
;
992 else if ( code
== wxLIST_RECT_ICON
)
994 else if ( code
== wxLIST_RECT_LABEL
)
995 codeWin
= LVIR_LABEL
;
998 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1000 codeWin
= LVIR_BOUNDS
;
1003 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1005 rect
.x
= rectWin
.left
;
1006 rect
.y
= rectWin
.top
;
1007 rect
.width
= rectWin
.right
- rectWin
.left
;
1008 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1013 // Gets the item position
1014 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1018 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1020 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1024 // Sets the item position.
1025 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1027 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1030 // Gets the number of items in the list control
1031 int wxListCtrl::GetItemCount() const
1036 wxSize
wxListCtrl::GetItemSpacing() const
1038 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1040 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1043 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1045 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1048 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1051 info
.m_itemId
= item
;
1052 info
.SetTextColour( col
);
1056 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1059 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1060 if ( data
&& data
->attr
)
1061 col
= data
->attr
->GetTextColour();
1066 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1069 info
.m_itemId
= item
;
1070 info
.SetBackgroundColour( col
);
1074 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1077 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1078 if ( data
&& data
->attr
)
1079 col
= data
->attr
->GetBackgroundColour();
1084 // Gets the number of selected items in the list control
1085 int wxListCtrl::GetSelectedItemCount() const
1087 return ListView_GetSelectedCount(GetHwnd());
1090 // Gets the text colour of the listview
1091 wxColour
wxListCtrl::GetTextColour() const
1093 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1094 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1098 // Sets the text colour of the listview
1099 void wxListCtrl::SetTextColour(const wxColour
& col
)
1101 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1104 // Gets the index of the topmost visible item when in
1105 // list or report view
1106 long wxListCtrl::GetTopItem() const
1108 return (long) ListView_GetTopIndex(GetHwnd());
1111 // Searches for an item, starting from 'item'.
1112 // 'geometry' is one of
1113 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1114 // 'state' is a state bit flag, one or more of
1115 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1116 // item can be -1 to find the first item that matches the
1118 // Returns the item or -1 if unsuccessful.
1119 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1123 if ( geom
== wxLIST_NEXT_ABOVE
)
1124 flags
|= LVNI_ABOVE
;
1125 if ( geom
== wxLIST_NEXT_ALL
)
1127 if ( geom
== wxLIST_NEXT_BELOW
)
1128 flags
|= LVNI_BELOW
;
1129 if ( geom
== wxLIST_NEXT_LEFT
)
1130 flags
|= LVNI_TOLEFT
;
1131 if ( geom
== wxLIST_NEXT_RIGHT
)
1132 flags
|= LVNI_TORIGHT
;
1134 if ( state
& wxLIST_STATE_CUT
)
1136 if ( state
& wxLIST_STATE_DROPHILITED
)
1137 flags
|= LVNI_DROPHILITED
;
1138 if ( state
& wxLIST_STATE_FOCUSED
)
1139 flags
|= LVNI_FOCUSED
;
1140 if ( state
& wxLIST_STATE_SELECTED
)
1141 flags
|= LVNI_SELECTED
;
1143 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1147 wxImageList
*wxListCtrl::GetImageList(int which
) const
1149 if ( which
== wxIMAGE_LIST_NORMAL
)
1151 return m_imageListNormal
;
1153 else if ( which
== wxIMAGE_LIST_SMALL
)
1155 return m_imageListSmall
;
1157 else if ( which
== wxIMAGE_LIST_STATE
)
1159 return m_imageListState
;
1164 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1167 if ( which
== wxIMAGE_LIST_NORMAL
)
1169 flags
= LVSIL_NORMAL
;
1170 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1171 m_imageListNormal
= imageList
;
1172 m_ownsImageListNormal
= false;
1174 else if ( which
== wxIMAGE_LIST_SMALL
)
1176 flags
= LVSIL_SMALL
;
1177 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1178 m_imageListSmall
= imageList
;
1179 m_ownsImageListSmall
= false;
1181 else if ( which
== wxIMAGE_LIST_STATE
)
1183 flags
= LVSIL_STATE
;
1184 if (m_ownsImageListState
) delete m_imageListState
;
1185 m_imageListState
= imageList
;
1186 m_ownsImageListState
= false;
1188 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1191 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1193 SetImageList(imageList
, which
);
1194 if ( which
== wxIMAGE_LIST_NORMAL
)
1195 m_ownsImageListNormal
= true;
1196 else if ( which
== wxIMAGE_LIST_SMALL
)
1197 m_ownsImageListSmall
= true;
1198 else if ( which
== wxIMAGE_LIST_STATE
)
1199 m_ownsImageListState
= true;
1202 // ----------------------------------------------------------------------------
1204 // ----------------------------------------------------------------------------
1206 // Arranges the items
1207 bool wxListCtrl::Arrange(int flag
)
1210 if ( flag
== wxLIST_ALIGN_LEFT
)
1211 code
= LVA_ALIGNLEFT
;
1212 else if ( flag
== wxLIST_ALIGN_TOP
)
1213 code
= LVA_ALIGNTOP
;
1214 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1216 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1217 code
= LVA_SNAPTOGRID
;
1219 return (ListView_Arrange(GetHwnd(), code
) != 0);
1223 bool wxListCtrl::DeleteItem(long item
)
1225 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1227 wxLogLastError(_T("ListView_DeleteItem"));
1232 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1233 wxT("m_count should match ListView_GetItemCount"));
1235 // the virtual list control doesn't refresh itself correctly, help it
1238 // we need to refresh all the lines below the one which was deleted
1240 if ( item
> 0 && GetItemCount() )
1242 GetItemRect(item
- 1, rectItem
);
1247 rectItem
.height
= 0;
1250 wxRect rectWin
= GetRect();
1251 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1252 rectWin
.y
= rectItem
.GetBottom();
1254 RefreshRect(rectWin
);
1260 // Deletes all items
1261 bool wxListCtrl::DeleteAllItems()
1263 FreeAllInternalData();
1264 return ListView_DeleteAllItems(GetHwnd()) != 0;
1267 // Deletes all items
1268 bool wxListCtrl::DeleteAllColumns()
1270 while ( m_colCount
> 0 )
1272 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1274 wxLogLastError(wxT("ListView_DeleteColumn"));
1282 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1288 bool wxListCtrl::DeleteColumn(int col
)
1290 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1292 if ( success
&& (m_colCount
> 0) )
1297 // Clears items, and columns if there are any.
1298 void wxListCtrl::ClearAll()
1301 if ( m_colCount
> 0 )
1305 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1307 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1309 // ListView_EditLabel requires that the list has focus.
1312 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1315 // failed to start editing
1319 // [re]create the text control wrapping the HWND we got
1322 m_textCtrl
->UnsubclassWin();
1323 m_textCtrl
->SetHWND(0);
1327 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1328 m_textCtrl
->SetHWND(hWnd
);
1329 m_textCtrl
->SubclassWin(hWnd
);
1330 m_textCtrl
->SetParent(this);
1332 // we must disallow TABbing away from the control while the edit contol is
1333 // shown because this leaves it in some strange state (just try removing
1334 // this line and then pressing TAB while editing an item in listctrl
1336 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1341 // End label editing, optionally cancelling the edit
1342 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1344 wxFAIL_MSG( _T("not implemented") );
1349 // Ensures this item is visible
1350 bool wxListCtrl::EnsureVisible(long item
)
1352 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1355 // Find an item whose label matches this string, starting from the item after 'start'
1356 // or the beginning if 'start' is -1.
1357 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1359 LV_FINDINFO findInfo
;
1361 findInfo
.flags
= LVFI_STRING
;
1363 findInfo
.flags
|= LVFI_PARTIAL
;
1366 // ListView_FindItem() excludes the first item from search and to look
1367 // through all the items you need to start from -1 which is unnatural and
1368 // inconsistent with the generic version - so we adjust the index
1371 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1374 // Find an item whose data matches this data, starting from the item after 'start'
1375 // or the beginning if 'start' is -1.
1376 // NOTE : Lindsay Mathieson - 14-July-2002
1377 // No longer use ListView_FindItem as the data attribute is now stored
1378 // in a wxListItemInternalData structure refernced by the actual lParam
1379 long wxListCtrl::FindItem(long start
, long data
)
1381 long idx
= start
+ 1;
1382 long count
= GetItemCount();
1386 if (GetItemData(idx
) == data
)
1394 // Find an item nearest this position in the specified direction, starting from
1395 // the item after 'start' or the beginning if 'start' is -1.
1396 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1398 LV_FINDINFO findInfo
;
1400 findInfo
.flags
= LVFI_NEARESTXY
;
1401 findInfo
.pt
.x
= pt
.x
;
1402 findInfo
.pt
.y
= pt
.y
;
1403 findInfo
.vkDirection
= VK_RIGHT
;
1405 if ( direction
== wxLIST_FIND_UP
)
1406 findInfo
.vkDirection
= VK_UP
;
1407 else if ( direction
== wxLIST_FIND_DOWN
)
1408 findInfo
.vkDirection
= VK_DOWN
;
1409 else if ( direction
== wxLIST_FIND_LEFT
)
1410 findInfo
.vkDirection
= VK_LEFT
;
1411 else if ( direction
== wxLIST_FIND_RIGHT
)
1412 findInfo
.vkDirection
= VK_RIGHT
;
1414 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1417 // Determines which item (if any) is at the specified point,
1418 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1419 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1421 LV_HITTESTINFO hitTestInfo
;
1422 hitTestInfo
.pt
.x
= (int) point
.x
;
1423 hitTestInfo
.pt
.y
= (int) point
.y
;
1425 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1428 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1429 flags
|= wxLIST_HITTEST_ABOVE
;
1430 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1431 flags
|= wxLIST_HITTEST_BELOW
;
1432 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1433 flags
|= wxLIST_HITTEST_NOWHERE
;
1434 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1435 flags
|= wxLIST_HITTEST_ONITEMICON
;
1436 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1437 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1438 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1439 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1440 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1441 flags
|= wxLIST_HITTEST_TOLEFT
;
1442 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1443 flags
|= wxLIST_HITTEST_TORIGHT
;
1445 return (long) hitTestInfo
.iItem
;
1448 // Inserts an item, returning the index of the new item if successful,
1450 long wxListCtrl::InsertItem(wxListItem
& info
)
1452 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1455 wxConvertToMSWListItem(this, info
, item
);
1456 item
.mask
&= ~LVIF_PARAM
;
1458 // check wether we need to allocate our internal data
1459 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1460 if (needInternalData
)
1462 m_AnyInternalData
= true;
1463 item
.mask
|= LVIF_PARAM
;
1465 // internal stucture that manages data
1466 wxListItemInternalData
*data
= new wxListItemInternalData();
1467 item
.lParam
= (LPARAM
) data
;
1469 if (info
.m_mask
& wxLIST_MASK_DATA
)
1470 data
->lParam
= info
.m_data
;
1472 // check whether it has any custom attributes
1473 if ( info
.HasAttributes() )
1475 // take copy of attributes
1476 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1478 // and remember that we have some now...
1479 m_hasAnyAttr
= true;
1483 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1486 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1487 wxT("m_count should match ListView_GetItemCount"));
1492 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1495 info
.m_text
= label
;
1496 info
.m_mask
= wxLIST_MASK_TEXT
;
1497 info
.m_itemId
= index
;
1498 return InsertItem(info
);
1501 // Inserts an image item
1502 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1505 info
.m_image
= imageIndex
;
1506 info
.m_mask
= wxLIST_MASK_IMAGE
;
1507 info
.m_itemId
= index
;
1508 return InsertItem(info
);
1511 // Inserts an image/string item
1512 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1515 info
.m_image
= imageIndex
;
1516 info
.m_text
= label
;
1517 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1518 info
.m_itemId
= index
;
1519 return InsertItem(info
);
1522 // For list view mode (only), inserts a column.
1523 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1526 wxConvertToMSWListCol(col
, item
, lvCol
);
1528 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1530 // always give some width to the new column: this one is compatible
1531 // with the generic version
1532 lvCol
.mask
|= LVCF_WIDTH
;
1536 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1541 else // failed to insert?
1543 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1550 long wxListCtrl::InsertColumn(long col
,
1551 const wxString
& heading
,
1556 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1557 item
.m_text
= heading
;
1560 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1561 item
.m_width
= width
;
1563 item
.m_format
= format
;
1565 return InsertColumn(col
, item
);
1568 // scroll the control by the given number of pixels (exception: in list view,
1569 // dx is interpreted as number of columns)
1570 bool wxListCtrl::ScrollList(int dx
, int dy
)
1572 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1574 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1584 // fn is a function which takes 3 long arguments: item1, item2, data.
1585 // item1 is the long data associated with a first item (NOT the index).
1586 // item2 is the long data associated with a second item (NOT the index).
1587 // data is the same value as passed to SortItems.
1588 // The return value is a negative number if the first item should precede the second
1589 // item, a positive number of the second item should precede the first,
1590 // or zero if the two items are equivalent.
1592 // data is arbitrary data to be passed to the sort function.
1594 // Internal structures for proxying the user compare function
1595 // so that we can pass it the *real* user data
1597 // translate lParam data and call user func
1598 struct wxInternalDataSort
1600 wxListCtrlCompare user_fn
;
1604 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1606 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1608 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1609 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1611 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1612 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1614 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1618 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1620 struct wxInternalDataSort internalData
;
1621 internalData
.user_fn
= fn
;
1622 internalData
.data
= data
;
1624 // WPARAM cast is needed for mingw/cygwin
1625 if ( !ListView_SortItems(GetHwnd(),
1626 wxInternalDataCompareFunc
,
1627 (WPARAM
) &internalData
) )
1629 wxLogDebug(_T("ListView_SortItems() failed"));
1639 // ----------------------------------------------------------------------------
1640 // message processing
1641 // ----------------------------------------------------------------------------
1643 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1645 if (cmd
== EN_UPDATE
)
1647 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1648 event
.SetEventObject( this );
1649 ProcessCommand(event
);
1652 else if (cmd
== EN_KILLFOCUS
)
1654 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1655 event
.SetEventObject( this );
1656 ProcessCommand(event
);
1663 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1666 // prepare the event
1667 // -----------------
1669 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1670 event
.SetEventObject(this);
1672 wxEventType eventType
= wxEVT_NULL
;
1674 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1676 // if your compiler is as broken as this, you should really change it: this
1677 // code is needed for normal operation! #ifdef below is only useful for
1678 // automatic rebuilds which are done with a very old compiler version
1679 #ifdef HDN_BEGINTRACKA
1681 // check for messages from the header (in report view)
1682 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1684 // is it a message from the header?
1685 if ( nmhdr
->hwndFrom
== hwndHdr
)
1687 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1689 event
.m_itemIndex
= -1;
1691 switch ( nmhdr
->code
)
1693 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1694 // TRACK messages even to ANSI programs: on my system I get
1695 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1697 // work around is to simply catch both versions and hope that it
1698 // works (why should this message exist in ANSI and Unicode is
1699 // beyond me as it doesn't deal with strings at all...)
1701 // note that fr HDN_TRACK another possibility could be to use
1702 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1703 // something other than the item width changes so we'd have to
1704 // filter out the unwanted events then
1705 case HDN_BEGINTRACKA
:
1706 case HDN_BEGINTRACKW
:
1707 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1712 if ( eventType
== wxEVT_NULL
)
1713 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1718 if ( eventType
== wxEVT_NULL
)
1719 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1721 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1722 event
.m_col
= nmHDR
->iItem
;
1725 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1726 case GN_CONTEXTMENU
:
1727 #endif //__WXWINCE__
1730 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1733 // find the column clicked: we have to search for it
1734 // ourselves as the notification message doesn't provide
1737 // where did the click occur?
1739 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1740 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1741 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1743 #endif //__WXWINCE__
1744 if ( !::GetCursorPos(&ptClick
) )
1746 wxLogLastError(_T("GetCursorPos"));
1749 if ( !::ScreenToClient(GetHwnd(), &ptClick
) )
1751 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1754 event
.m_pointDrag
.x
= ptClick
.x
;
1755 event
.m_pointDrag
.y
= ptClick
.y
;
1757 int colCount
= Header_GetItemCount(hwndHdr
);
1760 for ( int col
= 0; col
< colCount
; col
++ )
1762 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1764 if ( ::PtInRect(&rect
, ptClick
) )
1774 case HDN_GETDISPINFOW
:
1776 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1777 // This is a fix for a strange bug under XP.
1778 // Normally, info->iItem is a valid index, but
1779 // sometimes this is a silly (large) number
1780 // and when we return false via wxControl::MSWOnNotify
1781 // to indicate that it hasn't yet been processed,
1782 // there's a GPF in Windows.
1783 // By returning true here, we avoid further processing
1784 // of this strange message.
1785 if ( (unsigned)info
->iItem
>= (unsigned)GetColumnCount() )
1791 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1795 #endif // defined(HDN_BEGINTRACKA)
1796 if ( nmhdr
->hwndFrom
== GetHwnd() )
1798 // almost all messages use NM_LISTVIEW
1799 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1801 const int iItem
= nmLV
->iItem
;
1804 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1805 // ignored for efficiency. It is done here because the internal data is in the
1806 // process of being deleted so we don't want to try and access it below.
1807 if ( m_ignoreChangeMessages
&&
1808 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1814 // If we have a valid item then check if there is a data value
1815 // associated with it and put it in the event.
1816 if ( iItem
>= 0 && iItem
< GetItemCount() )
1818 wxListItemInternalData
*internaldata
=
1819 wxGetInternalData(GetHwnd(), iItem
);
1822 event
.m_item
.m_data
= internaldata
->lParam
;
1826 switch ( nmhdr
->code
)
1828 case LVN_BEGINRDRAG
:
1829 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1833 if ( eventType
== wxEVT_NULL
)
1835 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1838 event
.m_itemIndex
= iItem
;
1839 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1840 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1843 // NB: we have to handle both *A and *W versions here because some
1844 // versions of comctl32.dll send ANSI messages even to the
1846 case LVN_BEGINLABELEDITA
:
1847 case LVN_BEGINLABELEDITW
:
1850 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1852 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1854 else // LVN_BEGINLABELEDITW
1856 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1859 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1860 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1861 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1865 case LVN_ENDLABELEDITA
:
1866 case LVN_ENDLABELEDITW
:
1869 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1871 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1873 else // LVN_ENDLABELEDITW
1875 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1878 // was editing cancelled?
1879 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1880 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1882 // don't keep a stale wxTextCtrl around
1885 // EDIT control will be deleted by the list control itself so
1886 // prevent us from deleting it as well
1887 m_textCtrl
->UnsubclassWin();
1888 m_textCtrl
->SetHWND(0);
1893 event
.SetEditCanceled(true);
1896 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1897 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1898 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1902 case LVN_COLUMNCLICK
:
1903 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1904 event
.m_itemIndex
= -1;
1905 event
.m_col
= nmLV
->iSubItem
;
1908 case LVN_DELETEALLITEMS
:
1910 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1911 event
.m_itemIndex
= -1;
1914 case LVN_DELETEITEM
:
1916 // this should be prevented by the post-processing code below,
1917 // but "just in case"
1920 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1921 event
.m_itemIndex
= iItem
;
1922 // delete the assoicated internal data
1923 wxDeleteInternalData(this, iItem
);
1926 case LVN_SETDISPINFO
:
1928 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1929 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1930 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1934 case LVN_INSERTITEM
:
1935 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1936 event
.m_itemIndex
= iItem
;
1939 case LVN_ITEMCHANGED
:
1940 // we translate this catch all message into more interesting
1941 // (and more easy to process) wxWidgets events
1943 // first of all, we deal with the state change events only and
1944 // only for valid items (item == -1 for the virtual list
1946 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1948 // temp vars for readability
1949 const UINT stOld
= nmLV
->uOldState
;
1950 const UINT stNew
= nmLV
->uNewState
;
1952 event
.m_item
.SetId(iItem
);
1953 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1956 GetItem(event
.m_item
);
1958 // has the focus changed?
1959 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1961 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1962 event
.m_itemIndex
= iItem
;
1965 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1967 if ( eventType
!= wxEVT_NULL
)
1969 // focus and selection have both changed: send the
1970 // focus event from here and the selection one
1972 event
.SetEventType(eventType
);
1973 (void)GetEventHandler()->ProcessEvent(event
);
1975 else // no focus event to send
1977 // then need to set m_itemIndex as it wasn't done
1979 event
.m_itemIndex
= iItem
;
1982 eventType
= stNew
& LVIS_SELECTED
1983 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1984 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1988 if ( eventType
== wxEVT_NULL
)
1990 // not an interesting event for us
1998 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1999 WORD wVKey
= info
->wVKey
;
2001 // get the current selection
2002 long lItem
= GetNextItem(-1,
2004 wxLIST_STATE_SELECTED
);
2006 // <Enter> or <Space> activate the selected item if any (but
2007 // not with Shift and/or Ctrl as then they have a predefined
2008 // meaning for the list view)
2010 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2011 !(wxIsShiftDown() || wxIsCtrlDown()) )
2013 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2017 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2019 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2020 // value which should be used as is
2021 int code
= wxCharCodeMSWToWX(wVKey
);
2022 event
.m_code
= code
? code
: wVKey
;
2026 event
.m_item
.m_itemId
= lItem
;
2030 // fill the other fields too
2031 event
.m_item
.m_text
= GetItemText(lItem
);
2032 event
.m_item
.m_data
= GetItemData(lItem
);
2038 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2040 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2045 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2046 // if it happened on an item (and not on empty place)
2053 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2054 event
.m_itemIndex
= iItem
;
2055 event
.m_item
.m_text
= GetItemText(iItem
);
2056 event
.m_item
.m_data
= GetItemData(iItem
);
2059 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2060 case GN_CONTEXTMENU
:
2061 #endif //__WXWINCE__
2063 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2064 // don't do anything else
2065 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2070 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2071 LV_HITTESTINFO lvhti
;
2072 wxZeroMemory(lvhti
);
2074 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2075 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2076 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2078 #endif //__WXWINCE__
2079 ::GetCursorPos(&(lvhti
.pt
));
2080 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2081 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2083 if ( lvhti
.flags
& LVHT_ONITEM
)
2085 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2086 event
.m_itemIndex
= lvhti
.iItem
;
2087 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2088 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2093 #ifdef NM_CUSTOMDRAW
2095 *result
= OnCustomDraw(lParam
);
2098 #endif // _WIN32_IE >= 0x300
2100 case LVN_ODCACHEHINT
:
2102 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2104 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2106 // we get some really stupid cache hints like ones for
2107 // items in range 0..0 for an empty control or, after
2108 // deleting an item, for items in invalid range -- filter
2110 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2113 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2115 const long iMax
= GetItemCount();
2116 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2121 case LVN_GETDISPINFO
:
2124 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2126 LV_ITEM
& lvi
= info
->item
;
2127 long item
= lvi
.iItem
;
2129 if ( lvi
.mask
& LVIF_TEXT
)
2131 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2132 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2135 // see comment at the end of wxListCtrl::GetColumn()
2136 #ifdef NM_CUSTOMDRAW
2137 if ( lvi
.mask
& LVIF_IMAGE
)
2139 lvi
.iImage
= OnGetItemImage(item
);
2141 #endif // NM_CUSTOMDRAW
2143 // a little dose of healthy paranoia: as we never use
2144 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2145 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2146 _T("we don't support state callbacks yet!") );
2153 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2158 // where did this one come from?
2162 // process the event
2163 // -----------------
2165 event
.SetEventType(eventType
);
2167 bool processed
= GetEventHandler()->ProcessEvent(event
);
2171 switch ( nmhdr
->code
)
2173 case LVN_DELETEALLITEMS
:
2174 // always return true to suppress all additional LVN_DELETEITEM
2175 // notifications - this makes deleting all items from a list ctrl
2180 case LVN_ENDLABELEDITA
:
2181 case LVN_ENDLABELEDITW
:
2182 // logic here is inversed compared to all the other messages
2183 *result
= event
.IsAllowed();
2185 // don't keep a stale wxTextCtrl around
2188 // EDIT control will be deleted by the list control itself so
2189 // prevent us from deleting it as well
2190 m_textCtrl
->UnsubclassWin();
2191 m_textCtrl
->SetHWND(0);
2200 *result
= !event
.IsAllowed();
2205 // see comment at the end of wxListCtrl::GetColumn()
2206 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2208 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2210 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2211 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2212 switch ( nmcd
.dwDrawStage
)
2215 // if we've got any items with non standard attributes,
2216 // notify us before painting each item
2218 // for virtual controls, always suppose that we have attributes as
2219 // there is no way to check for this
2220 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2223 case CDDS_ITEMPREPAINT
:
2225 size_t item
= (size_t)nmcd
.dwItemSpec
;
2226 if ( item
>= (size_t)GetItemCount() )
2228 // we get this message with item == 0 for an empty control,
2229 // we must ignore it as calling OnGetItemAttr() would be
2231 return CDRF_DODEFAULT
;
2234 wxListItemAttr
*attr
=
2235 IsVirtual() ? OnGetItemAttr(item
)
2236 : wxGetInternalDataAttr(this, item
);
2240 // nothing to do for this item
2241 return CDRF_DODEFAULT
;
2245 wxColour colText
, colBack
;
2246 if ( attr
->HasFont() )
2248 wxFont font
= attr
->GetFont();
2249 hFont
= (HFONT
)font
.GetResourceHandle();
2256 if ( attr
->HasTextColour() )
2258 colText
= attr
->GetTextColour();
2262 colText
= GetTextColour();
2265 if ( attr
->HasBackgroundColour() )
2267 colBack
= attr
->GetBackgroundColour();
2271 colBack
= GetBackgroundColour();
2274 lplvcd
->clrText
= wxColourToRGB(colText
);
2275 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2277 // note that if we wanted to set colours for
2278 // individual columns (subitems), we would have
2279 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2282 ::SelectObject(nmcd
.hdc
, hFont
);
2284 return CDRF_NEWFONT
;
2287 // fall through to return CDRF_DODEFAULT
2290 return CDRF_DODEFAULT
;
2294 #endif // NM_CUSTOMDRAW supported
2296 // Necessary for drawing hrules and vrules, if specified
2297 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2301 wxControl::OnPaint(event
);
2303 // Reset the device origin since it may have been set
2304 dc
.SetDeviceOrigin(0, 0);
2306 bool drawHRules
= HasFlag(wxLC_HRULES
);
2307 bool drawVRules
= HasFlag(wxLC_VRULES
);
2309 if (!InReportView() || !drawHRules
&& !drawVRules
)
2312 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2314 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2316 wxSize clientSize
= GetClientSize();
2319 int itemCount
= GetItemCount();
2323 long top
= GetTopItem();
2324 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2326 if (GetItemRect(i
, itemRect
))
2328 int cy
= itemRect
.GetTop();
2329 if (i
!= 0) // Don't draw the first one
2331 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2334 if (i
== itemCount
- 1)
2336 cy
= itemRect
.GetBottom();
2337 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2343 if (drawVRules
&& (i
> -1))
2345 wxRect firstItemRect
;
2346 GetItemRect(0, firstItemRect
);
2348 if (GetItemRect(i
, itemRect
))
2351 int x
= itemRect
.GetX();
2352 for (col
= 0; col
< GetColumnCount(); col
++)
2354 int colWidth
= GetColumnWidth(col
);
2356 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2362 // ----------------------------------------------------------------------------
2363 // virtual list controls
2364 // ----------------------------------------------------------------------------
2366 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2368 // this is a pure virtual function, in fact - which is not really pure
2369 // because the controls which are not virtual don't need to implement it
2370 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2372 return wxEmptyString
;
2375 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2378 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2383 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2385 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2386 _T("invalid item index in OnGetItemAttr()") );
2388 // no attributes by default
2392 void wxListCtrl::SetItemCount(long count
)
2394 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2396 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2397 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2399 wxLogLastError(_T("ListView_SetItemCount"));
2402 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2403 wxT("m_count should match ListView_GetItemCount"));
2406 void wxListCtrl::RefreshItem(long item
)
2408 // strangely enough, ListView_Update() results in much more flicker here
2409 // than a dumb Refresh() -- why?
2411 if ( !ListView_Update(GetHwnd(), item
) )
2413 wxLogLastError(_T("ListView_Update"));
2417 GetItemRect(item
, rect
);
2422 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2424 wxRect rect1
, rect2
;
2425 GetItemRect(itemFrom
, rect1
);
2426 GetItemRect(itemTo
, rect2
);
2428 wxRect rect
= rect1
;
2429 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2434 // ----------------------------------------------------------------------------
2435 // internal data stuff
2436 // ----------------------------------------------------------------------------
2438 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2441 it
.mask
= LVIF_PARAM
;
2444 if ( !ListView_GetItem(hwnd
, &it
) )
2447 return (wxListItemInternalData
*) it
.lParam
;
2451 wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
)
2453 return wxGetInternalData(GetHwndOf(ctl
), itemId
);
2456 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2458 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2460 return data
? data
->attr
: NULL
;
2463 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2465 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2469 memset(&item
, 0, sizeof(item
));
2470 item
.iItem
= itemId
;
2471 item
.mask
= LVIF_PARAM
;
2472 item
.lParam
= (LPARAM
) 0;
2473 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2478 // ----------------------------------------------------------------------------
2479 // wxWin <-> MSW items conversions
2480 // ----------------------------------------------------------------------------
2482 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2486 wxListItemInternalData
*internaldata
=
2487 (wxListItemInternalData
*) lvItem
.lParam
;
2490 info
.m_data
= internaldata
->lParam
;
2494 info
.m_stateMask
= 0;
2495 info
.m_itemId
= lvItem
.iItem
;
2497 long oldMask
= lvItem
.mask
;
2499 bool needText
= false;
2500 if (hwndListCtrl
!= 0)
2502 if ( lvItem
.mask
& LVIF_TEXT
)
2509 lvItem
.pszText
= new wxChar
[513];
2510 lvItem
.cchTextMax
= 512;
2512 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2513 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2516 if ( lvItem
.mask
& LVIF_STATE
)
2518 info
.m_mask
|= wxLIST_MASK_STATE
;
2520 if ( lvItem
.stateMask
& LVIS_CUT
)
2522 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2523 if ( lvItem
.state
& LVIS_CUT
)
2524 info
.m_state
|= wxLIST_STATE_CUT
;
2526 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2528 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2529 if ( lvItem
.state
& LVIS_DROPHILITED
)
2530 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2532 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2534 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2535 if ( lvItem
.state
& LVIS_FOCUSED
)
2536 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2538 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2540 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2541 if ( lvItem
.state
& LVIS_SELECTED
)
2542 info
.m_state
|= wxLIST_STATE_SELECTED
;
2546 if ( lvItem
.mask
& LVIF_TEXT
)
2548 info
.m_mask
|= wxLIST_MASK_TEXT
;
2549 info
.m_text
= lvItem
.pszText
;
2551 if ( lvItem
.mask
& LVIF_IMAGE
)
2553 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2554 info
.m_image
= lvItem
.iImage
;
2556 if ( lvItem
.mask
& LVIF_PARAM
)
2557 info
.m_mask
|= wxLIST_MASK_DATA
;
2558 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2559 info
.m_mask
|= wxLIST_SET_ITEM
;
2560 info
.m_col
= lvItem
.iSubItem
;
2565 delete[] lvItem
.pszText
;
2567 lvItem
.mask
= oldMask
;
2570 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2572 if (stateMask
& wxLIST_STATE_CUT
)
2574 lvItem
.stateMask
|= LVIS_CUT
;
2575 if (state
& wxLIST_STATE_CUT
)
2576 lvItem
.state
|= LVIS_CUT
;
2578 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2580 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2581 if (state
& wxLIST_STATE_DROPHILITED
)
2582 lvItem
.state
|= LVIS_DROPHILITED
;
2584 if (stateMask
& wxLIST_STATE_FOCUSED
)
2586 lvItem
.stateMask
|= LVIS_FOCUSED
;
2587 if (state
& wxLIST_STATE_FOCUSED
)
2588 lvItem
.state
|= LVIS_FOCUSED
;
2590 if (stateMask
& wxLIST_STATE_SELECTED
)
2592 lvItem
.stateMask
|= LVIS_SELECTED
;
2593 if (state
& wxLIST_STATE_SELECTED
)
2594 lvItem
.state
|= LVIS_SELECTED
;
2598 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2599 const wxListItem
& info
,
2602 lvItem
.iItem
= (int) info
.m_itemId
;
2604 lvItem
.iImage
= info
.m_image
;
2605 lvItem
.stateMask
= 0;
2608 lvItem
.iSubItem
= info
.m_col
;
2610 if (info
.m_mask
& wxLIST_MASK_STATE
)
2612 lvItem
.mask
|= LVIF_STATE
;
2614 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2617 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2619 lvItem
.mask
|= LVIF_TEXT
;
2620 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
2622 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2626 // pszText is not const, hence the cast
2627 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2628 if ( lvItem
.pszText
)
2629 lvItem
.cchTextMax
= info
.m_text
.Length();
2631 lvItem
.cchTextMax
= 0;
2634 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2635 lvItem
.mask
|= LVIF_IMAGE
;
2638 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2641 wxZeroMemory(lvCol
);
2643 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2645 lvCol
.mask
|= LVCF_TEXT
;
2646 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2649 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2651 lvCol
.mask
|= LVCF_FMT
;
2653 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2654 lvCol
.fmt
= LVCFMT_LEFT
;
2655 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2656 lvCol
.fmt
= LVCFMT_RIGHT
;
2657 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2658 lvCol
.fmt
= LVCFMT_CENTER
;
2661 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2663 lvCol
.mask
|= LVCF_WIDTH
;
2664 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2665 lvCol
.cx
= LVSCW_AUTOSIZE
;
2666 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2667 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2669 lvCol
.cx
= item
.m_width
;
2672 // see comment at the end of wxListCtrl::GetColumn()
2673 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2674 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2676 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2678 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2680 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2681 // seem to be generally nicer than on the left and the generic
2682 // version only draws them on the right (we don't have a flag to
2683 // specify the image location anyhow)
2685 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2686 // make any difference in my tests -- but maybe we should?
2687 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2689 lvCol
.iImage
= item
.m_image
;
2691 //else: it doesn't support item images anyhow
2693 #endif // _WIN32_IE >= 0x0300
2696 #endif // wxUSE_LISTCTRL