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 comctl32.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().
81 #define LV_ITEM_NATIVE LV_ITEMW
82 #define LV_ITEM_OTHER LV_ITEMA
84 #define LV_CONV_TO_WX cMB2WX
85 #define LV_CONV_BUF wxMB2WXbuf
87 #define LV_ITEM_NATIVE LV_ITEMA
88 #define LV_ITEM_OTHER LV_ITEMW
90 #define LV_CONV_TO_WX cWC2WX
91 #define LV_CONV_BUF wxWC2WXbuf
92 #endif // Unicode/ANSI
97 // default ctor, use Init() later
98 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
100 // init without conversion
101 void Init(LV_ITEM_NATIVE
& item
)
103 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
108 // init with conversion
109 void Init(LV_ITEM_OTHER
& item
)
111 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
112 // point to our own m_item
114 // memcpy() can't work if the struct sizes are different
115 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
116 CodeCantWorkIfDiffSizes
);
118 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
120 // convert text from ANSI to Unicod if necessary
121 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
123 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
124 m_item
.pszText
= (wxChar
*)m_buf
->data();
128 // ctor without conversion
129 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
131 // ctor with conversion
132 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
137 ~wxLV_ITEM() { delete m_buf
; }
139 // conversion to the real LV_ITEM
140 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
145 LV_ITEM_NATIVE
*m_pItem
;
146 LV_ITEM_NATIVE m_item
;
148 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
151 ///////////////////////////////////////////////////////
153 // The MSW version had problems with SetTextColour() et
154 // al as the wxListItemAttr's were stored keyed on the
155 // item index. If a item was inserted anywhere but the end
156 // of the list the the text attributes (colour etc) for
157 // the following items were out of sync.
160 // Under MSW the only way to associate data with a List
161 // item independant of its position in the list is to
162 // store a pointer to it in its lParam attribute. However
163 // user programs are already using this (via the
164 // SetItemData() GetItemData() calls).
166 // However what we can do is store a pointer to a
167 // structure which contains the attributes we want *and*
168 // a lParam for the users data, e.g.
170 // class wxListItemInternalData
173 // wxListItemAttr *attr;
174 // long lParam; // user data
177 // To conserve memory, a wxListItemInternalData is
178 // only allocated for a LV_ITEM if text attributes or
179 // user data(lparam) are being set.
182 // class wxListItemInternalData
183 class wxListItemInternalData
186 wxListItemAttr
*attr
;
187 LPARAM lParam
; // user data
189 wxListItemInternalData() : attr(NULL
), lParam(0) {}
190 ~wxListItemInternalData()
196 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
199 // Get the internal data structure
200 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
201 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
202 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
203 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
211 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
213 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
215 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
216 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
217 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
233 #if wxUSE_EXTENDED_RTTI
234 WX_DEFINE_FLAGS( wxListCtrlStyle
)
236 wxBEGIN_FLAGS( wxListCtrlStyle
)
237 // new style border flags, we put them first to
238 // use them for streaming out
239 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
240 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
241 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
242 wxFLAGS_MEMBER(wxBORDER_RAISED
)
243 wxFLAGS_MEMBER(wxBORDER_STATIC
)
244 wxFLAGS_MEMBER(wxBORDER_NONE
)
246 // old style border flags
247 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
248 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
249 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
250 wxFLAGS_MEMBER(wxRAISED_BORDER
)
251 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
252 wxFLAGS_MEMBER(wxBORDER
)
254 // standard window styles
255 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
256 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
257 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
258 wxFLAGS_MEMBER(wxWANTS_CHARS
)
259 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
260 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
261 wxFLAGS_MEMBER(wxVSCROLL
)
262 wxFLAGS_MEMBER(wxHSCROLL
)
264 wxFLAGS_MEMBER(wxLC_LIST
)
265 wxFLAGS_MEMBER(wxLC_REPORT
)
266 wxFLAGS_MEMBER(wxLC_ICON
)
267 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
268 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
269 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
270 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
271 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
272 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
273 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
274 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
275 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
276 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
277 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
279 wxEND_FLAGS( wxListCtrlStyle
)
281 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
283 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
284 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
286 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
287 wxEND_PROPERTIES_TABLE()
289 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
290 wxEND_HANDLERS_TABLE()
292 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
295 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
298 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
301 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
302 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
304 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
306 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
307 EVT_PAINT(wxListCtrl::OnPaint
)
310 // ============================================================================
312 // ============================================================================
314 // ----------------------------------------------------------------------------
315 // wxListCtrl construction
316 // ----------------------------------------------------------------------------
318 void wxListCtrl::Init()
320 m_imageListNormal
= NULL
;
321 m_imageListSmall
= NULL
;
322 m_imageListState
= NULL
;
323 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
327 m_ignoreChangeMessages
= FALSE
;
329 m_AnyInternalData
= FALSE
;
330 m_hasAnyAttr
= FALSE
;
333 bool wxListCtrl::Create(wxWindow
*parent
,
338 const wxValidator
& wxVALIDATOR_PARAM(validator
),
339 const wxString
& name
)
342 SetValidator(validator
);
343 #endif // wxUSE_VALIDATORS
352 m_windowStyle
= style
;
365 m_windowId
= (id
== -1) ? NewControlId() : id
;
367 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
368 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
370 m_baseStyle
= wstyle
;
372 if ( !DoCreateControl(x
, y
, width
, height
) )
376 parent
->AddChild(this);
381 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
383 DWORD wstyle
= m_baseStyle
;
386 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
388 long oldStyle
= 0; // Dummy
389 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
390 wstyle
|= standardStyle
;
392 // Create the ListView control.
393 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
398 GetWinHwnd(GetParent()),
405 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
410 // explicitly say that we want to use Unicode because otherwise we get ANSI
411 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
413 ::SendMessage(GetHwnd(), LVM_SETUNICODEFORMAT
, TRUE
, 0);
416 // for comctl32.dll v 4.70+ we want to have this attribute because it's
417 // prettier (and also because wxGTK does it like this)
418 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
420 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
421 0, LVS_EX_FULLROWSELECT
);
424 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
425 SetForegroundColour(GetParent()->GetForegroundColour());
432 void wxListCtrl::UpdateStyle()
436 // The new window view style
438 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
439 dwStyleNew
|= m_baseStyle
;
441 // Get the current window style.
442 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
444 // Only set the window style if the view bits have changed.
445 if ( dwStyleOld
!= dwStyleNew
)
447 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
452 void wxListCtrl::FreeAllInternalData()
454 if (m_AnyInternalData
)
456 int n
= GetItemCount();
459 m_ignoreChangeMessages
= TRUE
;
460 for (i
= 0; i
< n
; i
++)
461 wxDeleteInternalData(this, i
);
462 m_ignoreChangeMessages
= FALSE
;
464 m_AnyInternalData
= FALSE
;
468 wxListCtrl::~wxListCtrl()
470 FreeAllInternalData();
474 m_textCtrl
->UnsubclassWin();
475 m_textCtrl
->SetHWND(0);
480 if (m_ownsImageListNormal
) delete m_imageListNormal
;
481 if (m_ownsImageListSmall
) delete m_imageListSmall
;
482 if (m_ownsImageListState
) delete m_imageListState
;
485 // ----------------------------------------------------------------------------
486 // set/get/change style
487 // ----------------------------------------------------------------------------
489 // Add or remove a single window style
490 void wxListCtrl::SetSingleStyle(long style
, bool add
)
492 long flag
= GetWindowStyleFlag();
494 // Get rid of conflicting styles
497 if ( style
& wxLC_MASK_TYPE
)
498 flag
= flag
& ~wxLC_MASK_TYPE
;
499 if ( style
& wxLC_MASK_ALIGN
)
500 flag
= flag
& ~wxLC_MASK_ALIGN
;
501 if ( style
& wxLC_MASK_SORT
)
502 flag
= flag
& ~wxLC_MASK_SORT
;
518 m_windowStyle
= flag
;
523 // Set the whole window style
524 void wxListCtrl::SetWindowStyleFlag(long flag
)
526 m_windowStyle
= flag
;
531 // Can be just a single style, or a bitlist
532 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
535 if ( style
& wxLC_ICON
)
537 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
538 oldStyle
-= LVS_SMALLICON
;
539 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
540 oldStyle
-= LVS_REPORT
;
541 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
542 oldStyle
-= LVS_LIST
;
546 if ( style
& wxLC_SMALL_ICON
)
548 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
549 oldStyle
-= LVS_ICON
;
550 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
551 oldStyle
-= LVS_REPORT
;
552 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
553 oldStyle
-= LVS_LIST
;
554 wstyle
|= LVS_SMALLICON
;
557 if ( style
& wxLC_LIST
)
559 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
560 oldStyle
-= LVS_ICON
;
561 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
562 oldStyle
-= LVS_REPORT
;
563 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
564 oldStyle
-= LVS_SMALLICON
;
568 if ( style
& wxLC_REPORT
)
570 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
571 oldStyle
-= LVS_ICON
;
572 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
573 oldStyle
-= LVS_LIST
;
574 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
575 oldStyle
-= LVS_SMALLICON
;
577 wstyle
|= LVS_REPORT
;
580 if ( style
& wxLC_ALIGN_LEFT
)
582 if ( oldStyle
& LVS_ALIGNTOP
)
583 oldStyle
-= LVS_ALIGNTOP
;
584 wstyle
|= LVS_ALIGNLEFT
;
587 if ( style
& wxLC_ALIGN_TOP
)
589 if ( oldStyle
& LVS_ALIGNLEFT
)
590 oldStyle
-= LVS_ALIGNLEFT
;
591 wstyle
|= LVS_ALIGNTOP
;
594 if ( style
& wxLC_AUTOARRANGE
)
595 wstyle
|= LVS_AUTOARRANGE
;
597 if ( style
& wxLC_NO_SORT_HEADER
)
598 wstyle
|= LVS_NOSORTHEADER
;
600 if ( style
& wxLC_NO_HEADER
)
601 wstyle
|= LVS_NOCOLUMNHEADER
;
603 if ( style
& wxLC_EDIT_LABELS
)
604 wstyle
|= LVS_EDITLABELS
;
606 if ( style
& wxLC_SINGLE_SEL
)
607 wstyle
|= LVS_SINGLESEL
;
609 if ( style
& wxLC_SORT_ASCENDING
)
611 if ( oldStyle
& LVS_SORTDESCENDING
)
612 oldStyle
-= LVS_SORTDESCENDING
;
613 wstyle
|= LVS_SORTASCENDING
;
616 if ( style
& wxLC_SORT_DESCENDING
)
618 if ( oldStyle
& LVS_SORTASCENDING
)
619 oldStyle
-= LVS_SORTASCENDING
;
620 wstyle
|= LVS_SORTDESCENDING
;
623 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
624 if ( style
& wxLC_VIRTUAL
)
626 int ver
= wxTheApp
->GetComCtl32Version();
629 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."),
630 ver
/ 100, ver
% 100);
633 wstyle
|= LVS_OWNERDATA
;
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 // Sets the foreground, i.e. text, colour
645 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
647 if ( !wxWindow::SetForegroundColour(col
) )
650 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
655 // Sets the background colour
656 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
658 if ( !wxWindow::SetBackgroundColour(col
) )
661 // we set the same colour for both the "empty" background and the items
663 COLORREF color
= wxColourToRGB(col
);
664 ListView_SetBkColor(GetHwnd(), color
);
665 ListView_SetTextBkColor(GetHwnd(), color
);
670 // Gets information about this column
671 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
676 lvCol
.mask
= LVCF_WIDTH
;
678 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
680 lvCol
.mask
|= LVCF_TEXT
;
681 lvCol
.pszText
= new wxChar
[513];
682 lvCol
.cchTextMax
= 512;
685 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
687 lvCol
.mask
|= LVCF_FMT
;
690 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
692 lvCol
.mask
|= LVCF_IMAGE
;
695 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
697 // item.m_subItem = lvCol.iSubItem;
698 item
.m_width
= lvCol
.cx
;
700 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
702 item
.m_text
= lvCol
.pszText
;
703 delete[] lvCol
.pszText
;
706 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
708 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
710 item
.m_format
= wxLIST_FORMAT_LEFT
;
713 item
.m_format
= wxLIST_FORMAT_RIGHT
;
716 item
.m_format
= wxLIST_FORMAT_CENTRE
;
719 item
.m_format
= -1; // Unknown?
724 // the column images were not supported in older versions but how to check
725 // for this? we can't use _WIN32_IE because we always define it to a very
726 // high value, so see if another symbol which is only defined starting from
727 // comctl32.dll 4.70 is available
728 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
729 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
731 item
.m_image
= lvCol
.iImage
;
733 #endif // LVCOLUMN::iImage exists
738 // Sets information about this column
739 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
742 wxConvertToMSWListCol(col
, item
, lvCol
);
744 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
747 // Gets the column width
748 int wxListCtrl::GetColumnWidth(int col
) const
750 return ListView_GetColumnWidth(GetHwnd(), col
);
753 // Sets the column width
754 bool wxListCtrl::SetColumnWidth(int col
, int width
)
757 if ( m_windowStyle
& wxLC_LIST
)
761 if ( width2
== wxLIST_AUTOSIZE
)
762 width2
= LVSCW_AUTOSIZE
;
763 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
764 width2
= LVSCW_AUTOSIZE_USEHEADER
;
766 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
769 // Gets the number of items that can fit vertically in the
770 // visible area of the list control (list or report view)
771 // or the total number of items in the list control (icon
772 // or small icon view)
773 int wxListCtrl::GetCountPerPage() const
775 return ListView_GetCountPerPage(GetHwnd());
778 // Gets the edit control for editing labels.
779 wxTextCtrl
* wxListCtrl::GetEditControl() const
784 // Gets information about the item
785 bool wxListCtrl::GetItem(wxListItem
& info
) const
788 wxZeroMemory(lvItem
);
790 lvItem
.iItem
= info
.m_itemId
;
791 lvItem
.iSubItem
= info
.m_col
;
793 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
795 lvItem
.mask
|= LVIF_TEXT
;
796 lvItem
.pszText
= new wxChar
[513];
797 lvItem
.cchTextMax
= 512;
801 lvItem
.pszText
= NULL
;
804 if (info
.m_mask
& wxLIST_MASK_DATA
)
805 lvItem
.mask
|= LVIF_PARAM
;
807 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
808 lvItem
.mask
|= LVIF_IMAGE
;
810 if ( info
.m_mask
& wxLIST_MASK_STATE
)
812 lvItem
.mask
|= LVIF_STATE
;
813 // the other bits are hardly interesting anyhow
814 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
817 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
820 wxLogError(_("Couldn't retrieve information about list control item %d."),
825 // give NULL as hwnd as we already have everything we need
826 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
830 delete[] lvItem
.pszText
;
835 // Sets information about the item
836 bool wxListCtrl::SetItem(wxListItem
& info
)
839 wxConvertToMSWListItem(this, info
, item
);
841 // we never update the lParam if it contains our pointer
842 // to the wxListItemInternalData structure
843 item
.mask
&= ~LVIF_PARAM
;
845 // check if setting attributes or lParam
846 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
848 // get internal item data
849 // perhaps a cache here ?
850 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
855 m_AnyInternalData
= TRUE
;
856 data
= new wxListItemInternalData();
857 item
.lParam
= (LPARAM
) data
;
858 item
.mask
|= LVIF_PARAM
;
863 if (info
.m_mask
& wxLIST_MASK_DATA
)
864 data
->lParam
= info
.m_data
;
867 if (info
.HasAttributes())
870 *data
->attr
= *info
.GetAttributes();
872 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
877 // we could be changing only the attribute in which case we don't need to
878 // call ListView_SetItem() at all
882 if ( !ListView_SetItem(GetHwnd(), &item
) )
884 wxLogDebug(_T("ListView_SetItem() failed"));
890 // we need to update the item immediately to show the new image
891 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
893 // check whether it has any custom attributes
894 if ( info
.HasAttributes() )
898 // if the colour has changed, we must redraw the item
904 // we need this to make the change visible right now
905 RefreshItem(item
.iItem
);
911 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
915 info
.m_mask
= wxLIST_MASK_TEXT
;
916 info
.m_itemId
= index
;
920 info
.m_image
= imageId
;
921 info
.m_mask
|= wxLIST_MASK_IMAGE
;
923 return SetItem(info
);
927 // Gets the item state
928 int wxListCtrl::GetItemState(long item
, long stateMask
) const
932 info
.m_mask
= wxLIST_MASK_STATE
;
933 info
.m_stateMask
= stateMask
;
934 info
.m_itemId
= item
;
942 // Sets the item state
943 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
945 // NB: don't use SetItem() here as it doesn't work with the virtual list
948 wxZeroMemory(lvItem
);
950 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
952 // for the virtual list controls we need to refresh the previously focused
953 // item manually when changing focus without changing selection
954 // programmatically because otherwise it keeps its focus rectangle until
955 // next repaint (yet another comctl32 bug)
958 (stateMask
& wxLIST_STATE_FOCUSED
) &&
959 (state
& wxLIST_STATE_FOCUSED
) )
961 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
968 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
969 (WPARAM
)item
, (LPARAM
)&lvItem
) )
971 wxLogLastError(_T("ListView_SetItemState"));
976 if ( focusOld
!= -1 )
978 // no need to refresh the item if it was previously selected, it would
979 // only result in annoying flicker
980 if ( !(GetItemState(focusOld
,
981 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
983 RefreshItem(focusOld
);
990 // Sets the item image
991 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
995 info
.m_mask
= wxLIST_MASK_IMAGE
;
996 info
.m_image
= image
;
997 info
.m_itemId
= item
;
999 return SetItem(info
);
1002 // Gets the item text
1003 wxString
wxListCtrl::GetItemText(long item
) const
1007 info
.m_mask
= wxLIST_MASK_TEXT
;
1008 info
.m_itemId
= item
;
1011 return wxEmptyString
;
1015 // Sets the item text
1016 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
1020 info
.m_mask
= wxLIST_MASK_TEXT
;
1021 info
.m_itemId
= item
;
1027 // Gets the item data
1028 long wxListCtrl::GetItemData(long item
) const
1032 info
.m_mask
= wxLIST_MASK_DATA
;
1033 info
.m_itemId
= item
;
1040 // Sets the item data
1041 bool wxListCtrl::SetItemData(long item
, long data
)
1045 info
.m_mask
= wxLIST_MASK_DATA
;
1046 info
.m_itemId
= item
;
1049 return SetItem(info
);
1052 wxRect
wxListCtrl::GetViewRect() const
1054 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
1055 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1058 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1060 wxLogDebug(_T("ListView_GetViewRect() failed."));
1066 wxCopyRECTToRect(rc
, rect
);
1071 // Gets the item rectangle
1072 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1077 if ( code
== wxLIST_RECT_BOUNDS
)
1078 codeWin
= LVIR_BOUNDS
;
1079 else if ( code
== wxLIST_RECT_ICON
)
1080 codeWin
= LVIR_ICON
;
1081 else if ( code
== wxLIST_RECT_LABEL
)
1082 codeWin
= LVIR_LABEL
;
1085 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1087 codeWin
= LVIR_BOUNDS
;
1090 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1092 rect
.x
= rectWin
.left
;
1093 rect
.y
= rectWin
.top
;
1094 rect
.width
= rectWin
.right
- rectWin
.left
;
1095 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1100 // Gets the item position
1101 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1105 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1107 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1111 // Sets the item position.
1112 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1114 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1117 // Gets the number of items in the list control
1118 int wxListCtrl::GetItemCount() const
1123 wxSize
wxListCtrl::GetItemSpacing() const
1125 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1127 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1130 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1132 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1135 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1138 info
.m_itemId
= item
;
1139 info
.SetTextColour( col
);
1143 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1146 info
.m_itemId
= item
;
1148 return info
.GetTextColour();
1151 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1154 info
.m_itemId
= item
;
1155 info
.SetBackgroundColour( col
);
1159 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1162 info
.m_itemId
= item
;
1164 return info
.GetBackgroundColour();
1167 // Gets the number of selected items in the list control
1168 int wxListCtrl::GetSelectedItemCount() const
1170 return ListView_GetSelectedCount(GetHwnd());
1173 // Gets the text colour of the listview
1174 wxColour
wxListCtrl::GetTextColour() const
1176 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1177 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1181 // Sets the text colour of the listview
1182 void wxListCtrl::SetTextColour(const wxColour
& col
)
1184 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1187 // Gets the index of the topmost visible item when in
1188 // list or report view
1189 long wxListCtrl::GetTopItem() const
1191 return (long) ListView_GetTopIndex(GetHwnd());
1194 // Searches for an item, starting from 'item'.
1195 // 'geometry' is one of
1196 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1197 // 'state' is a state bit flag, one or more of
1198 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1199 // item can be -1 to find the first item that matches the
1201 // Returns the item or -1 if unsuccessful.
1202 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1206 if ( geom
== wxLIST_NEXT_ABOVE
)
1207 flags
|= LVNI_ABOVE
;
1208 if ( geom
== wxLIST_NEXT_ALL
)
1210 if ( geom
== wxLIST_NEXT_BELOW
)
1211 flags
|= LVNI_BELOW
;
1212 if ( geom
== wxLIST_NEXT_LEFT
)
1213 flags
|= LVNI_TOLEFT
;
1214 if ( geom
== wxLIST_NEXT_RIGHT
)
1215 flags
|= LVNI_TORIGHT
;
1217 if ( state
& wxLIST_STATE_CUT
)
1219 if ( state
& wxLIST_STATE_DROPHILITED
)
1220 flags
|= LVNI_DROPHILITED
;
1221 if ( state
& wxLIST_STATE_FOCUSED
)
1222 flags
|= LVNI_FOCUSED
;
1223 if ( state
& wxLIST_STATE_SELECTED
)
1224 flags
|= LVNI_SELECTED
;
1226 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1230 wxImageList
*wxListCtrl::GetImageList(int which
) const
1232 if ( which
== wxIMAGE_LIST_NORMAL
)
1234 return m_imageListNormal
;
1236 else if ( which
== wxIMAGE_LIST_SMALL
)
1238 return m_imageListSmall
;
1240 else if ( which
== wxIMAGE_LIST_STATE
)
1242 return m_imageListState
;
1247 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1250 if ( which
== wxIMAGE_LIST_NORMAL
)
1252 flags
= LVSIL_NORMAL
;
1253 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1254 m_imageListNormal
= imageList
;
1255 m_ownsImageListNormal
= FALSE
;
1257 else if ( which
== wxIMAGE_LIST_SMALL
)
1259 flags
= LVSIL_SMALL
;
1260 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1261 m_imageListSmall
= imageList
;
1262 m_ownsImageListSmall
= FALSE
;
1264 else if ( which
== wxIMAGE_LIST_STATE
)
1266 flags
= LVSIL_STATE
;
1267 if (m_ownsImageListState
) delete m_imageListState
;
1268 m_imageListState
= imageList
;
1269 m_ownsImageListState
= FALSE
;
1271 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1274 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1276 SetImageList(imageList
, which
);
1277 if ( which
== wxIMAGE_LIST_NORMAL
)
1278 m_ownsImageListNormal
= TRUE
;
1279 else if ( which
== wxIMAGE_LIST_SMALL
)
1280 m_ownsImageListSmall
= TRUE
;
1281 else if ( which
== wxIMAGE_LIST_STATE
)
1282 m_ownsImageListState
= TRUE
;
1285 // ----------------------------------------------------------------------------
1287 // ----------------------------------------------------------------------------
1289 // Arranges the items
1290 bool wxListCtrl::Arrange(int flag
)
1293 if ( flag
== wxLIST_ALIGN_LEFT
)
1294 code
= LVA_ALIGNLEFT
;
1295 else if ( flag
== wxLIST_ALIGN_TOP
)
1296 code
= LVA_ALIGNTOP
;
1297 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1299 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1300 code
= LVA_SNAPTOGRID
;
1302 return (ListView_Arrange(GetHwnd(), code
) != 0);
1306 bool wxListCtrl::DeleteItem(long item
)
1308 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1310 wxLogLastError(_T("ListView_DeleteItem"));
1315 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1316 wxT("m_count should match ListView_GetItemCount"));
1318 // the virtual list control doesn't refresh itself correctly, help it
1321 // we need to refresh all the lines below the one which was deleted
1323 if ( item
> 0 && GetItemCount() )
1325 GetItemRect(item
- 1, rectItem
);
1330 rectItem
.height
= 0;
1333 wxRect rectWin
= GetRect();
1334 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1335 rectWin
.y
= rectItem
.GetBottom();
1337 RefreshRect(rectWin
);
1343 // Deletes all items
1344 bool wxListCtrl::DeleteAllItems()
1346 FreeAllInternalData();
1347 return ListView_DeleteAllItems(GetHwnd()) != 0;
1350 // Deletes all items
1351 bool wxListCtrl::DeleteAllColumns()
1353 while ( m_colCount
> 0 )
1355 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1357 wxLogLastError(wxT("ListView_DeleteColumn"));
1365 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1371 bool wxListCtrl::DeleteColumn(int col
)
1373 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1375 if ( success
&& (m_colCount
> 0) )
1380 // Clears items, and columns if there are any.
1381 void wxListCtrl::ClearAll()
1384 if ( m_colCount
> 0 )
1388 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1390 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1392 // ListView_EditLabel requires that the list has focus.
1395 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1398 // failed to start editing
1402 // [re]create the text control wrapping the HWND we got
1405 m_textCtrl
->UnsubclassWin();
1406 m_textCtrl
->SetHWND(0);
1410 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1411 m_textCtrl
->SetHWND(hWnd
);
1412 m_textCtrl
->SubclassWin(hWnd
);
1413 m_textCtrl
->SetParent(this);
1415 // we must disallow TABbing away from the control while the edit contol is
1416 // shown because this leaves it in some strange state (just try removing
1417 // this line and then pressing TAB while editing an item in listctrl
1419 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1424 // End label editing, optionally cancelling the edit
1425 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1427 wxFAIL_MSG( _T("not implemented") );
1432 // Ensures this item is visible
1433 bool wxListCtrl::EnsureVisible(long item
)
1435 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1438 // Find an item whose label matches this string, starting from the item after 'start'
1439 // or the beginning if 'start' is -1.
1440 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1442 LV_FINDINFO findInfo
;
1444 findInfo
.flags
= LVFI_STRING
;
1446 findInfo
.flags
|= LVFI_PARTIAL
;
1449 // ListView_FindItem() excludes the first item from search and to look
1450 // through all the items you need to start from -1 which is unnatural and
1451 // inconsistent with the generic version - so we adjust the index
1454 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1457 // Find an item whose data matches this data, starting from the item after 'start'
1458 // or the beginning if 'start' is -1.
1459 // NOTE : Lindsay Mathieson - 14-July-2002
1460 // No longer use ListView_FindItem as the data attribute is now stored
1461 // in a wxListItemInternalData structure refernced by the actual lParam
1462 long wxListCtrl::FindItem(long start
, long data
)
1464 long idx
= start
+ 1;
1465 long count
= GetItemCount();
1469 if (GetItemData(idx
) == data
)
1477 // Find an item nearest this position in the specified direction, starting from
1478 // the item after 'start' or the beginning if 'start' is -1.
1479 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1481 LV_FINDINFO findInfo
;
1483 findInfo
.flags
= LVFI_NEARESTXY
;
1484 findInfo
.pt
.x
= pt
.x
;
1485 findInfo
.pt
.y
= pt
.y
;
1486 findInfo
.vkDirection
= VK_RIGHT
;
1488 if ( direction
== wxLIST_FIND_UP
)
1489 findInfo
.vkDirection
= VK_UP
;
1490 else if ( direction
== wxLIST_FIND_DOWN
)
1491 findInfo
.vkDirection
= VK_DOWN
;
1492 else if ( direction
== wxLIST_FIND_LEFT
)
1493 findInfo
.vkDirection
= VK_LEFT
;
1494 else if ( direction
== wxLIST_FIND_RIGHT
)
1495 findInfo
.vkDirection
= VK_RIGHT
;
1497 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1500 // Determines which item (if any) is at the specified point,
1501 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1502 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1504 LV_HITTESTINFO hitTestInfo
;
1505 hitTestInfo
.pt
.x
= (int) point
.x
;
1506 hitTestInfo
.pt
.y
= (int) point
.y
;
1508 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1511 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1512 flags
|= wxLIST_HITTEST_ABOVE
;
1513 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1514 flags
|= wxLIST_HITTEST_BELOW
;
1515 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1516 flags
|= wxLIST_HITTEST_NOWHERE
;
1517 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1518 flags
|= wxLIST_HITTEST_ONITEMICON
;
1519 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1520 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1521 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1522 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1523 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1524 flags
|= wxLIST_HITTEST_TOLEFT
;
1525 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1526 flags
|= wxLIST_HITTEST_TORIGHT
;
1528 return (long) hitTestInfo
.iItem
;
1531 // Inserts an item, returning the index of the new item if successful,
1533 long wxListCtrl::InsertItem(wxListItem
& info
)
1535 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1538 wxConvertToMSWListItem(this, info
, item
);
1539 item
.mask
&= ~LVIF_PARAM
;
1541 // check wether we need to allocate our internal data
1542 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1543 if (needInternalData
)
1545 m_AnyInternalData
= TRUE
;
1546 item
.mask
|= LVIF_PARAM
;
1548 // internal stucture that manages data
1549 wxListItemInternalData
*data
= new wxListItemInternalData();
1550 item
.lParam
= (LPARAM
) data
;
1552 if (info
.m_mask
& wxLIST_MASK_DATA
)
1553 data
->lParam
= info
.m_data
;
1555 // check whether it has any custom attributes
1556 if ( info
.HasAttributes() )
1558 // take copy of attributes
1559 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1563 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1566 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1567 wxT("m_count should match ListView_GetItemCount"));
1572 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1575 info
.m_text
= label
;
1576 info
.m_mask
= wxLIST_MASK_TEXT
;
1577 info
.m_itemId
= index
;
1578 return InsertItem(info
);
1581 // Inserts an image item
1582 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1585 info
.m_image
= imageIndex
;
1586 info
.m_mask
= wxLIST_MASK_IMAGE
;
1587 info
.m_itemId
= index
;
1588 return InsertItem(info
);
1591 // Inserts an image/string item
1592 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1595 info
.m_image
= imageIndex
;
1596 info
.m_text
= label
;
1597 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1598 info
.m_itemId
= index
;
1599 return InsertItem(info
);
1602 // For list view mode (only), inserts a column.
1603 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1606 wxConvertToMSWListCol(col
, item
, lvCol
);
1608 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1610 // always give some width to the new column: this one is compatible
1611 // with the generic version
1612 lvCol
.mask
|= LVCF_WIDTH
;
1616 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1621 else // failed to insert?
1623 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1630 long wxListCtrl::InsertColumn(long col
,
1631 const wxString
& heading
,
1636 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1637 item
.m_text
= heading
;
1640 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1641 item
.m_width
= width
;
1643 item
.m_format
= format
;
1645 return InsertColumn(col
, item
);
1648 // scroll the control by the given number of pixels (exception: in list view,
1649 // dx is interpreted as number of columns)
1650 bool wxListCtrl::ScrollList(int dx
, int dy
)
1652 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1654 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1664 // fn is a function which takes 3 long arguments: item1, item2, data.
1665 // item1 is the long data associated with a first item (NOT the index).
1666 // item2 is the long data associated with a second item (NOT the index).
1667 // data is the same value as passed to SortItems.
1668 // The return value is a negative number if the first item should precede the second
1669 // item, a positive number of the second item should precede the first,
1670 // or zero if the two items are equivalent.
1672 // data is arbitrary data to be passed to the sort function.
1674 // Internal structures for proxying the user compare function
1675 // so that we can pass it the *real* user data
1677 // translate lParam data and call user func
1678 struct wxInternalDataSort
1680 wxListCtrlCompare user_fn
;
1684 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1686 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1688 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1689 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1691 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1692 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1694 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1698 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1700 struct wxInternalDataSort internalData
;
1701 internalData
.user_fn
= fn
;
1702 internalData
.data
= data
;
1704 // WPARAM cast is needed for mingw/cygwin
1705 if ( !ListView_SortItems(GetHwnd(),
1706 wxInternalDataCompareFunc
,
1707 (WPARAM
) &internalData
) )
1709 wxLogDebug(_T("ListView_SortItems() failed"));
1719 // ----------------------------------------------------------------------------
1720 // message processing
1721 // ----------------------------------------------------------------------------
1723 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1725 if (cmd
== EN_UPDATE
)
1727 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1728 event
.SetEventObject( this );
1729 ProcessCommand(event
);
1732 else if (cmd
== EN_KILLFOCUS
)
1734 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1735 event
.SetEventObject( this );
1736 ProcessCommand(event
);
1743 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1746 // prepare the event
1747 // -----------------
1749 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1750 event
.SetEventObject(this);
1752 wxEventType eventType
= wxEVT_NULL
;
1754 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1756 // if your compiler is as broken as this, you should really change it: this
1757 // code is needed for normal operation! #ifdef below is only useful for
1758 // automatic rebuilds which are done with a very old compiler version
1759 #ifdef HDN_BEGINTRACKA
1761 // check for messages from the header (in report view)
1762 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1764 // is it a message from the header?
1765 if ( nmhdr
->hwndFrom
== hwndHdr
)
1767 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1769 event
.m_itemIndex
= -1;
1771 switch ( nmhdr
->code
)
1773 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1774 // TRACK messages even to ANSI programs: on my system I get
1775 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1777 // work around is to simply catch both versions and hope that it
1778 // works (why should this message exist in ANSI and Unicode is
1779 // beyond me as it doesn't deal with strings at all...)
1781 // note that fr HDN_TRACK another possibility could be to use
1782 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1783 // something other than the item width changes so we'd have to
1784 // filter out the unwanted events then
1785 case HDN_BEGINTRACKA
:
1786 case HDN_BEGINTRACKW
:
1787 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1792 if ( eventType
== wxEVT_NULL
)
1793 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1798 if ( eventType
== wxEVT_NULL
)
1799 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1801 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1802 event
.m_col
= nmHDR
->iItem
;
1807 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1810 // find the column clicked: we have to search for it
1811 // ourselves as the notification message doesn't provide
1814 // where did the click occur?
1816 if ( !::GetCursorPos(&ptClick
) )
1818 wxLogLastError(_T("GetCursorPos"));
1821 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1823 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1826 event
.m_pointDrag
.x
= ptClick
.x
;
1827 event
.m_pointDrag
.y
= ptClick
.y
;
1829 int colCount
= Header_GetItemCount(hwndHdr
);
1832 for ( int col
= 0; col
< colCount
; col
++ )
1834 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1836 if ( ::PtInRect(&rect
, ptClick
) )
1846 case HDN_GETDISPINFOW
:
1848 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1849 // This is a fix for a strange bug under XP.
1850 // Normally, info->iItem is a valid index, but
1851 // sometimes this is a silly (large) number
1852 // and when we return FALSE via wxControl::MSWOnNotify
1853 // to indicate that it hasn't yet been processed,
1854 // there's a GPF in Windows.
1855 // By returning TRUE here, we avoid further processing
1856 // of this strange message.
1857 if ( info
->iItem
>= GetColumnCount() )
1863 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1867 #endif // defined(HDN_BEGINTRACKA)
1868 if ( nmhdr
->hwndFrom
== GetHwnd() )
1870 // almost all messages use NM_LISTVIEW
1871 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1873 const int iItem
= nmLV
->iItem
;
1876 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1877 // ignored for efficiency. It is done here because the internal data is in the
1878 // process of being deleted so we don't want to try and access it below.
1879 if ( m_ignoreChangeMessages
&&
1880 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1886 // If we have a valid item then check if there is a data value
1887 // associated with it and put it in the event.
1888 if ( iItem
>= 0 && iItem
< GetItemCount() )
1890 wxListItemInternalData
*internaldata
=
1891 wxGetInternalData(GetHwnd(), iItem
);
1894 event
.m_item
.m_data
= internaldata
->lParam
;
1898 switch ( nmhdr
->code
)
1900 case LVN_BEGINRDRAG
:
1901 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1905 if ( eventType
== wxEVT_NULL
)
1907 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1910 event
.m_itemIndex
= iItem
;
1911 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1912 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1915 // NB: we have to handle both *A and *W versions here because some
1916 // versions of comctl32.dll send ANSI messages even to the
1918 case LVN_BEGINLABELEDITA
:
1919 case LVN_BEGINLABELEDITW
:
1922 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1924 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1926 else // LVN_BEGINLABELEDITW
1928 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1931 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1932 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1933 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1937 case LVN_ENDLABELEDITA
:
1938 case LVN_ENDLABELEDITW
:
1941 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1943 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1945 else // LVN_ENDLABELEDITW
1947 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1950 // was editing cancelled?
1951 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1952 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1954 // don't keep a stale wxTextCtrl around
1957 // EDIT control will be deleted by the list control itself so
1958 // prevent us from deleting it as well
1959 m_textCtrl
->UnsubclassWin();
1960 m_textCtrl
->SetHWND(0);
1965 event
.SetEditCanceled(true);
1968 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1969 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1970 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1974 case LVN_COLUMNCLICK
:
1975 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1976 event
.m_itemIndex
= -1;
1977 event
.m_col
= nmLV
->iSubItem
;
1980 case LVN_DELETEALLITEMS
:
1982 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1983 event
.m_itemIndex
= -1;
1986 case LVN_DELETEITEM
:
1988 // this should be prevented by the post-processing code below,
1989 // but "just in case"
1992 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1993 event
.m_itemIndex
= iItem
;
1994 // delete the assoicated internal data
1995 wxDeleteInternalData(this, iItem
);
1998 case LVN_SETDISPINFO
:
2000 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
2001 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2002 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2006 case LVN_INSERTITEM
:
2007 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2008 event
.m_itemIndex
= iItem
;
2011 case LVN_ITEMCHANGED
:
2012 // we translate this catch all message into more interesting
2013 // (and more easy to process) wxWindows events
2015 // first of all, we deal with the state change events only and
2016 // only for valid items (item == -1 for the virtual list
2018 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2020 // temp vars for readability
2021 const UINT stOld
= nmLV
->uOldState
;
2022 const UINT stNew
= nmLV
->uNewState
;
2024 event
.m_item
.SetId(iItem
);
2025 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2028 GetItem(event
.m_item
);
2030 // has the focus changed?
2031 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2033 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2034 event
.m_itemIndex
= iItem
;
2037 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2039 if ( eventType
!= wxEVT_NULL
)
2041 // focus and selection have both changed: send the
2042 // focus event from here and the selection one
2044 event
.SetEventType(eventType
);
2045 (void)GetEventHandler()->ProcessEvent(event
);
2047 else // no focus event to send
2049 // then need to set m_itemIndex as it wasn't done
2051 event
.m_itemIndex
= iItem
;
2054 eventType
= stNew
& LVIS_SELECTED
2055 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2056 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2060 if ( eventType
== wxEVT_NULL
)
2062 // not an interesting event for us
2070 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2071 WORD wVKey
= info
->wVKey
;
2073 // get the current selection
2074 long lItem
= GetNextItem(-1,
2076 wxLIST_STATE_SELECTED
);
2078 // <Enter> or <Space> activate the selected item if any (but
2079 // not with Shift and/or Ctrl as then they have a predefined
2080 // meaning for the list view)
2082 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2083 !(wxIsShiftDown() || wxIsCtrlDown()) )
2085 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2089 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2091 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2092 // value which should be used as is
2093 int code
= wxCharCodeMSWToWX(wVKey
);
2094 event
.m_code
= code
? code
: wVKey
;
2098 event
.m_item
.m_itemId
= lItem
;
2102 // fill the other fields too
2103 event
.m_item
.m_text
= GetItemText(lItem
);
2104 event
.m_item
.m_data
= GetItemData(lItem
);
2110 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2112 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2117 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2118 // if it happened on an item (and not on empty place)
2125 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2126 event
.m_itemIndex
= iItem
;
2127 event
.m_item
.m_text
= GetItemText(iItem
);
2128 event
.m_item
.m_data
= GetItemData(iItem
);
2132 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2133 // don't do anything else
2134 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2139 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2140 LV_HITTESTINFO lvhti
;
2141 wxZeroMemory(lvhti
);
2143 ::GetCursorPos(&(lvhti
.pt
));
2144 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2145 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2147 if ( lvhti
.flags
& LVHT_ONITEM
)
2149 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2150 event
.m_itemIndex
= lvhti
.iItem
;
2151 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2152 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2157 #ifdef NM_CUSTOMDRAW
2159 *result
= OnCustomDraw(lParam
);
2162 #endif // _WIN32_IE >= 0x300
2164 case LVN_ODCACHEHINT
:
2166 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2168 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2170 // we get some really stupid cache hints like ones for
2171 // items in range 0..0 for an empty control or, after
2172 // deleting an item, for items in invalid range -- filter
2174 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2177 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2179 const long iMax
= GetItemCount();
2180 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2185 case LVN_GETDISPINFO
:
2188 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2190 LV_ITEM
& lvi
= info
->item
;
2191 long item
= lvi
.iItem
;
2193 if ( lvi
.mask
& LVIF_TEXT
)
2195 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2196 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2199 // see comment at the end of wxListCtrl::GetColumn()
2200 #ifdef NM_CUSTOMDRAW
2201 if ( lvi
.mask
& LVIF_IMAGE
)
2203 lvi
.iImage
= OnGetItemImage(item
);
2205 #endif // NM_CUSTOMDRAW
2207 // a little dose of healthy paranoia: as we never use
2208 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2209 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2210 _T("we don't support state callbacks yet!") );
2217 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2222 // where did this one come from?
2226 // process the event
2227 // -----------------
2229 event
.SetEventType(eventType
);
2231 bool processed
= GetEventHandler()->ProcessEvent(event
);
2235 switch ( nmhdr
->code
)
2237 case LVN_DELETEALLITEMS
:
2238 // always return TRUE to suppress all additional LVN_DELETEITEM
2239 // notifications - this makes deleting all items from a list ctrl
2244 case LVN_ENDLABELEDITA
:
2245 case LVN_ENDLABELEDITW
:
2246 // logic here is inversed compared to all the other messages
2247 *result
= event
.IsAllowed();
2249 // don't keep a stale wxTextCtrl around
2252 // EDIT control will be deleted by the list control itself so
2253 // prevent us from deleting it as well
2254 m_textCtrl
->UnsubclassWin();
2255 m_textCtrl
->SetHWND(0);
2264 *result
= !event
.IsAllowed();
2269 // see comment at the end of wxListCtrl::GetColumn()
2270 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2272 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2274 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2275 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2276 switch ( nmcd
.dwDrawStage
)
2279 // if we've got any items with non standard attributes,
2280 // notify us before painting each item
2282 // for virtual controls, always suppose that we have attributes as
2283 // there is no way to check for this
2284 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2287 case CDDS_ITEMPREPAINT
:
2289 size_t item
= (size_t)nmcd
.dwItemSpec
;
2290 if ( item
>= (size_t)GetItemCount() )
2292 // we get this message with item == 0 for an empty control,
2293 // we must ignore it as calling OnGetItemAttr() would be
2295 return CDRF_DODEFAULT
;
2298 wxListItemAttr
*attr
=
2299 IsVirtual() ? OnGetItemAttr(item
)
2300 : wxGetInternalDataAttr(this, item
);
2304 // nothing to do for this item
2305 return CDRF_DODEFAULT
;
2309 wxColour colText
, colBack
;
2310 if ( attr
->HasFont() )
2312 wxFont font
= attr
->GetFont();
2313 hFont
= (HFONT
)font
.GetResourceHandle();
2320 if ( attr
->HasTextColour() )
2322 colText
= attr
->GetTextColour();
2326 colText
= GetTextColour();
2329 if ( attr
->HasBackgroundColour() )
2331 colBack
= attr
->GetBackgroundColour();
2335 colBack
= GetBackgroundColour();
2338 lplvcd
->clrText
= wxColourToRGB(colText
);
2339 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2341 // note that if we wanted to set colours for
2342 // individual columns (subitems), we would have
2343 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2346 ::SelectObject(nmcd
.hdc
, hFont
);
2348 return CDRF_NEWFONT
;
2351 // fall through to return CDRF_DODEFAULT
2354 return CDRF_DODEFAULT
;
2358 #endif // NM_CUSTOMDRAW supported
2360 // Necessary for drawing hrules and vrules, if specified
2361 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2365 wxControl::OnPaint(event
);
2367 // Reset the device origin since it may have been set
2368 dc
.SetDeviceOrigin(0, 0);
2370 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2371 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2373 if (!drawHRules
&& !drawVRules
)
2375 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2378 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2380 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2382 wxSize clientSize
= GetClientSize();
2386 int itemCount
= GetItemCount();
2390 long top
= GetTopItem();
2391 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2393 if (GetItemRect(i
, itemRect
))
2395 cy
= itemRect
.GetTop();
2396 if (i
!= 0) // Don't draw the first one
2398 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2401 if (i
== itemCount
- 1)
2403 cy
= itemRect
.GetBottom();
2404 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2410 if (drawVRules
&& (i
> -1))
2412 wxRect firstItemRect
;
2413 GetItemRect(0, firstItemRect
);
2415 if (GetItemRect(i
, itemRect
))
2418 int x
= itemRect
.GetX();
2419 for (col
= 0; col
< GetColumnCount(); col
++)
2421 int colWidth
= GetColumnWidth(col
);
2423 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2429 // ----------------------------------------------------------------------------
2430 // virtual list controls
2431 // ----------------------------------------------------------------------------
2433 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2435 // this is a pure virtual function, in fact - which is not really pure
2436 // because the controls which are not virtual don't need to implement it
2437 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2439 return wxEmptyString
;
2442 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2445 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2450 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2452 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2453 _T("invalid item index in OnGetItemAttr()") );
2455 // no attributes by default
2459 void wxListCtrl::SetItemCount(long count
)
2461 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2463 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2464 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2466 wxLogLastError(_T("ListView_SetItemCount"));
2469 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2470 wxT("m_count should match ListView_GetItemCount"));
2473 void wxListCtrl::RefreshItem(long item
)
2475 // strangely enough, ListView_Update() results in much more flicker here
2476 // than a dumb Refresh() -- why?
2478 if ( !ListView_Update(GetHwnd(), item
) )
2480 wxLogLastError(_T("ListView_Update"));
2484 GetItemRect(item
, rect
);
2489 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2491 wxRect rect1
, rect2
;
2492 GetItemRect(itemFrom
, rect1
);
2493 GetItemRect(itemTo
, rect2
);
2495 wxRect rect
= rect1
;
2496 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2501 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2504 it
.mask
= LVIF_PARAM
;
2507 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2509 return (wxListItemInternalData
*) it
.lParam
;
2514 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2516 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2519 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2521 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2528 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2530 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2534 memset(&item
, 0, sizeof(item
));
2535 item
.iItem
= itemId
;
2536 item
.mask
= LVIF_PARAM
;
2537 item
.lParam
= (LPARAM
) 0;
2538 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2543 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2547 wxListItemInternalData
*internaldata
=
2548 (wxListItemInternalData
*) lvItem
.lParam
;
2551 info
.m_data
= internaldata
->lParam
;
2555 info
.m_stateMask
= 0;
2556 info
.m_itemId
= lvItem
.iItem
;
2558 long oldMask
= lvItem
.mask
;
2560 bool needText
= FALSE
;
2561 if (hwndListCtrl
!= 0)
2563 if ( lvItem
.mask
& LVIF_TEXT
)
2570 lvItem
.pszText
= new wxChar
[513];
2571 lvItem
.cchTextMax
= 512;
2573 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2574 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2577 if ( lvItem
.mask
& LVIF_STATE
)
2579 info
.m_mask
|= wxLIST_MASK_STATE
;
2581 if ( lvItem
.stateMask
& LVIS_CUT
)
2583 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2584 if ( lvItem
.state
& LVIS_CUT
)
2585 info
.m_state
|= wxLIST_STATE_CUT
;
2587 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2589 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2590 if ( lvItem
.state
& LVIS_DROPHILITED
)
2591 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2593 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2595 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2596 if ( lvItem
.state
& LVIS_FOCUSED
)
2597 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2599 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2601 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2602 if ( lvItem
.state
& LVIS_SELECTED
)
2603 info
.m_state
|= wxLIST_STATE_SELECTED
;
2607 if ( lvItem
.mask
& LVIF_TEXT
)
2609 info
.m_mask
|= wxLIST_MASK_TEXT
;
2610 info
.m_text
= lvItem
.pszText
;
2612 if ( lvItem
.mask
& LVIF_IMAGE
)
2614 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2615 info
.m_image
= lvItem
.iImage
;
2617 if ( lvItem
.mask
& LVIF_PARAM
)
2618 info
.m_mask
|= wxLIST_MASK_DATA
;
2619 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2620 info
.m_mask
|= wxLIST_SET_ITEM
;
2621 info
.m_col
= lvItem
.iSubItem
;
2626 delete[] lvItem
.pszText
;
2628 lvItem
.mask
= oldMask
;
2631 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2633 if (stateMask
& wxLIST_STATE_CUT
)
2635 lvItem
.stateMask
|= LVIS_CUT
;
2636 if (state
& wxLIST_STATE_CUT
)
2637 lvItem
.state
|= LVIS_CUT
;
2639 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2641 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2642 if (state
& wxLIST_STATE_DROPHILITED
)
2643 lvItem
.state
|= LVIS_DROPHILITED
;
2645 if (stateMask
& wxLIST_STATE_FOCUSED
)
2647 lvItem
.stateMask
|= LVIS_FOCUSED
;
2648 if (state
& wxLIST_STATE_FOCUSED
)
2649 lvItem
.state
|= LVIS_FOCUSED
;
2651 if (stateMask
& wxLIST_STATE_SELECTED
)
2653 lvItem
.stateMask
|= LVIS_SELECTED
;
2654 if (state
& wxLIST_STATE_SELECTED
)
2655 lvItem
.state
|= LVIS_SELECTED
;
2659 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2660 const wxListItem
& info
,
2663 lvItem
.iItem
= (int) info
.m_itemId
;
2665 lvItem
.iImage
= info
.m_image
;
2666 lvItem
.stateMask
= 0;
2669 lvItem
.iSubItem
= info
.m_col
;
2671 if (info
.m_mask
& wxLIST_MASK_STATE
)
2673 lvItem
.mask
|= LVIF_STATE
;
2675 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2678 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2680 lvItem
.mask
|= LVIF_TEXT
;
2681 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2683 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2687 // pszText is not const, hence the cast
2688 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2689 if ( lvItem
.pszText
)
2690 lvItem
.cchTextMax
= info
.m_text
.Length();
2692 lvItem
.cchTextMax
= 0;
2695 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2696 lvItem
.mask
|= LVIF_IMAGE
;
2699 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2702 wxZeroMemory(lvCol
);
2704 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2706 lvCol
.mask
|= LVCF_TEXT
;
2707 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2710 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2712 lvCol
.mask
|= LVCF_FMT
;
2714 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2715 lvCol
.fmt
= LVCFMT_LEFT
;
2716 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2717 lvCol
.fmt
= LVCFMT_RIGHT
;
2718 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2719 lvCol
.fmt
= LVCFMT_CENTER
;
2722 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2724 lvCol
.mask
|= LVCF_WIDTH
;
2725 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2726 lvCol
.cx
= LVSCW_AUTOSIZE
;
2727 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2728 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2730 lvCol
.cx
= item
.m_width
;
2733 // see comment at the end of wxListCtrl::GetColumn()
2734 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2735 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2737 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2739 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2741 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2742 // seem to be generally nicer than on the left and the generic
2743 // version only draws them on the right (we don't have a flag to
2744 // specify the image location anyhow)
2746 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2747 // make any difference in my tests -- but maybe we should?
2748 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2750 lvCol
.iImage
= item
.m_image
;
2752 //else: it doesn't support item images anyhow
2754 #endif // _WIN32_IE >= 0x0300
2757 #endif // wxUSE_LISTCTRL