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 // include <commctrl.h> "properly"
49 #include "wx/msw/wrapcctl.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // convert our state and mask flags to LV_ITEM constants
56 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
58 // convert wxListItem to LV_ITEM
59 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
60 const wxListItem
& info
, LV_ITEM
& lvItem
);
62 // convert LV_ITEM to wxListItem
63 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
65 /* const */ LV_ITEM
& lvItem
);
67 // convert our wxListItem to LV_COLUMN
68 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
71 // ----------------------------------------------------------------------------
72 // private helper classes
73 // ----------------------------------------------------------------------------
75 // We have to handle both fooW and fooA notifications in several cases
76 // because of broken commctl.dll and/or unicows.dll. This class is used to
77 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
78 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
79 // by wxConvertToMSWListItem().
83 ~wxLV_ITEM() { delete m_buf
; }
84 operator LV_ITEM
&() const { return *m_item
; }
87 wxLV_ITEM(LV_ITEMW
&item
) : m_buf(NULL
), m_item(&item
) {}
88 wxLV_ITEM(LV_ITEMA
&item
)
90 m_item
= new LV_ITEM((LV_ITEM
&)item
);
91 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
93 m_buf
= new wxMB2WXbuf(wxConvLocal
.cMB2WX(item
.pszText
));
94 m_item
->pszText
= (wxChar
*)m_buf
->data();
102 #else // !wxUSE_UNICODE
103 wxLV_ITEM(LV_ITEMW
&item
)
105 m_item
= new LV_ITEM((LV_ITEM
&)item
);
107 // the code below doesn't compile without wxUSE_WCHAR_T and as I don't
108 // know if it's useful to have it at all (do we ever get Unicode
109 // notifications in ANSI mode? I don't think so...) I'm not going to
110 // write alternative implementation right now
112 // but if it is indeed used, we should simply directly use
113 // ::WideCharToMultiByte() here
115 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
117 m_buf
= new wxWC2WXbuf(wxConvLocal
.cWC2WX(item
.pszText
));
118 m_item
->pszText
= (wxChar
*)m_buf
->data();
121 #endif // wxUSE_WCHAR_T
124 wxLV_ITEM(LV_ITEMA
&item
) : m_buf(NULL
), m_item(&item
) {}
127 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
131 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
134 ///////////////////////////////////////////////////////
136 // The MSW version had problems with SetTextColour() et
137 // al as the wxListItemAttr's were stored keyed on the
138 // item index. If a item was inserted anywhere but the end
139 // of the list the the text attributes (colour etc) for
140 // the following items were out of sync.
143 // Under MSW the only way to associate data with a List
144 // item independant of its position in the list is to
145 // store a pointer to it in its lParam attribute. However
146 // user programs are already using this (via the
147 // SetItemData() GetItemData() calls).
149 // However what we can do is store a pointer to a
150 // structure which contains the attributes we want *and*
151 // a lParam for the users data, e.g.
153 // class wxListItemInternalData
156 // wxListItemAttr *attr;
157 // long lParam; // user data
160 // To conserve memory, a wxListItemInternalData is
161 // only allocated for a LV_ITEM if text attributes or
162 // user data(lparam) are being set.
165 // class wxListItemInternalData
166 class wxListItemInternalData
169 wxListItemAttr
*attr
;
170 LPARAM lParam
; // user data
172 wxListItemInternalData() : attr(NULL
), lParam(0) {}
173 ~wxListItemInternalData()
179 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
182 // Get the internal data structure
183 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
184 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
185 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
186 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
193 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
194 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
195 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
196 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
197 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
198 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
199 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
200 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
201 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
202 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
203 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
204 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
205 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
206 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
207 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
208 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
209 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
210 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
211 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
213 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
216 #if wxUSE_EXTENDED_RTTI
217 WX_DEFINE_FLAGS( wxListCtrlStyle
)
219 wxBEGIN_FLAGS( wxListCtrlStyle
)
220 // new style border flags, we put them first to
221 // use them for streaming out
222 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
223 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
224 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
225 wxFLAGS_MEMBER(wxBORDER_RAISED
)
226 wxFLAGS_MEMBER(wxBORDER_STATIC
)
227 wxFLAGS_MEMBER(wxBORDER_NONE
)
229 // old style border flags
230 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
231 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
232 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
233 wxFLAGS_MEMBER(wxRAISED_BORDER
)
234 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
235 wxFLAGS_MEMBER(wxNO_BORDER
)
237 // standard window styles
238 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
239 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
240 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
241 wxFLAGS_MEMBER(wxWANTS_CHARS
)
242 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
243 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
244 wxFLAGS_MEMBER(wxVSCROLL
)
245 wxFLAGS_MEMBER(wxHSCROLL
)
247 wxFLAGS_MEMBER(wxLC_LIST
)
248 wxFLAGS_MEMBER(wxLC_REPORT
)
249 wxFLAGS_MEMBER(wxLC_ICON
)
250 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
251 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
252 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
253 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
254 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
255 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
256 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
257 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
258 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
259 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
260 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
262 wxEND_FLAGS( wxListCtrlStyle
)
264 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
266 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
267 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
269 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
270 wxEND_PROPERTIES_TABLE()
272 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
273 wxEND_HANDLERS_TABLE()
275 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
278 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
281 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
284 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
285 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
287 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
289 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
290 EVT_PAINT(wxListCtrl::OnPaint
)
293 // ============================================================================
295 // ============================================================================
297 // ----------------------------------------------------------------------------
298 // wxListCtrl construction
299 // ----------------------------------------------------------------------------
301 void wxListCtrl::Init()
303 m_imageListNormal
= NULL
;
304 m_imageListSmall
= NULL
;
305 m_imageListState
= NULL
;
306 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
310 m_ignoreChangeMessages
= FALSE
;
312 m_AnyInternalData
= FALSE
;
313 m_hasAnyAttr
= FALSE
;
316 bool wxListCtrl::Create(wxWindow
*parent
,
321 const wxValidator
& validator
,
322 const wxString
& name
)
325 SetValidator(validator
);
326 #endif // wxUSE_VALIDATORS
335 m_windowStyle
= style
;
348 m_windowId
= (id
== -1) ? NewControlId() : id
;
350 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
351 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
353 m_baseStyle
= wstyle
;
355 if ( !DoCreateControl(x
, y
, width
, height
) )
359 parent
->AddChild(this);
364 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
366 DWORD wstyle
= m_baseStyle
;
369 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
371 long oldStyle
= 0; // Dummy
372 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
373 wstyle
|= standardStyle
;
375 // Create the ListView control.
376 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
381 GetWinHwnd(GetParent()),
388 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
393 // explicitly say that we want to use Unicode because otherwise we get ANSI
394 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
396 ::SendMessage(GetHwnd(), LVM_SETUNICODEFORMAT
, TRUE
, 0);
399 // for comctl32.dll v 4.70+ we want to have this attribute because it's
400 // prettier (and also because wxGTK does it like this)
401 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
403 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
404 0, LVS_EX_FULLROWSELECT
);
407 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
408 SetForegroundColour(GetParent()->GetForegroundColour());
415 void wxListCtrl::UpdateStyle()
419 // The new window view style
421 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
422 dwStyleNew
|= m_baseStyle
;
424 // Get the current window style.
425 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
427 // Only set the window style if the view bits have changed.
428 if ( dwStyleOld
!= dwStyleNew
)
430 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
435 void wxListCtrl::FreeAllInternalData()
437 if (m_AnyInternalData
)
439 int n
= GetItemCount();
442 m_ignoreChangeMessages
= TRUE
;
443 for (i
= 0; i
< n
; i
++)
444 wxDeleteInternalData(this, i
);
445 m_ignoreChangeMessages
= FALSE
;
447 m_AnyInternalData
= FALSE
;
451 wxListCtrl::~wxListCtrl()
453 FreeAllInternalData();
457 m_textCtrl
->UnsubclassWin();
458 m_textCtrl
->SetHWND(0);
463 if (m_ownsImageListNormal
) delete m_imageListNormal
;
464 if (m_ownsImageListSmall
) delete m_imageListSmall
;
465 if (m_ownsImageListState
) delete m_imageListState
;
468 // ----------------------------------------------------------------------------
469 // set/get/change style
470 // ----------------------------------------------------------------------------
472 // Add or remove a single window style
473 void wxListCtrl::SetSingleStyle(long style
, bool add
)
475 long flag
= GetWindowStyleFlag();
477 // Get rid of conflicting styles
480 if ( style
& wxLC_MASK_TYPE
)
481 flag
= flag
& ~wxLC_MASK_TYPE
;
482 if ( style
& wxLC_MASK_ALIGN
)
483 flag
= flag
& ~wxLC_MASK_ALIGN
;
484 if ( style
& wxLC_MASK_SORT
)
485 flag
= flag
& ~wxLC_MASK_SORT
;
501 m_windowStyle
= flag
;
506 // Set the whole window style
507 void wxListCtrl::SetWindowStyleFlag(long flag
)
509 m_windowStyle
= flag
;
514 // Can be just a single style, or a bitlist
515 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
518 if ( style
& wxLC_ICON
)
520 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
521 oldStyle
-= LVS_SMALLICON
;
522 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
523 oldStyle
-= LVS_REPORT
;
524 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
525 oldStyle
-= LVS_LIST
;
529 if ( style
& wxLC_SMALL_ICON
)
531 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
532 oldStyle
-= LVS_ICON
;
533 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
534 oldStyle
-= LVS_REPORT
;
535 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
536 oldStyle
-= LVS_LIST
;
537 wstyle
|= LVS_SMALLICON
;
540 if ( style
& wxLC_LIST
)
542 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
543 oldStyle
-= LVS_ICON
;
544 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
545 oldStyle
-= LVS_REPORT
;
546 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
547 oldStyle
-= LVS_SMALLICON
;
551 if ( style
& wxLC_REPORT
)
553 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
554 oldStyle
-= LVS_ICON
;
555 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
556 oldStyle
-= LVS_LIST
;
557 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
558 oldStyle
-= LVS_SMALLICON
;
560 wstyle
|= LVS_REPORT
;
563 if ( style
& wxLC_ALIGN_LEFT
)
565 if ( oldStyle
& LVS_ALIGNTOP
)
566 oldStyle
-= LVS_ALIGNTOP
;
567 wstyle
|= LVS_ALIGNLEFT
;
570 if ( style
& wxLC_ALIGN_TOP
)
572 if ( oldStyle
& LVS_ALIGNLEFT
)
573 oldStyle
-= LVS_ALIGNLEFT
;
574 wstyle
|= LVS_ALIGNTOP
;
577 if ( style
& wxLC_AUTOARRANGE
)
578 wstyle
|= LVS_AUTOARRANGE
;
580 if ( style
& wxLC_NO_SORT_HEADER
)
581 wstyle
|= LVS_NOSORTHEADER
;
583 if ( style
& wxLC_NO_HEADER
)
584 wstyle
|= LVS_NOCOLUMNHEADER
;
586 if ( style
& wxLC_EDIT_LABELS
)
587 wstyle
|= LVS_EDITLABELS
;
589 if ( style
& wxLC_SINGLE_SEL
)
590 wstyle
|= LVS_SINGLESEL
;
592 if ( style
& wxLC_SORT_ASCENDING
)
594 if ( oldStyle
& LVS_SORTDESCENDING
)
595 oldStyle
-= LVS_SORTDESCENDING
;
596 wstyle
|= LVS_SORTASCENDING
;
599 if ( style
& wxLC_SORT_DESCENDING
)
601 if ( oldStyle
& LVS_SORTASCENDING
)
602 oldStyle
-= LVS_SORTASCENDING
;
603 wstyle
|= LVS_SORTDESCENDING
;
606 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
607 if ( style
& wxLC_VIRTUAL
)
609 int ver
= wxTheApp
->GetComCtl32Version();
612 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."),
613 ver
/ 100, ver
% 100);
616 wstyle
|= LVS_OWNERDATA
;
623 // ----------------------------------------------------------------------------
625 // ----------------------------------------------------------------------------
627 // Sets the foreground, i.e. text, colour
628 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
630 if ( !wxWindow::SetForegroundColour(col
) )
633 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
638 // Sets the background colour
639 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
641 if ( !wxWindow::SetBackgroundColour(col
) )
644 // we set the same colour for both the "empty" background and the items
646 COLORREF color
= wxColourToRGB(col
);
647 ListView_SetBkColor(GetHwnd(), color
);
648 ListView_SetTextBkColor(GetHwnd(), color
);
653 // Gets information about this column
654 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
659 lvCol
.mask
= LVCF_WIDTH
;
661 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
663 lvCol
.mask
|= LVCF_TEXT
;
664 lvCol
.pszText
= new wxChar
[513];
665 lvCol
.cchTextMax
= 512;
668 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
670 lvCol
.mask
|= LVCF_FMT
;
673 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
675 lvCol
.mask
|= LVCF_IMAGE
;
678 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
680 // item.m_subItem = lvCol.iSubItem;
681 item
.m_width
= lvCol
.cx
;
683 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
685 item
.m_text
= lvCol
.pszText
;
686 delete[] lvCol
.pszText
;
689 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
691 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
693 item
.m_format
= wxLIST_FORMAT_LEFT
;
696 item
.m_format
= wxLIST_FORMAT_RIGHT
;
699 item
.m_format
= wxLIST_FORMAT_CENTRE
;
702 item
.m_format
= -1; // Unknown?
707 // the column images were not supported in older versions but how to check
708 // for this? we can't use _WIN32_IE because we always define it to a very
709 // high value, so see if another symbol which is only defined starting from
710 // comctl32.dll 4.70 is available
711 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
712 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
714 item
.m_image
= lvCol
.iImage
;
716 #endif // LVCOLUMN::iImage exists
721 // Sets information about this column
722 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
725 wxConvertToMSWListCol(col
, item
, lvCol
);
727 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
730 // Gets the column width
731 int wxListCtrl::GetColumnWidth(int col
) const
733 return ListView_GetColumnWidth(GetHwnd(), col
);
736 // Sets the column width
737 bool wxListCtrl::SetColumnWidth(int col
, int width
)
740 if ( m_windowStyle
& wxLC_LIST
)
744 if ( width2
== wxLIST_AUTOSIZE
)
745 width2
= LVSCW_AUTOSIZE
;
746 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
747 width2
= LVSCW_AUTOSIZE_USEHEADER
;
749 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
752 // Gets the number of items that can fit vertically in the
753 // visible area of the list control (list or report view)
754 // or the total number of items in the list control (icon
755 // or small icon view)
756 int wxListCtrl::GetCountPerPage() const
758 return ListView_GetCountPerPage(GetHwnd());
761 // Gets the edit control for editing labels.
762 wxTextCtrl
* wxListCtrl::GetEditControl() const
767 // Gets information about the item
768 bool wxListCtrl::GetItem(wxListItem
& info
) const
771 wxZeroMemory(lvItem
);
773 lvItem
.iItem
= info
.m_itemId
;
774 lvItem
.iSubItem
= info
.m_col
;
776 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
778 lvItem
.mask
|= LVIF_TEXT
;
779 lvItem
.pszText
= new wxChar
[513];
780 lvItem
.cchTextMax
= 512;
784 lvItem
.pszText
= NULL
;
787 if (info
.m_mask
& wxLIST_MASK_DATA
)
788 lvItem
.mask
|= LVIF_PARAM
;
790 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
791 lvItem
.mask
|= LVIF_IMAGE
;
793 if ( info
.m_mask
& wxLIST_MASK_STATE
)
795 lvItem
.mask
|= LVIF_STATE
;
796 // the other bits are hardly interesting anyhow
797 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
800 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
803 wxLogError(_("Couldn't retrieve information about list control item %d."),
808 // give NULL as hwnd as we already have everything we need
809 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
813 delete[] lvItem
.pszText
;
818 // Sets information about the item
819 bool wxListCtrl::SetItem(wxListItem
& info
)
822 wxConvertToMSWListItem(this, info
, item
);
824 // we never update the lParam if it contains our pointer
825 // to the wxListItemInternalData structure
826 item
.mask
&= ~LVIF_PARAM
;
828 // check if setting attributes or lParam
829 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
831 // get internal item data
832 // perhaps a cache here ?
833 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
838 m_AnyInternalData
= TRUE
;
839 data
= new wxListItemInternalData();
840 item
.lParam
= (LPARAM
) data
;
841 item
.mask
|= LVIF_PARAM
;
846 if (info
.m_mask
& wxLIST_MASK_DATA
)
847 data
->lParam
= info
.m_data
;
850 if (info
.HasAttributes())
853 *data
->attr
= *info
.GetAttributes();
855 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
860 // we could be changing only the attribute in which case we don't need to
861 // call ListView_SetItem() at all
865 if ( !ListView_SetItem(GetHwnd(), &item
) )
867 wxLogDebug(_T("ListView_SetItem() failed"));
873 // we need to update the item immediately to show the new image
874 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
876 // check whether it has any custom attributes
877 if ( info
.HasAttributes() )
881 // if the colour has changed, we must redraw the item
887 // we need this to make the change visible right now
888 RefreshItem(item
.iItem
);
894 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
898 info
.m_mask
= wxLIST_MASK_TEXT
;
899 info
.m_itemId
= index
;
903 info
.m_image
= imageId
;
904 info
.m_mask
|= wxLIST_MASK_IMAGE
;
906 return SetItem(info
);
910 // Gets the item state
911 int wxListCtrl::GetItemState(long item
, long stateMask
) const
915 info
.m_mask
= wxLIST_MASK_STATE
;
916 info
.m_stateMask
= stateMask
;
917 info
.m_itemId
= item
;
925 // Sets the item state
926 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
928 // NB: don't use SetItem() here as it doesn't work with the virtual list
931 wxZeroMemory(lvItem
);
933 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
935 // for the virtual list controls we need to refresh the previously focused
936 // item manually when changing focus without changing selection
937 // programmatically because otherwise it keeps its focus rectangle until
938 // next repaint (yet another comctl32 bug)
941 (stateMask
& wxLIST_STATE_FOCUSED
) &&
942 (state
& wxLIST_STATE_FOCUSED
) )
944 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
951 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
952 (WPARAM
)item
, (LPARAM
)&lvItem
) )
954 wxLogLastError(_T("ListView_SetItemState"));
959 if ( focusOld
!= -1 )
961 // no need to refresh the item if it was previously selected, it would
962 // only result in annoying flicker
963 if ( !(GetItemState(focusOld
,
964 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
966 RefreshItem(focusOld
);
973 // Sets the item image
974 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
978 info
.m_mask
= wxLIST_MASK_IMAGE
;
979 info
.m_image
= image
;
980 info
.m_itemId
= item
;
982 return SetItem(info
);
985 // Gets the item text
986 wxString
wxListCtrl::GetItemText(long item
) const
990 info
.m_mask
= wxLIST_MASK_TEXT
;
991 info
.m_itemId
= item
;
994 return wxEmptyString
;
998 // Sets the item text
999 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
1003 info
.m_mask
= wxLIST_MASK_TEXT
;
1004 info
.m_itemId
= item
;
1010 // Gets the item data
1011 long wxListCtrl::GetItemData(long item
) const
1015 info
.m_mask
= wxLIST_MASK_DATA
;
1016 info
.m_itemId
= item
;
1023 // Sets the item data
1024 bool wxListCtrl::SetItemData(long item
, long data
)
1028 info
.m_mask
= wxLIST_MASK_DATA
;
1029 info
.m_itemId
= item
;
1032 return SetItem(info
);
1035 wxRect
wxListCtrl::GetViewRect() const
1037 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
1038 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1041 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1043 wxLogDebug(_T("ListView_GetViewRect() failed."));
1049 // VZ: I have no idea why is this needed but without it the listbook
1050 // control shows a tiny vertical scrollbar, make sure that it works
1051 // correctly if you decide to change this
1056 wxCopyRECTToRect(rc
, rect
);
1061 // Gets the item rectangle
1062 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1067 if ( code
== wxLIST_RECT_BOUNDS
)
1068 codeWin
= LVIR_BOUNDS
;
1069 else if ( code
== wxLIST_RECT_ICON
)
1070 codeWin
= LVIR_ICON
;
1071 else if ( code
== wxLIST_RECT_LABEL
)
1072 codeWin
= LVIR_LABEL
;
1075 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1077 codeWin
= LVIR_BOUNDS
;
1080 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1082 rect
.x
= rectWin
.left
;
1083 rect
.y
= rectWin
.top
;
1084 rect
.width
= rectWin
.right
- rectWin
.left
;
1085 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1090 // Gets the item position
1091 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1095 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1097 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1101 // Sets the item position.
1102 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1104 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1107 // Gets the number of items in the list control
1108 int wxListCtrl::GetItemCount() const
1113 // Retrieves the spacing between icons in pixels.
1114 // If small is TRUE, gets the spacing for the small icon
1115 // view, otherwise the large icon view.
1116 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1118 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1121 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1124 info
.m_itemId
= item
;
1125 info
.SetTextColour( col
);
1129 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1132 info
.m_itemId
= item
;
1134 return info
.GetTextColour();
1137 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1140 info
.m_itemId
= item
;
1141 info
.SetBackgroundColour( col
);
1145 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1148 info
.m_itemId
= item
;
1150 return info
.GetBackgroundColour();
1153 // Gets the number of selected items in the list control
1154 int wxListCtrl::GetSelectedItemCount() const
1156 return ListView_GetSelectedCount(GetHwnd());
1159 // Gets the text colour of the listview
1160 wxColour
wxListCtrl::GetTextColour() const
1162 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1163 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1167 // Sets the text colour of the listview
1168 void wxListCtrl::SetTextColour(const wxColour
& col
)
1170 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1173 // Gets the index of the topmost visible item when in
1174 // list or report view
1175 long wxListCtrl::GetTopItem() const
1177 return (long) ListView_GetTopIndex(GetHwnd());
1180 // Searches for an item, starting from 'item'.
1181 // 'geometry' is one of
1182 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1183 // 'state' is a state bit flag, one or more of
1184 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1185 // item can be -1 to find the first item that matches the
1187 // Returns the item or -1 if unsuccessful.
1188 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1192 if ( geom
== wxLIST_NEXT_ABOVE
)
1193 flags
|= LVNI_ABOVE
;
1194 if ( geom
== wxLIST_NEXT_ALL
)
1196 if ( geom
== wxLIST_NEXT_BELOW
)
1197 flags
|= LVNI_BELOW
;
1198 if ( geom
== wxLIST_NEXT_LEFT
)
1199 flags
|= LVNI_TOLEFT
;
1200 if ( geom
== wxLIST_NEXT_RIGHT
)
1201 flags
|= LVNI_TORIGHT
;
1203 if ( state
& wxLIST_STATE_CUT
)
1205 if ( state
& wxLIST_STATE_DROPHILITED
)
1206 flags
|= LVNI_DROPHILITED
;
1207 if ( state
& wxLIST_STATE_FOCUSED
)
1208 flags
|= LVNI_FOCUSED
;
1209 if ( state
& wxLIST_STATE_SELECTED
)
1210 flags
|= LVNI_SELECTED
;
1212 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1216 wxImageList
*wxListCtrl::GetImageList(int which
) const
1218 if ( which
== wxIMAGE_LIST_NORMAL
)
1220 return m_imageListNormal
;
1222 else if ( which
== wxIMAGE_LIST_SMALL
)
1224 return m_imageListSmall
;
1226 else if ( which
== wxIMAGE_LIST_STATE
)
1228 return m_imageListState
;
1233 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1236 if ( which
== wxIMAGE_LIST_NORMAL
)
1238 flags
= LVSIL_NORMAL
;
1239 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1240 m_imageListNormal
= imageList
;
1241 m_ownsImageListNormal
= FALSE
;
1243 else if ( which
== wxIMAGE_LIST_SMALL
)
1245 flags
= LVSIL_SMALL
;
1246 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1247 m_imageListSmall
= imageList
;
1248 m_ownsImageListSmall
= FALSE
;
1250 else if ( which
== wxIMAGE_LIST_STATE
)
1252 flags
= LVSIL_STATE
;
1253 if (m_ownsImageListState
) delete m_imageListState
;
1254 m_imageListState
= imageList
;
1255 m_ownsImageListState
= FALSE
;
1257 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1260 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1262 SetImageList(imageList
, which
);
1263 if ( which
== wxIMAGE_LIST_NORMAL
)
1264 m_ownsImageListNormal
= TRUE
;
1265 else if ( which
== wxIMAGE_LIST_SMALL
)
1266 m_ownsImageListSmall
= TRUE
;
1267 else if ( which
== wxIMAGE_LIST_STATE
)
1268 m_ownsImageListState
= TRUE
;
1271 // ----------------------------------------------------------------------------
1273 // ----------------------------------------------------------------------------
1275 // Arranges the items
1276 bool wxListCtrl::Arrange(int flag
)
1279 if ( flag
== wxLIST_ALIGN_LEFT
)
1280 code
= LVA_ALIGNLEFT
;
1281 else if ( flag
== wxLIST_ALIGN_TOP
)
1282 code
= LVA_ALIGNTOP
;
1283 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1285 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1286 code
= LVA_SNAPTOGRID
;
1288 return (ListView_Arrange(GetHwnd(), code
) != 0);
1292 bool wxListCtrl::DeleteItem(long item
)
1294 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1296 wxLogLastError(_T("ListView_DeleteItem"));
1301 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1302 wxT("m_count should match ListView_GetItemCount"));
1304 // the virtual list control doesn't refresh itself correctly, help it
1307 // we need to refresh all the lines below the one which was deleted
1309 if ( item
> 0 && GetItemCount() )
1311 GetItemRect(item
- 1, rectItem
);
1316 rectItem
.height
= 0;
1319 wxRect rectWin
= GetRect();
1320 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1321 rectWin
.y
= rectItem
.GetBottom();
1323 RefreshRect(rectWin
);
1329 // Deletes all items
1330 bool wxListCtrl::DeleteAllItems()
1332 FreeAllInternalData();
1333 return ListView_DeleteAllItems(GetHwnd()) != 0;
1336 // Deletes all items
1337 bool wxListCtrl::DeleteAllColumns()
1339 while ( m_colCount
> 0 )
1341 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1343 wxLogLastError(wxT("ListView_DeleteColumn"));
1351 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1357 bool wxListCtrl::DeleteColumn(int col
)
1359 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1361 if ( success
&& (m_colCount
> 0) )
1366 // Clears items, and columns if there are any.
1367 void wxListCtrl::ClearAll()
1370 if ( m_colCount
> 0 )
1374 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1376 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1378 // ListView_EditLabel requires that the list has focus.
1381 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1384 // failed to start editing
1388 // [re]create the text control wrapping the HWND we got
1391 m_textCtrl
->UnsubclassWin();
1392 m_textCtrl
->SetHWND(0);
1396 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1397 m_textCtrl
->SetHWND(hWnd
);
1398 m_textCtrl
->SubclassWin(hWnd
);
1399 m_textCtrl
->SetParent(this);
1401 // we must disallow TABbing away from the control while the edit contol is
1402 // shown because this leaves it in some strange state (just try removing
1403 // this line and then pressing TAB while editing an item in listctrl
1405 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1410 // End label editing, optionally cancelling the edit
1411 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1413 wxFAIL_MSG( _T("not implemented") );
1418 // Ensures this item is visible
1419 bool wxListCtrl::EnsureVisible(long item
)
1421 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1424 // Find an item whose label matches this string, starting from the item after 'start'
1425 // or the beginning if 'start' is -1.
1426 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1428 LV_FINDINFO findInfo
;
1430 findInfo
.flags
= LVFI_STRING
;
1432 findInfo
.flags
|= LVFI_PARTIAL
;
1435 // ListView_FindItem() excludes the first item from search and to look
1436 // through all the items you need to start from -1 which is unnatural and
1437 // inconsistent with the generic version - so we adjust the index
1440 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1443 // Find an item whose data matches this data, starting from the item after 'start'
1444 // or the beginning if 'start' is -1.
1445 // NOTE : Lindsay Mathieson - 14-July-2002
1446 // No longer use ListView_FindItem as the data attribute is now stored
1447 // in a wxListItemInternalData structure refernced by the actual lParam
1448 long wxListCtrl::FindItem(long start
, long data
)
1450 long idx
= start
+ 1;
1451 long count
= GetItemCount();
1455 if (GetItemData(idx
) == data
)
1463 // Find an item nearest this position in the specified direction, starting from
1464 // the item after 'start' or the beginning if 'start' is -1.
1465 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1467 LV_FINDINFO findInfo
;
1469 findInfo
.flags
= LVFI_NEARESTXY
;
1470 findInfo
.pt
.x
= pt
.x
;
1471 findInfo
.pt
.y
= pt
.y
;
1472 findInfo
.vkDirection
= VK_RIGHT
;
1474 if ( direction
== wxLIST_FIND_UP
)
1475 findInfo
.vkDirection
= VK_UP
;
1476 else if ( direction
== wxLIST_FIND_DOWN
)
1477 findInfo
.vkDirection
= VK_DOWN
;
1478 else if ( direction
== wxLIST_FIND_LEFT
)
1479 findInfo
.vkDirection
= VK_LEFT
;
1480 else if ( direction
== wxLIST_FIND_RIGHT
)
1481 findInfo
.vkDirection
= VK_RIGHT
;
1483 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1486 // Determines which item (if any) is at the specified point,
1487 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1488 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1490 LV_HITTESTINFO hitTestInfo
;
1491 hitTestInfo
.pt
.x
= (int) point
.x
;
1492 hitTestInfo
.pt
.y
= (int) point
.y
;
1494 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1497 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1498 flags
|= wxLIST_HITTEST_ABOVE
;
1499 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1500 flags
|= wxLIST_HITTEST_BELOW
;
1501 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1502 flags
|= wxLIST_HITTEST_NOWHERE
;
1503 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1504 flags
|= wxLIST_HITTEST_ONITEMICON
;
1505 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1506 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1507 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1508 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1509 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1510 flags
|= wxLIST_HITTEST_TOLEFT
;
1511 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1512 flags
|= wxLIST_HITTEST_TORIGHT
;
1514 return (long) hitTestInfo
.iItem
;
1517 // Inserts an item, returning the index of the new item if successful,
1519 long wxListCtrl::InsertItem(wxListItem
& info
)
1521 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1524 wxConvertToMSWListItem(this, info
, item
);
1525 item
.mask
&= ~LVIF_PARAM
;
1527 // check wether we need to allocate our internal data
1528 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1529 if (needInternalData
)
1531 m_AnyInternalData
= TRUE
;
1532 item
.mask
|= LVIF_PARAM
;
1534 // internal stucture that manages data
1535 wxListItemInternalData
*data
= new wxListItemInternalData();
1536 item
.lParam
= (LPARAM
) data
;
1538 if (info
.m_mask
& wxLIST_MASK_DATA
)
1539 data
->lParam
= info
.m_data
;
1541 // check whether it has any custom attributes
1542 if ( info
.HasAttributes() )
1544 // take copy of attributes
1545 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1549 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1552 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1553 wxT("m_count should match ListView_GetItemCount"));
1558 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1561 info
.m_text
= label
;
1562 info
.m_mask
= wxLIST_MASK_TEXT
;
1563 info
.m_itemId
= index
;
1564 return InsertItem(info
);
1567 // Inserts an image item
1568 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1571 info
.m_image
= imageIndex
;
1572 info
.m_mask
= wxLIST_MASK_IMAGE
;
1573 info
.m_itemId
= index
;
1574 return InsertItem(info
);
1577 // Inserts an image/string item
1578 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1581 info
.m_image
= imageIndex
;
1582 info
.m_text
= label
;
1583 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1584 info
.m_itemId
= index
;
1585 return InsertItem(info
);
1588 // For list view mode (only), inserts a column.
1589 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1592 wxConvertToMSWListCol(col
, item
, lvCol
);
1594 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1596 // always give some width to the new column: this one is compatible
1597 // with the generic version
1598 lvCol
.mask
|= LVCF_WIDTH
;
1602 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1607 else // failed to insert?
1609 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1616 long wxListCtrl::InsertColumn(long col
,
1617 const wxString
& heading
,
1622 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1623 item
.m_text
= heading
;
1626 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1627 item
.m_width
= width
;
1629 item
.m_format
= format
;
1631 return InsertColumn(col
, item
);
1634 // scroll the control by the given number of pixels (exception: in list view,
1635 // dx is interpreted as number of columns)
1636 bool wxListCtrl::ScrollList(int dx
, int dy
)
1638 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1640 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1650 // fn is a function which takes 3 long arguments: item1, item2, data.
1651 // item1 is the long data associated with a first item (NOT the index).
1652 // item2 is the long data associated with a second item (NOT the index).
1653 // data is the same value as passed to SortItems.
1654 // The return value is a negative number if the first item should precede the second
1655 // item, a positive number of the second item should precede the first,
1656 // or zero if the two items are equivalent.
1658 // data is arbitrary data to be passed to the sort function.
1660 // Internal structures for proxying the user compare function
1661 // so that we can pass it the *real* user data
1663 // translate lParam data and call user func
1664 struct wxInternalDataSort
1666 wxListCtrlCompare user_fn
;
1670 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1672 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1674 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1675 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1677 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1678 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1680 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1684 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1686 struct wxInternalDataSort internalData
;
1687 internalData
.user_fn
= fn
;
1688 internalData
.data
= data
;
1690 // WPARAM cast is needed for mingw/cygwin
1691 if ( !ListView_SortItems(GetHwnd(),
1692 wxInternalDataCompareFunc
,
1693 (WPARAM
) &internalData
) )
1695 wxLogDebug(_T("ListView_SortItems() failed"));
1705 // ----------------------------------------------------------------------------
1706 // message processing
1707 // ----------------------------------------------------------------------------
1709 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1711 if (cmd
== EN_UPDATE
)
1713 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1714 event
.SetEventObject( this );
1715 ProcessCommand(event
);
1718 else if (cmd
== EN_KILLFOCUS
)
1720 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1721 event
.SetEventObject( this );
1722 ProcessCommand(event
);
1729 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1732 // prepare the event
1733 // -----------------
1735 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1736 event
.SetEventObject(this);
1738 wxEventType eventType
= wxEVT_NULL
;
1740 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1742 // if your compiler is as broken as this, you should really change it: this
1743 // code is needed for normal operation! #ifdef below is only useful for
1744 // automatic rebuilds which are done with a very old compiler version
1745 #ifdef HDN_BEGINTRACKA
1747 // check for messages from the header (in report view)
1748 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1750 // is it a message from the header?
1751 if ( nmhdr
->hwndFrom
== hwndHdr
)
1753 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1755 event
.m_itemIndex
= -1;
1757 switch ( nmhdr
->code
)
1759 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1760 // TRACK messages even to ANSI programs: on my system I get
1761 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1763 // work around is to simply catch both versions and hope that it
1764 // works (why should this message exist in ANSI and Unicode is
1765 // beyond me as it doesn't deal with strings at all...)
1767 // note that fr HDN_TRACK another possibility could be to use
1768 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1769 // something other than the item width changes so we'd have to
1770 // filter out the unwanted events then
1771 case HDN_BEGINTRACKA
:
1772 case HDN_BEGINTRACKW
:
1773 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1778 if ( eventType
== wxEVT_NULL
)
1779 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1784 if ( eventType
== wxEVT_NULL
)
1785 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1787 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1788 event
.m_col
= nmHDR
->iItem
;
1793 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1796 // find the column clicked: we have to search for it
1797 // ourselves as the notification message doesn't provide
1800 // where did the click occur?
1802 if ( !::GetCursorPos(&ptClick
) )
1804 wxLogLastError(_T("GetCursorPos"));
1807 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1809 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1812 event
.m_pointDrag
.x
= ptClick
.x
;
1813 event
.m_pointDrag
.y
= ptClick
.y
;
1815 int colCount
= Header_GetItemCount(hwndHdr
);
1818 for ( int col
= 0; col
< colCount
; col
++ )
1820 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1822 if ( ::PtInRect(&rect
, ptClick
) )
1832 case HDN_GETDISPINFOW
:
1834 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1835 // This is a fix for a strange bug under XP.
1836 // Normally, info->iItem is a valid index, but
1837 // sometimes this is a silly (large) number
1838 // and when we return FALSE via wxControl::MSWOnNotify
1839 // to indicate that it hasn't yet been processed,
1840 // there's a GPF in Windows.
1841 // By returning TRUE here, we avoid further processing
1842 // of this strange message.
1843 if ( info
->iItem
>= GetColumnCount() )
1849 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1853 #endif // defined(HDN_BEGINTRACKA)
1854 if ( nmhdr
->hwndFrom
== GetHwnd() )
1856 // almost all messages use NM_LISTVIEW
1857 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1859 const int iItem
= nmLV
->iItem
;
1862 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1863 // ignored for efficiency. It is done here because the internal data is in the
1864 // process of being deleted so we don't want to try and access it below.
1865 if ( m_ignoreChangeMessages
&&
1866 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1872 // If we have a valid item then check if there is a data value
1873 // associated with it and put it in the event.
1874 if ( iItem
>= 0 && iItem
< GetItemCount() )
1876 wxListItemInternalData
*internaldata
=
1877 wxGetInternalData(GetHwnd(), iItem
);
1880 event
.m_item
.m_data
= internaldata
->lParam
;
1884 switch ( nmhdr
->code
)
1886 case LVN_BEGINRDRAG
:
1887 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1891 if ( eventType
== wxEVT_NULL
)
1893 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1896 event
.m_itemIndex
= iItem
;
1897 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1898 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1901 // NB: we have to handle both *A and *W versions here because some
1902 // versions of comctl32.dll send ANSI message to an Unicode app
1903 case LVN_BEGINLABELEDITA
:
1905 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1906 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1907 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1908 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1911 case LVN_BEGINLABELEDITW
:
1913 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1914 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1915 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1916 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1920 case LVN_ENDLABELEDITA
:
1922 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1923 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1924 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1925 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1926 ((LV_ITEM
)item
).iItem
== -1 )
1928 // don't keep a stale wxTextCtrl around
1931 // EDIT control will be deleted by the list control itself so
1932 // prevent us from deleting it as well
1933 m_textCtrl
->UnsubclassWin();
1934 m_textCtrl
->SetHWND(0);
1941 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1944 case LVN_ENDLABELEDITW
:
1946 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1947 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1948 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1949 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1950 ((LV_ITEM
)item
).iItem
== -1 )
1952 // don't keep a stale wxTextCtrl around
1955 // EDIT control will be deleted by the list control itself so
1956 // prevent us from deleting it as well
1957 m_textCtrl
->UnsubclassWin();
1958 m_textCtrl
->SetHWND(0);
1965 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1969 case LVN_COLUMNCLICK
:
1970 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1971 event
.m_itemIndex
= -1;
1972 event
.m_col
= nmLV
->iSubItem
;
1975 case LVN_DELETEALLITEMS
:
1977 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1978 event
.m_itemIndex
= -1;
1981 case LVN_DELETEITEM
:
1983 // this should be prevented by the post-processing code below,
1984 // but "just in case"
1987 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1988 event
.m_itemIndex
= iItem
;
1989 // delete the assoicated internal data
1990 wxDeleteInternalData(this, iItem
);
1993 case LVN_SETDISPINFO
:
1995 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1996 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1997 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2001 case LVN_INSERTITEM
:
2002 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2003 event
.m_itemIndex
= iItem
;
2006 case LVN_ITEMCHANGED
:
2007 // we translate this catch all message into more interesting
2008 // (and more easy to process) wxWindows events
2010 // first of all, we deal with the state change events only and
2011 // only for valid items (item == -1 for the virtual list
2013 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2015 // temp vars for readability
2016 const UINT stOld
= nmLV
->uOldState
;
2017 const UINT stNew
= nmLV
->uNewState
;
2019 event
.m_item
.SetId(iItem
);
2020 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2023 GetItem(event
.m_item
);
2025 // has the focus changed?
2026 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2028 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2029 event
.m_itemIndex
= iItem
;
2032 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2034 if ( eventType
!= wxEVT_NULL
)
2036 // focus and selection have both changed: send the
2037 // focus event from here and the selection one
2039 event
.SetEventType(eventType
);
2040 (void)GetEventHandler()->ProcessEvent(event
);
2042 else // no focus event to send
2044 // then need to set m_itemIndex as it wasn't done
2046 event
.m_itemIndex
= iItem
;
2049 eventType
= stNew
& LVIS_SELECTED
2050 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2051 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2055 if ( eventType
== wxEVT_NULL
)
2057 // not an interesting event for us
2065 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2066 WORD wVKey
= info
->wVKey
;
2068 // get the current selection
2069 long lItem
= GetNextItem(-1,
2071 wxLIST_STATE_SELECTED
);
2073 // <Enter> or <Space> activate the selected item if any (but
2074 // not with Shift and/or Ctrl as then they have a predefined
2075 // meaning for the list view)
2077 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2078 !(wxIsShiftDown() || wxIsCtrlDown()) )
2080 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2084 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2086 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2087 // value which should be used as is
2088 int code
= wxCharCodeMSWToWX(wVKey
);
2089 event
.m_code
= code
? code
: wVKey
;
2093 event
.m_item
.m_itemId
= lItem
;
2097 // fill the other fields too
2098 event
.m_item
.m_text
= GetItemText(lItem
);
2099 event
.m_item
.m_data
= GetItemData(lItem
);
2105 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2107 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2112 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2113 // if it happened on an item (and not on empty place)
2120 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2121 event
.m_itemIndex
= iItem
;
2122 event
.m_item
.m_text
= GetItemText(iItem
);
2123 event
.m_item
.m_data
= GetItemData(iItem
);
2127 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2128 // don't do anything else
2129 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2134 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2135 LV_HITTESTINFO lvhti
;
2136 wxZeroMemory(lvhti
);
2138 ::GetCursorPos(&(lvhti
.pt
));
2139 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2140 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2142 if ( lvhti
.flags
& LVHT_ONITEM
)
2144 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2145 event
.m_itemIndex
= lvhti
.iItem
;
2146 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2147 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2152 #ifdef NM_CUSTOMDRAW
2154 *result
= OnCustomDraw(lParam
);
2157 #endif // _WIN32_IE >= 0x300
2159 case LVN_ODCACHEHINT
:
2161 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2163 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2165 // we get some really stupid cache hints like ones for
2166 // items in range 0..0 for an empty control or, after
2167 // deleting an item, for items in invalid range -- filter
2169 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2172 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2174 const long iMax
= GetItemCount();
2175 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2180 case LVN_GETDISPINFO
:
2183 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2185 LV_ITEM
& lvi
= info
->item
;
2186 long item
= lvi
.iItem
;
2188 if ( lvi
.mask
& LVIF_TEXT
)
2190 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2191 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2194 // see comment at the end of wxListCtrl::GetColumn()
2195 #ifdef NM_CUSTOMDRAW
2196 if ( lvi
.mask
& LVIF_IMAGE
)
2198 lvi
.iImage
= OnGetItemImage(item
);
2200 #endif // NM_CUSTOMDRAW
2202 // a little dose of healthy paranoia: as we never use
2203 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2204 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2205 _T("we don't support state callbacks yet!") );
2212 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2217 // where did this one come from?
2221 // process the event
2222 // -----------------
2224 event
.SetEventType(eventType
);
2226 bool processed
= GetEventHandler()->ProcessEvent(event
);
2230 switch ( nmhdr
->code
)
2232 case LVN_DELETEALLITEMS
:
2233 // always return TRUE to suppress all additional LVN_DELETEITEM
2234 // notifications - this makes deleting all items from a list ctrl
2239 case LVN_ENDLABELEDITA
:
2240 case LVN_ENDLABELEDITW
:
2241 // logic here is inversed compared to all the other messages
2242 *result
= event
.IsAllowed();
2244 // don't keep a stale wxTextCtrl around
2247 // EDIT control will be deleted by the list control itself so
2248 // prevent us from deleting it as well
2249 m_textCtrl
->UnsubclassWin();
2250 m_textCtrl
->SetHWND(0);
2259 *result
= !event
.IsAllowed();
2264 // see comment at the end of wxListCtrl::GetColumn()
2265 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2267 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2269 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2270 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2271 switch ( nmcd
.dwDrawStage
)
2274 // if we've got any items with non standard attributes,
2275 // notify us before painting each item
2277 // for virtual controls, always suppose that we have attributes as
2278 // there is no way to check for this
2279 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2282 case CDDS_ITEMPREPAINT
:
2284 size_t item
= (size_t)nmcd
.dwItemSpec
;
2285 if ( item
>= (size_t)GetItemCount() )
2287 // we get this message with item == 0 for an empty control,
2288 // we must ignore it as calling OnGetItemAttr() would be
2290 return CDRF_DODEFAULT
;
2293 wxListItemAttr
*attr
=
2294 IsVirtual() ? OnGetItemAttr(item
)
2295 : wxGetInternalDataAttr(this, item
);
2299 // nothing to do for this item
2300 return CDRF_DODEFAULT
;
2304 wxColour colText
, colBack
;
2305 if ( attr
->HasFont() )
2307 wxFont font
= attr
->GetFont();
2308 hFont
= (HFONT
)font
.GetResourceHandle();
2315 if ( attr
->HasTextColour() )
2317 colText
= attr
->GetTextColour();
2321 colText
= GetTextColour();
2324 if ( attr
->HasBackgroundColour() )
2326 colBack
= attr
->GetBackgroundColour();
2330 colBack
= GetBackgroundColour();
2333 lplvcd
->clrText
= wxColourToRGB(colText
);
2334 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2336 // note that if we wanted to set colours for
2337 // individual columns (subitems), we would have
2338 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2341 ::SelectObject(nmcd
.hdc
, hFont
);
2343 return CDRF_NEWFONT
;
2346 // fall through to return CDRF_DODEFAULT
2349 return CDRF_DODEFAULT
;
2353 #endif // NM_CUSTOMDRAW supported
2355 // Necessary for drawing hrules and vrules, if specified
2356 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2360 wxControl::OnPaint(event
);
2362 // Reset the device origin since it may have been set
2363 dc
.SetDeviceOrigin(0, 0);
2365 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2366 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2368 if (!drawHRules
&& !drawVRules
)
2370 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2373 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2375 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2377 wxSize clientSize
= GetClientSize();
2381 int itemCount
= GetItemCount();
2385 long top
= GetTopItem();
2386 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2388 if (GetItemRect(i
, itemRect
))
2390 cy
= itemRect
.GetTop();
2391 if (i
!= 0) // Don't draw the first one
2393 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2396 if (i
== itemCount
- 1)
2398 cy
= itemRect
.GetBottom();
2399 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2405 if (drawVRules
&& (i
> -1))
2407 wxRect firstItemRect
;
2408 GetItemRect(0, firstItemRect
);
2410 if (GetItemRect(i
, itemRect
))
2413 int x
= itemRect
.GetX();
2414 for (col
= 0; col
< GetColumnCount(); col
++)
2416 int colWidth
= GetColumnWidth(col
);
2418 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2424 // ----------------------------------------------------------------------------
2425 // virtual list controls
2426 // ----------------------------------------------------------------------------
2428 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2430 // this is a pure virtual function, in fact - which is not really pure
2431 // because the controls which are not virtual don't need to implement it
2432 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2434 return wxEmptyString
;
2437 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2440 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2445 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2447 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2448 _T("invalid item index in OnGetItemAttr()") );
2450 // no attributes by default
2454 void wxListCtrl::SetItemCount(long count
)
2456 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2458 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, LVSICF_NOSCROLL
) )
2460 wxLogLastError(_T("ListView_SetItemCount"));
2463 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2464 wxT("m_count should match ListView_GetItemCount"));
2467 void wxListCtrl::RefreshItem(long item
)
2469 // strangely enough, ListView_Update() results in much more flicker here
2470 // than a dumb Refresh() -- why?
2472 if ( !ListView_Update(GetHwnd(), item
) )
2474 wxLogLastError(_T("ListView_Update"));
2478 GetItemRect(item
, rect
);
2483 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2485 wxRect rect1
, rect2
;
2486 GetItemRect(itemFrom
, rect1
);
2487 GetItemRect(itemTo
, rect2
);
2489 wxRect rect
= rect1
;
2490 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2495 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2498 it
.mask
= LVIF_PARAM
;
2501 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2503 return (wxListItemInternalData
*) it
.lParam
;
2508 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2510 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2513 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2515 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2522 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2524 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2528 memset(&item
, 0, sizeof(item
));
2529 item
.iItem
= itemId
;
2530 item
.mask
= LVIF_PARAM
;
2531 item
.lParam
= (LPARAM
) 0;
2532 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2537 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2541 wxListItemInternalData
*internaldata
=
2542 (wxListItemInternalData
*) lvItem
.lParam
;
2545 info
.m_data
= internaldata
->lParam
;
2549 info
.m_stateMask
= 0;
2550 info
.m_itemId
= lvItem
.iItem
;
2552 long oldMask
= lvItem
.mask
;
2554 bool needText
= FALSE
;
2555 if (hwndListCtrl
!= 0)
2557 if ( lvItem
.mask
& LVIF_TEXT
)
2564 lvItem
.pszText
= new wxChar
[513];
2565 lvItem
.cchTextMax
= 512;
2567 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2568 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2571 if ( lvItem
.mask
& LVIF_STATE
)
2573 info
.m_mask
|= wxLIST_MASK_STATE
;
2575 if ( lvItem
.stateMask
& LVIS_CUT
)
2577 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2578 if ( lvItem
.state
& LVIS_CUT
)
2579 info
.m_state
|= wxLIST_STATE_CUT
;
2581 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2583 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2584 if ( lvItem
.state
& LVIS_DROPHILITED
)
2585 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2587 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2589 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2590 if ( lvItem
.state
& LVIS_FOCUSED
)
2591 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2593 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2595 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2596 if ( lvItem
.state
& LVIS_SELECTED
)
2597 info
.m_state
|= wxLIST_STATE_SELECTED
;
2601 if ( lvItem
.mask
& LVIF_TEXT
)
2603 info
.m_mask
|= wxLIST_MASK_TEXT
;
2604 info
.m_text
= lvItem
.pszText
;
2606 if ( lvItem
.mask
& LVIF_IMAGE
)
2608 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2609 info
.m_image
= lvItem
.iImage
;
2611 if ( lvItem
.mask
& LVIF_PARAM
)
2612 info
.m_mask
|= wxLIST_MASK_DATA
;
2613 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2614 info
.m_mask
|= wxLIST_SET_ITEM
;
2615 info
.m_col
= lvItem
.iSubItem
;
2620 delete[] lvItem
.pszText
;
2622 lvItem
.mask
= oldMask
;
2625 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2627 if (stateMask
& wxLIST_STATE_CUT
)
2629 lvItem
.stateMask
|= LVIS_CUT
;
2630 if (state
& wxLIST_STATE_CUT
)
2631 lvItem
.state
|= LVIS_CUT
;
2633 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2635 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2636 if (state
& wxLIST_STATE_DROPHILITED
)
2637 lvItem
.state
|= LVIS_DROPHILITED
;
2639 if (stateMask
& wxLIST_STATE_FOCUSED
)
2641 lvItem
.stateMask
|= LVIS_FOCUSED
;
2642 if (state
& wxLIST_STATE_FOCUSED
)
2643 lvItem
.state
|= LVIS_FOCUSED
;
2645 if (stateMask
& wxLIST_STATE_SELECTED
)
2647 lvItem
.stateMask
|= LVIS_SELECTED
;
2648 if (state
& wxLIST_STATE_SELECTED
)
2649 lvItem
.state
|= LVIS_SELECTED
;
2653 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2654 const wxListItem
& info
,
2657 lvItem
.iItem
= (int) info
.m_itemId
;
2659 lvItem
.iImage
= info
.m_image
;
2660 lvItem
.stateMask
= 0;
2663 lvItem
.iSubItem
= info
.m_col
;
2665 if (info
.m_mask
& wxLIST_MASK_STATE
)
2667 lvItem
.mask
|= LVIF_STATE
;
2669 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2672 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2674 lvItem
.mask
|= LVIF_TEXT
;
2675 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2677 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2681 // pszText is not const, hence the cast
2682 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2683 if ( lvItem
.pszText
)
2684 lvItem
.cchTextMax
= info
.m_text
.Length();
2686 lvItem
.cchTextMax
= 0;
2689 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2690 lvItem
.mask
|= LVIF_IMAGE
;
2693 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2696 wxZeroMemory(lvCol
);
2698 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2700 lvCol
.mask
|= LVCF_TEXT
;
2701 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2704 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2706 lvCol
.mask
|= LVCF_FMT
;
2708 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2709 lvCol
.fmt
= LVCFMT_LEFT
;
2710 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2711 lvCol
.fmt
= LVCFMT_RIGHT
;
2712 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2713 lvCol
.fmt
= LVCFMT_CENTER
;
2716 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2718 lvCol
.mask
|= LVCF_WIDTH
;
2719 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2720 lvCol
.cx
= LVSCW_AUTOSIZE
;
2721 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2722 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2724 lvCol
.cx
= item
.m_width
;
2727 // see comment at the end of wxListCtrl::GetColumn()
2728 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2729 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2731 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2733 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2735 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2736 // seem to be generally nicer than on the left and the generic
2737 // version only draws them on the right (we don't have a flag to
2738 // specify the image location anyhow)
2740 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2741 // make any difference in my tests -- but maybe we should?
2742 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2744 lvCol
.iImage
= item
.m_image
;
2746 //else: it doesn't support item images anyhow
2748 #endif // _WIN32_IE >= 0x0300
2751 #endif // wxUSE_LISTCTRL