1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
38 #include "wx/settings.h"
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
46 #include "wx/msw/private.h"
48 #if defined(__WXWINCE__)
56 // include <commctrl.h> "properly"
57 #include "wx/msw/wrapcctl.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // convert our state and mask flags to LV_ITEM constants
64 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
66 // convert wxListItem to LV_ITEM
67 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
68 const wxListItem
& info
, LV_ITEM
& lvItem
);
70 // convert LV_ITEM to wxListItem
71 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
73 /* const */ LV_ITEM
& lvItem
);
75 // convert our wxListItem to LV_COLUMN
76 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
79 // ----------------------------------------------------------------------------
80 // private helper classes
81 // ----------------------------------------------------------------------------
83 // We have to handle both fooW and fooA notifications in several cases
84 // because of broken comctl32.dll and/or unicows.dll. This class is used to
85 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
86 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
87 // by wxConvertToMSWListItem().
89 #define LV_ITEM_NATIVE LV_ITEMW
90 #define LV_ITEM_OTHER LV_ITEMA
92 #define LV_CONV_TO_WX cMB2WX
93 #define LV_CONV_BUF wxMB2WXbuf
95 #define LV_ITEM_NATIVE LV_ITEMA
96 #define LV_ITEM_OTHER LV_ITEMW
98 #define LV_CONV_TO_WX cWC2WX
99 #define LV_CONV_BUF wxWC2WXbuf
100 #endif // Unicode/ANSI
105 // default ctor, use Init() later
106 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
108 // init without conversion
109 void Init(LV_ITEM_NATIVE
& item
)
111 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
116 // init with conversion
117 void Init(LV_ITEM_OTHER
& item
)
119 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
120 // point to our own m_item
122 // memcpy() can't work if the struct sizes are different
123 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
124 CodeCantWorkIfDiffSizes
);
126 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
128 // convert text from ANSI to Unicod if necessary
129 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
131 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
132 m_item
.pszText
= (wxChar
*)m_buf
->data();
136 // ctor without conversion
137 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
139 // ctor with conversion
140 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
145 ~wxLV_ITEM() { delete m_buf
; }
147 // conversion to the real LV_ITEM
148 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
153 LV_ITEM_NATIVE
*m_pItem
;
154 LV_ITEM_NATIVE m_item
;
156 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
159 ///////////////////////////////////////////////////////
161 // The MSW version had problems with SetTextColour() et
162 // al as the wxListItemAttr's were stored keyed on the
163 // item index. If a item was inserted anywhere but the end
164 // of the list the the text attributes (colour etc) for
165 // the following items were out of sync.
168 // Under MSW the only way to associate data with a List
169 // item independant of its position in the list is to
170 // store a pointer to it in its lParam attribute. However
171 // user programs are already using this (via the
172 // SetItemData() GetItemData() calls).
174 // However what we can do is store a pointer to a
175 // structure which contains the attributes we want *and*
176 // a lParam for the users data, e.g.
178 // class wxListItemInternalData
181 // wxListItemAttr *attr;
182 // long lParam; // user data
185 // To conserve memory, a wxListItemInternalData is
186 // only allocated for a LV_ITEM if text attributes or
187 // user data(lparam) are being set.
190 // class wxListItemInternalData
191 class wxListItemInternalData
194 wxListItemAttr
*attr
;
195 LPARAM lParam
; // user data
197 wxListItemInternalData() : attr(NULL
), lParam(0) {}
198 ~wxListItemInternalData()
204 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
207 // Get the internal data structure
208 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
209 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
210 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
211 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
237 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
238 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
239 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
241 #if wxUSE_EXTENDED_RTTI
242 WX_DEFINE_FLAGS( wxListCtrlStyle
)
244 wxBEGIN_FLAGS( wxListCtrlStyle
)
245 // new style border flags, we put them first to
246 // use them for streaming out
247 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
248 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
249 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
250 wxFLAGS_MEMBER(wxBORDER_RAISED
)
251 wxFLAGS_MEMBER(wxBORDER_STATIC
)
252 wxFLAGS_MEMBER(wxBORDER_NONE
)
254 // old style border flags
255 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
256 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
257 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
258 wxFLAGS_MEMBER(wxRAISED_BORDER
)
259 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
260 wxFLAGS_MEMBER(wxBORDER
)
262 // standard window styles
263 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
264 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
265 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
266 wxFLAGS_MEMBER(wxWANTS_CHARS
)
267 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
268 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
269 wxFLAGS_MEMBER(wxVSCROLL
)
270 wxFLAGS_MEMBER(wxHSCROLL
)
272 wxFLAGS_MEMBER(wxLC_LIST
)
273 wxFLAGS_MEMBER(wxLC_REPORT
)
274 wxFLAGS_MEMBER(wxLC_ICON
)
275 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
276 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
277 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
278 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
279 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
280 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
281 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
282 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
283 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
284 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
285 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
287 wxEND_FLAGS( wxListCtrlStyle
)
289 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
291 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
292 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
294 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
295 wxEND_PROPERTIES_TABLE()
297 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
298 wxEND_HANDLERS_TABLE()
300 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
303 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
306 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
309 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
310 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
312 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
314 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
315 EVT_PAINT(wxListCtrl::OnPaint
)
318 // ============================================================================
320 // ============================================================================
322 // ----------------------------------------------------------------------------
323 // wxListCtrl construction
324 // ----------------------------------------------------------------------------
326 void wxListCtrl::Init()
328 m_imageListNormal
= NULL
;
329 m_imageListSmall
= NULL
;
330 m_imageListState
= NULL
;
331 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
335 m_ignoreChangeMessages
= FALSE
;
337 m_AnyInternalData
= FALSE
;
338 m_hasAnyAttr
= FALSE
;
341 bool wxListCtrl::Create(wxWindow
*parent
,
346 const wxValidator
& wxVALIDATOR_PARAM(validator
),
347 const wxString
& name
)
350 SetValidator(validator
);
351 #endif // wxUSE_VALIDATORS
360 m_windowStyle
= style
;
373 m_windowId
= (id
== -1) ? NewControlId() : id
;
375 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
376 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
378 m_baseStyle
= wstyle
;
380 if ( !DoCreateControl(x
, y
, width
, height
) )
384 parent
->AddChild(this);
389 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
391 DWORD wstyle
= m_baseStyle
;
394 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
396 long oldStyle
= 0; // Dummy
397 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
398 wstyle
|= standardStyle
;
400 // Create the ListView control.
401 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
406 GetWinHwnd(GetParent()),
413 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
418 // explicitly say that we want to use Unicode because otherwise we get ANSI
419 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
421 ::SendMessage(GetHwnd(), LVM_SETUNICODEFORMAT
, TRUE
, 0);
424 // for comctl32.dll v 4.70+ we want to have this attribute because it's
425 // prettier (and also because wxGTK does it like this)
426 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
428 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
429 0, LVS_EX_FULLROWSELECT
);
432 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
433 SetForegroundColour(GetParent()->GetForegroundColour());
440 void wxListCtrl::UpdateStyle()
444 // The new window view style
446 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
447 dwStyleNew
|= m_baseStyle
;
449 // Get the current window style.
450 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
452 // Only set the window style if the view bits have changed.
453 if ( dwStyleOld
!= dwStyleNew
)
455 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
460 void wxListCtrl::FreeAllInternalData()
462 if (m_AnyInternalData
)
464 int n
= GetItemCount();
467 m_ignoreChangeMessages
= TRUE
;
468 for (i
= 0; i
< n
; i
++)
469 wxDeleteInternalData(this, i
);
470 m_ignoreChangeMessages
= FALSE
;
472 m_AnyInternalData
= FALSE
;
476 wxListCtrl::~wxListCtrl()
478 FreeAllInternalData();
482 m_textCtrl
->UnsubclassWin();
483 m_textCtrl
->SetHWND(0);
488 if (m_ownsImageListNormal
) delete m_imageListNormal
;
489 if (m_ownsImageListSmall
) delete m_imageListSmall
;
490 if (m_ownsImageListState
) delete m_imageListState
;
493 // ----------------------------------------------------------------------------
494 // set/get/change style
495 // ----------------------------------------------------------------------------
497 // Add or remove a single window style
498 void wxListCtrl::SetSingleStyle(long style
, bool add
)
500 long flag
= GetWindowStyleFlag();
502 // Get rid of conflicting styles
505 if ( style
& wxLC_MASK_TYPE
)
506 flag
= flag
& ~wxLC_MASK_TYPE
;
507 if ( style
& wxLC_MASK_ALIGN
)
508 flag
= flag
& ~wxLC_MASK_ALIGN
;
509 if ( style
& wxLC_MASK_SORT
)
510 flag
= flag
& ~wxLC_MASK_SORT
;
526 m_windowStyle
= flag
;
531 // Set the whole window style
532 void wxListCtrl::SetWindowStyleFlag(long flag
)
534 m_windowStyle
= flag
;
539 // Can be just a single style, or a bitlist
540 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
543 if ( style
& wxLC_ICON
)
545 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
546 oldStyle
-= LVS_SMALLICON
;
547 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
548 oldStyle
-= LVS_REPORT
;
549 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
550 oldStyle
-= LVS_LIST
;
554 if ( style
& wxLC_SMALL_ICON
)
556 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
557 oldStyle
-= LVS_ICON
;
558 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
559 oldStyle
-= LVS_REPORT
;
560 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
561 oldStyle
-= LVS_LIST
;
562 wstyle
|= LVS_SMALLICON
;
565 if ( style
& wxLC_LIST
)
567 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
568 oldStyle
-= LVS_ICON
;
569 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
570 oldStyle
-= LVS_REPORT
;
571 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
572 oldStyle
-= LVS_SMALLICON
;
576 if ( style
& wxLC_REPORT
)
578 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
579 oldStyle
-= LVS_ICON
;
580 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
581 oldStyle
-= LVS_LIST
;
582 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
583 oldStyle
-= LVS_SMALLICON
;
585 wstyle
|= LVS_REPORT
;
588 if ( style
& wxLC_ALIGN_LEFT
)
590 if ( oldStyle
& LVS_ALIGNTOP
)
591 oldStyle
-= LVS_ALIGNTOP
;
592 wstyle
|= LVS_ALIGNLEFT
;
595 if ( style
& wxLC_ALIGN_TOP
)
597 if ( oldStyle
& LVS_ALIGNLEFT
)
598 oldStyle
-= LVS_ALIGNLEFT
;
599 wstyle
|= LVS_ALIGNTOP
;
602 if ( style
& wxLC_AUTOARRANGE
)
603 wstyle
|= LVS_AUTOARRANGE
;
605 if ( style
& wxLC_NO_SORT_HEADER
)
606 wstyle
|= LVS_NOSORTHEADER
;
608 if ( style
& wxLC_NO_HEADER
)
609 wstyle
|= LVS_NOCOLUMNHEADER
;
611 if ( style
& wxLC_EDIT_LABELS
)
612 wstyle
|= LVS_EDITLABELS
;
614 if ( style
& wxLC_SINGLE_SEL
)
615 wstyle
|= LVS_SINGLESEL
;
617 if ( style
& wxLC_SORT_ASCENDING
)
619 if ( oldStyle
& LVS_SORTDESCENDING
)
620 oldStyle
-= LVS_SORTDESCENDING
;
621 wstyle
|= LVS_SORTASCENDING
;
624 if ( style
& wxLC_SORT_DESCENDING
)
626 if ( oldStyle
& LVS_SORTASCENDING
)
627 oldStyle
-= LVS_SORTASCENDING
;
628 wstyle
|= LVS_SORTDESCENDING
;
631 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
632 if ( style
& wxLC_VIRTUAL
)
634 int ver
= wxTheApp
->GetComCtl32Version();
637 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."),
638 ver
/ 100, ver
% 100);
641 wstyle
|= LVS_OWNERDATA
;
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
652 // Sets the foreground, i.e. text, colour
653 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
655 if ( !wxWindow::SetForegroundColour(col
) )
658 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
663 // Sets the background colour
664 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
666 if ( !wxWindow::SetBackgroundColour(col
) )
669 // we set the same colour for both the "empty" background and the items
671 COLORREF color
= wxColourToRGB(col
);
672 ListView_SetBkColor(GetHwnd(), color
);
673 ListView_SetTextBkColor(GetHwnd(), color
);
678 // Gets information about this column
679 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
684 lvCol
.mask
= LVCF_WIDTH
;
686 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
688 lvCol
.mask
|= LVCF_TEXT
;
689 lvCol
.pszText
= new wxChar
[513];
690 lvCol
.cchTextMax
= 512;
693 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
695 lvCol
.mask
|= LVCF_FMT
;
698 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
700 lvCol
.mask
|= LVCF_IMAGE
;
703 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
705 // item.m_subItem = lvCol.iSubItem;
706 item
.m_width
= lvCol
.cx
;
708 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
710 item
.m_text
= lvCol
.pszText
;
711 delete[] lvCol
.pszText
;
714 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
716 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
718 item
.m_format
= wxLIST_FORMAT_LEFT
;
721 item
.m_format
= wxLIST_FORMAT_RIGHT
;
724 item
.m_format
= wxLIST_FORMAT_CENTRE
;
727 item
.m_format
= -1; // Unknown?
732 // the column images were not supported in older versions but how to check
733 // for this? we can't use _WIN32_IE because we always define it to a very
734 // high value, so see if another symbol which is only defined starting from
735 // comctl32.dll 4.70 is available
736 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
737 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
739 item
.m_image
= lvCol
.iImage
;
741 #endif // LVCOLUMN::iImage exists
746 // Sets information about this column
747 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
750 wxConvertToMSWListCol(col
, item
, lvCol
);
752 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
755 // Gets the column width
756 int wxListCtrl::GetColumnWidth(int col
) const
758 return ListView_GetColumnWidth(GetHwnd(), col
);
761 // Sets the column width
762 bool wxListCtrl::SetColumnWidth(int col
, int width
)
765 if ( m_windowStyle
& wxLC_LIST
)
769 if ( width2
== wxLIST_AUTOSIZE
)
770 width2
= LVSCW_AUTOSIZE
;
771 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
772 width2
= LVSCW_AUTOSIZE_USEHEADER
;
774 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
777 // Gets the number of items that can fit vertically in the
778 // visible area of the list control (list or report view)
779 // or the total number of items in the list control (icon
780 // or small icon view)
781 int wxListCtrl::GetCountPerPage() const
783 return ListView_GetCountPerPage(GetHwnd());
786 // Gets the edit control for editing labels.
787 wxTextCtrl
* wxListCtrl::GetEditControl() const
792 // Gets information about the item
793 bool wxListCtrl::GetItem(wxListItem
& info
) const
796 wxZeroMemory(lvItem
);
798 lvItem
.iItem
= info
.m_itemId
;
799 lvItem
.iSubItem
= info
.m_col
;
801 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
803 lvItem
.mask
|= LVIF_TEXT
;
804 lvItem
.pszText
= new wxChar
[513];
805 lvItem
.cchTextMax
= 512;
809 lvItem
.pszText
= NULL
;
812 if (info
.m_mask
& wxLIST_MASK_DATA
)
813 lvItem
.mask
|= LVIF_PARAM
;
815 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
816 lvItem
.mask
|= LVIF_IMAGE
;
818 if ( info
.m_mask
& wxLIST_MASK_STATE
)
820 lvItem
.mask
|= LVIF_STATE
;
821 // the other bits are hardly interesting anyhow
822 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
825 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
828 wxLogError(_("Couldn't retrieve information about list control item %d."),
833 // give NULL as hwnd as we already have everything we need
834 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
838 delete[] lvItem
.pszText
;
843 // Sets information about the item
844 bool wxListCtrl::SetItem(wxListItem
& info
)
847 wxConvertToMSWListItem(this, info
, item
);
849 // we never update the lParam if it contains our pointer
850 // to the wxListItemInternalData structure
851 item
.mask
&= ~LVIF_PARAM
;
853 // check if setting attributes or lParam
854 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
856 // get internal item data
857 // perhaps a cache here ?
858 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
863 m_AnyInternalData
= TRUE
;
864 data
= new wxListItemInternalData();
865 item
.lParam
= (LPARAM
) data
;
866 item
.mask
|= LVIF_PARAM
;
871 if (info
.m_mask
& wxLIST_MASK_DATA
)
872 data
->lParam
= info
.m_data
;
875 if (info
.HasAttributes())
878 *data
->attr
= *info
.GetAttributes();
880 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
885 // we could be changing only the attribute in which case we don't need to
886 // call ListView_SetItem() at all
890 if ( !ListView_SetItem(GetHwnd(), &item
) )
892 wxLogDebug(_T("ListView_SetItem() failed"));
898 // we need to update the item immediately to show the new image
899 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
901 // check whether it has any custom attributes
902 if ( info
.HasAttributes() )
906 // if the colour has changed, we must redraw the item
912 // we need this to make the change visible right now
913 RefreshItem(item
.iItem
);
919 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
923 info
.m_mask
= wxLIST_MASK_TEXT
;
924 info
.m_itemId
= index
;
928 info
.m_image
= imageId
;
929 info
.m_mask
|= wxLIST_MASK_IMAGE
;
931 return SetItem(info
);
935 // Gets the item state
936 int wxListCtrl::GetItemState(long item
, long stateMask
) const
940 info
.m_mask
= wxLIST_MASK_STATE
;
941 info
.m_stateMask
= stateMask
;
942 info
.m_itemId
= item
;
950 // Sets the item state
951 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
953 // NB: don't use SetItem() here as it doesn't work with the virtual list
956 wxZeroMemory(lvItem
);
958 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
960 // for the virtual list controls we need to refresh the previously focused
961 // item manually when changing focus without changing selection
962 // programmatically because otherwise it keeps its focus rectangle until
963 // next repaint (yet another comctl32 bug)
966 (stateMask
& wxLIST_STATE_FOCUSED
) &&
967 (state
& wxLIST_STATE_FOCUSED
) )
969 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
976 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
977 (WPARAM
)item
, (LPARAM
)&lvItem
) )
979 wxLogLastError(_T("ListView_SetItemState"));
984 if ( focusOld
!= -1 )
986 // no need to refresh the item if it was previously selected, it would
987 // only result in annoying flicker
988 if ( !(GetItemState(focusOld
,
989 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
991 RefreshItem(focusOld
);
998 // Sets the item image
999 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
1003 info
.m_mask
= wxLIST_MASK_IMAGE
;
1004 info
.m_image
= image
;
1005 info
.m_itemId
= item
;
1007 return SetItem(info
);
1010 // Gets the item text
1011 wxString
wxListCtrl::GetItemText(long item
) const
1015 info
.m_mask
= wxLIST_MASK_TEXT
;
1016 info
.m_itemId
= item
;
1019 return wxEmptyString
;
1023 // Sets the item text
1024 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
1028 info
.m_mask
= wxLIST_MASK_TEXT
;
1029 info
.m_itemId
= item
;
1035 // Gets the item data
1036 long wxListCtrl::GetItemData(long item
) const
1040 info
.m_mask
= wxLIST_MASK_DATA
;
1041 info
.m_itemId
= item
;
1048 // Sets the item data
1049 bool wxListCtrl::SetItemData(long item
, long data
)
1053 info
.m_mask
= wxLIST_MASK_DATA
;
1054 info
.m_itemId
= item
;
1057 return SetItem(info
);
1060 wxRect
wxListCtrl::GetViewRect() const
1062 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
1063 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1066 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1068 wxLogDebug(_T("ListView_GetViewRect() failed."));
1074 wxCopyRECTToRect(rc
, rect
);
1079 // Gets the item rectangle
1080 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1085 if ( code
== wxLIST_RECT_BOUNDS
)
1086 codeWin
= LVIR_BOUNDS
;
1087 else if ( code
== wxLIST_RECT_ICON
)
1088 codeWin
= LVIR_ICON
;
1089 else if ( code
== wxLIST_RECT_LABEL
)
1090 codeWin
= LVIR_LABEL
;
1093 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1095 codeWin
= LVIR_BOUNDS
;
1098 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1100 rect
.x
= rectWin
.left
;
1101 rect
.y
= rectWin
.top
;
1102 rect
.width
= rectWin
.right
- rectWin
.left
;
1103 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1108 // Gets the item position
1109 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1113 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1115 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1119 // Sets the item position.
1120 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1122 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1125 // Gets the number of items in the list control
1126 int wxListCtrl::GetItemCount() const
1131 wxSize
wxListCtrl::GetItemSpacing() const
1133 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1135 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1138 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1140 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1143 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1146 info
.m_itemId
= item
;
1147 info
.SetTextColour( col
);
1151 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1154 info
.m_itemId
= item
;
1156 return info
.GetTextColour();
1159 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1162 info
.m_itemId
= item
;
1163 info
.SetBackgroundColour( col
);
1167 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1170 info
.m_itemId
= item
;
1172 return info
.GetBackgroundColour();
1175 // Gets the number of selected items in the list control
1176 int wxListCtrl::GetSelectedItemCount() const
1178 return ListView_GetSelectedCount(GetHwnd());
1181 // Gets the text colour of the listview
1182 wxColour
wxListCtrl::GetTextColour() const
1184 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1185 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1189 // Sets the text colour of the listview
1190 void wxListCtrl::SetTextColour(const wxColour
& col
)
1192 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1195 // Gets the index of the topmost visible item when in
1196 // list or report view
1197 long wxListCtrl::GetTopItem() const
1199 return (long) ListView_GetTopIndex(GetHwnd());
1202 // Searches for an item, starting from 'item'.
1203 // 'geometry' is one of
1204 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1205 // 'state' is a state bit flag, one or more of
1206 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1207 // item can be -1 to find the first item that matches the
1209 // Returns the item or -1 if unsuccessful.
1210 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1214 if ( geom
== wxLIST_NEXT_ABOVE
)
1215 flags
|= LVNI_ABOVE
;
1216 if ( geom
== wxLIST_NEXT_ALL
)
1218 if ( geom
== wxLIST_NEXT_BELOW
)
1219 flags
|= LVNI_BELOW
;
1220 if ( geom
== wxLIST_NEXT_LEFT
)
1221 flags
|= LVNI_TOLEFT
;
1222 if ( geom
== wxLIST_NEXT_RIGHT
)
1223 flags
|= LVNI_TORIGHT
;
1225 if ( state
& wxLIST_STATE_CUT
)
1227 if ( state
& wxLIST_STATE_DROPHILITED
)
1228 flags
|= LVNI_DROPHILITED
;
1229 if ( state
& wxLIST_STATE_FOCUSED
)
1230 flags
|= LVNI_FOCUSED
;
1231 if ( state
& wxLIST_STATE_SELECTED
)
1232 flags
|= LVNI_SELECTED
;
1234 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1238 wxImageList
*wxListCtrl::GetImageList(int which
) const
1240 if ( which
== wxIMAGE_LIST_NORMAL
)
1242 return m_imageListNormal
;
1244 else if ( which
== wxIMAGE_LIST_SMALL
)
1246 return m_imageListSmall
;
1248 else if ( which
== wxIMAGE_LIST_STATE
)
1250 return m_imageListState
;
1255 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1258 if ( which
== wxIMAGE_LIST_NORMAL
)
1260 flags
= LVSIL_NORMAL
;
1261 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1262 m_imageListNormal
= imageList
;
1263 m_ownsImageListNormal
= FALSE
;
1265 else if ( which
== wxIMAGE_LIST_SMALL
)
1267 flags
= LVSIL_SMALL
;
1268 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1269 m_imageListSmall
= imageList
;
1270 m_ownsImageListSmall
= FALSE
;
1272 else if ( which
== wxIMAGE_LIST_STATE
)
1274 flags
= LVSIL_STATE
;
1275 if (m_ownsImageListState
) delete m_imageListState
;
1276 m_imageListState
= imageList
;
1277 m_ownsImageListState
= FALSE
;
1279 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1282 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1284 SetImageList(imageList
, which
);
1285 if ( which
== wxIMAGE_LIST_NORMAL
)
1286 m_ownsImageListNormal
= TRUE
;
1287 else if ( which
== wxIMAGE_LIST_SMALL
)
1288 m_ownsImageListSmall
= TRUE
;
1289 else if ( which
== wxIMAGE_LIST_STATE
)
1290 m_ownsImageListState
= TRUE
;
1293 // ----------------------------------------------------------------------------
1295 // ----------------------------------------------------------------------------
1297 // Arranges the items
1298 bool wxListCtrl::Arrange(int flag
)
1301 if ( flag
== wxLIST_ALIGN_LEFT
)
1302 code
= LVA_ALIGNLEFT
;
1303 else if ( flag
== wxLIST_ALIGN_TOP
)
1304 code
= LVA_ALIGNTOP
;
1305 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1307 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1308 code
= LVA_SNAPTOGRID
;
1310 return (ListView_Arrange(GetHwnd(), code
) != 0);
1314 bool wxListCtrl::DeleteItem(long item
)
1316 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1318 wxLogLastError(_T("ListView_DeleteItem"));
1323 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1324 wxT("m_count should match ListView_GetItemCount"));
1326 // the virtual list control doesn't refresh itself correctly, help it
1329 // we need to refresh all the lines below the one which was deleted
1331 if ( item
> 0 && GetItemCount() )
1333 GetItemRect(item
- 1, rectItem
);
1338 rectItem
.height
= 0;
1341 wxRect rectWin
= GetRect();
1342 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1343 rectWin
.y
= rectItem
.GetBottom();
1345 RefreshRect(rectWin
);
1351 // Deletes all items
1352 bool wxListCtrl::DeleteAllItems()
1354 FreeAllInternalData();
1355 return ListView_DeleteAllItems(GetHwnd()) != 0;
1358 // Deletes all items
1359 bool wxListCtrl::DeleteAllColumns()
1361 while ( m_colCount
> 0 )
1363 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1365 wxLogLastError(wxT("ListView_DeleteColumn"));
1373 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1379 bool wxListCtrl::DeleteColumn(int col
)
1381 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1383 if ( success
&& (m_colCount
> 0) )
1388 // Clears items, and columns if there are any.
1389 void wxListCtrl::ClearAll()
1392 if ( m_colCount
> 0 )
1396 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1398 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1400 // ListView_EditLabel requires that the list has focus.
1403 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1406 // failed to start editing
1410 // [re]create the text control wrapping the HWND we got
1413 m_textCtrl
->UnsubclassWin();
1414 m_textCtrl
->SetHWND(0);
1418 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1419 m_textCtrl
->SetHWND(hWnd
);
1420 m_textCtrl
->SubclassWin(hWnd
);
1421 m_textCtrl
->SetParent(this);
1423 // we must disallow TABbing away from the control while the edit contol is
1424 // shown because this leaves it in some strange state (just try removing
1425 // this line and then pressing TAB while editing an item in listctrl
1427 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1432 // End label editing, optionally cancelling the edit
1433 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1435 wxFAIL_MSG( _T("not implemented") );
1440 // Ensures this item is visible
1441 bool wxListCtrl::EnsureVisible(long item
)
1443 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1446 // Find an item whose label matches this string, starting from the item after 'start'
1447 // or the beginning if 'start' is -1.
1448 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1450 LV_FINDINFO findInfo
;
1452 findInfo
.flags
= LVFI_STRING
;
1454 findInfo
.flags
|= LVFI_PARTIAL
;
1457 // ListView_FindItem() excludes the first item from search and to look
1458 // through all the items you need to start from -1 which is unnatural and
1459 // inconsistent with the generic version - so we adjust the index
1462 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1465 // Find an item whose data matches this data, starting from the item after 'start'
1466 // or the beginning if 'start' is -1.
1467 // NOTE : Lindsay Mathieson - 14-July-2002
1468 // No longer use ListView_FindItem as the data attribute is now stored
1469 // in a wxListItemInternalData structure refernced by the actual lParam
1470 long wxListCtrl::FindItem(long start
, long data
)
1472 long idx
= start
+ 1;
1473 long count
= GetItemCount();
1477 if (GetItemData(idx
) == data
)
1485 // Find an item nearest this position in the specified direction, starting from
1486 // the item after 'start' or the beginning if 'start' is -1.
1487 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1489 LV_FINDINFO findInfo
;
1491 findInfo
.flags
= LVFI_NEARESTXY
;
1492 findInfo
.pt
.x
= pt
.x
;
1493 findInfo
.pt
.y
= pt
.y
;
1494 findInfo
.vkDirection
= VK_RIGHT
;
1496 if ( direction
== wxLIST_FIND_UP
)
1497 findInfo
.vkDirection
= VK_UP
;
1498 else if ( direction
== wxLIST_FIND_DOWN
)
1499 findInfo
.vkDirection
= VK_DOWN
;
1500 else if ( direction
== wxLIST_FIND_LEFT
)
1501 findInfo
.vkDirection
= VK_LEFT
;
1502 else if ( direction
== wxLIST_FIND_RIGHT
)
1503 findInfo
.vkDirection
= VK_RIGHT
;
1505 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1508 // Determines which item (if any) is at the specified point,
1509 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1510 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1512 LV_HITTESTINFO hitTestInfo
;
1513 hitTestInfo
.pt
.x
= (int) point
.x
;
1514 hitTestInfo
.pt
.y
= (int) point
.y
;
1516 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1519 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1520 flags
|= wxLIST_HITTEST_ABOVE
;
1521 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1522 flags
|= wxLIST_HITTEST_BELOW
;
1523 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1524 flags
|= wxLIST_HITTEST_NOWHERE
;
1525 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1526 flags
|= wxLIST_HITTEST_ONITEMICON
;
1527 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1528 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1529 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1530 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1531 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1532 flags
|= wxLIST_HITTEST_TOLEFT
;
1533 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1534 flags
|= wxLIST_HITTEST_TORIGHT
;
1536 return (long) hitTestInfo
.iItem
;
1539 // Inserts an item, returning the index of the new item if successful,
1541 long wxListCtrl::InsertItem(wxListItem
& info
)
1543 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1546 wxConvertToMSWListItem(this, info
, item
);
1547 item
.mask
&= ~LVIF_PARAM
;
1549 // check wether we need to allocate our internal data
1550 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1551 if (needInternalData
)
1553 m_AnyInternalData
= TRUE
;
1554 item
.mask
|= LVIF_PARAM
;
1556 // internal stucture that manages data
1557 wxListItemInternalData
*data
= new wxListItemInternalData();
1558 item
.lParam
= (LPARAM
) data
;
1560 if (info
.m_mask
& wxLIST_MASK_DATA
)
1561 data
->lParam
= info
.m_data
;
1563 // check whether it has any custom attributes
1564 if ( info
.HasAttributes() )
1566 // take copy of attributes
1567 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1571 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1574 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1575 wxT("m_count should match ListView_GetItemCount"));
1580 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1583 info
.m_text
= label
;
1584 info
.m_mask
= wxLIST_MASK_TEXT
;
1585 info
.m_itemId
= index
;
1586 return InsertItem(info
);
1589 // Inserts an image item
1590 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1593 info
.m_image
= imageIndex
;
1594 info
.m_mask
= wxLIST_MASK_IMAGE
;
1595 info
.m_itemId
= index
;
1596 return InsertItem(info
);
1599 // Inserts an image/string item
1600 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1603 info
.m_image
= imageIndex
;
1604 info
.m_text
= label
;
1605 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1606 info
.m_itemId
= index
;
1607 return InsertItem(info
);
1610 // For list view mode (only), inserts a column.
1611 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1614 wxConvertToMSWListCol(col
, item
, lvCol
);
1616 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1618 // always give some width to the new column: this one is compatible
1619 // with the generic version
1620 lvCol
.mask
|= LVCF_WIDTH
;
1624 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1629 else // failed to insert?
1631 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1638 long wxListCtrl::InsertColumn(long col
,
1639 const wxString
& heading
,
1644 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1645 item
.m_text
= heading
;
1648 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1649 item
.m_width
= width
;
1651 item
.m_format
= format
;
1653 return InsertColumn(col
, item
);
1656 // scroll the control by the given number of pixels (exception: in list view,
1657 // dx is interpreted as number of columns)
1658 bool wxListCtrl::ScrollList(int dx
, int dy
)
1660 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1662 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1672 // fn is a function which takes 3 long arguments: item1, item2, data.
1673 // item1 is the long data associated with a first item (NOT the index).
1674 // item2 is the long data associated with a second item (NOT the index).
1675 // data is the same value as passed to SortItems.
1676 // The return value is a negative number if the first item should precede the second
1677 // item, a positive number of the second item should precede the first,
1678 // or zero if the two items are equivalent.
1680 // data is arbitrary data to be passed to the sort function.
1682 // Internal structures for proxying the user compare function
1683 // so that we can pass it the *real* user data
1685 // translate lParam data and call user func
1686 struct wxInternalDataSort
1688 wxListCtrlCompare user_fn
;
1692 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1694 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1696 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1697 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1699 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1700 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1702 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1706 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1708 struct wxInternalDataSort internalData
;
1709 internalData
.user_fn
= fn
;
1710 internalData
.data
= data
;
1712 // WPARAM cast is needed for mingw/cygwin
1713 if ( !ListView_SortItems(GetHwnd(),
1714 wxInternalDataCompareFunc
,
1715 (WPARAM
) &internalData
) )
1717 wxLogDebug(_T("ListView_SortItems() failed"));
1727 // ----------------------------------------------------------------------------
1728 // message processing
1729 // ----------------------------------------------------------------------------
1731 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1733 if (cmd
== EN_UPDATE
)
1735 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1736 event
.SetEventObject( this );
1737 ProcessCommand(event
);
1740 else if (cmd
== EN_KILLFOCUS
)
1742 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1743 event
.SetEventObject( this );
1744 ProcessCommand(event
);
1751 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1754 // prepare the event
1755 // -----------------
1757 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1758 event
.SetEventObject(this);
1760 wxEventType eventType
= wxEVT_NULL
;
1762 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1764 // if your compiler is as broken as this, you should really change it: this
1765 // code is needed for normal operation! #ifdef below is only useful for
1766 // automatic rebuilds which are done with a very old compiler version
1767 #ifdef HDN_BEGINTRACKA
1769 // check for messages from the header (in report view)
1770 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1772 // is it a message from the header?
1773 if ( nmhdr
->hwndFrom
== hwndHdr
)
1775 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1777 event
.m_itemIndex
= -1;
1779 switch ( nmhdr
->code
)
1781 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1782 // TRACK messages even to ANSI programs: on my system I get
1783 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1785 // work around is to simply catch both versions and hope that it
1786 // works (why should this message exist in ANSI and Unicode is
1787 // beyond me as it doesn't deal with strings at all...)
1789 // note that fr HDN_TRACK another possibility could be to use
1790 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1791 // something other than the item width changes so we'd have to
1792 // filter out the unwanted events then
1793 case HDN_BEGINTRACKA
:
1794 case HDN_BEGINTRACKW
:
1795 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1800 if ( eventType
== wxEVT_NULL
)
1801 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1806 if ( eventType
== wxEVT_NULL
)
1807 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1809 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1810 event
.m_col
= nmHDR
->iItem
;
1813 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1814 case GN_CONTEXTMENU
:
1815 #endif //__WXWINCE__
1818 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1821 // find the column clicked: we have to search for it
1822 // ourselves as the notification message doesn't provide
1825 // where did the click occur?
1827 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1828 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1829 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1831 #endif //__WXWINCE__
1832 if ( !::GetCursorPos(&ptClick
) )
1834 wxLogLastError(_T("GetCursorPos"));
1837 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1839 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1842 event
.m_pointDrag
.x
= ptClick
.x
;
1843 event
.m_pointDrag
.y
= ptClick
.y
;
1845 int colCount
= Header_GetItemCount(hwndHdr
);
1848 for ( int col
= 0; col
< colCount
; col
++ )
1850 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1852 if ( ::PtInRect(&rect
, ptClick
) )
1862 case HDN_GETDISPINFOW
:
1864 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1865 // This is a fix for a strange bug under XP.
1866 // Normally, info->iItem is a valid index, but
1867 // sometimes this is a silly (large) number
1868 // and when we return FALSE via wxControl::MSWOnNotify
1869 // to indicate that it hasn't yet been processed,
1870 // there's a GPF in Windows.
1871 // By returning TRUE here, we avoid further processing
1872 // of this strange message.
1873 if ( info
->iItem
>= GetColumnCount() )
1879 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1883 #endif // defined(HDN_BEGINTRACKA)
1884 if ( nmhdr
->hwndFrom
== GetHwnd() )
1886 // almost all messages use NM_LISTVIEW
1887 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1889 const int iItem
= nmLV
->iItem
;
1892 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1893 // ignored for efficiency. It is done here because the internal data is in the
1894 // process of being deleted so we don't want to try and access it below.
1895 if ( m_ignoreChangeMessages
&&
1896 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1902 // If we have a valid item then check if there is a data value
1903 // associated with it and put it in the event.
1904 if ( iItem
>= 0 && iItem
< GetItemCount() )
1906 wxListItemInternalData
*internaldata
=
1907 wxGetInternalData(GetHwnd(), iItem
);
1910 event
.m_item
.m_data
= internaldata
->lParam
;
1914 switch ( nmhdr
->code
)
1916 case LVN_BEGINRDRAG
:
1917 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1921 if ( eventType
== wxEVT_NULL
)
1923 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1926 event
.m_itemIndex
= iItem
;
1927 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1928 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1931 // NB: we have to handle both *A and *W versions here because some
1932 // versions of comctl32.dll send ANSI messages even to the
1934 case LVN_BEGINLABELEDITA
:
1935 case LVN_BEGINLABELEDITW
:
1938 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1940 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1942 else // LVN_BEGINLABELEDITW
1944 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1947 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1948 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1949 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1953 case LVN_ENDLABELEDITA
:
1954 case LVN_ENDLABELEDITW
:
1957 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1959 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1961 else // LVN_ENDLABELEDITW
1963 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1966 // was editing cancelled?
1967 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1968 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1970 // don't keep a stale wxTextCtrl around
1973 // EDIT control will be deleted by the list control itself so
1974 // prevent us from deleting it as well
1975 m_textCtrl
->UnsubclassWin();
1976 m_textCtrl
->SetHWND(0);
1981 event
.SetEditCanceled(true);
1984 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1985 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1986 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1990 case LVN_COLUMNCLICK
:
1991 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1992 event
.m_itemIndex
= -1;
1993 event
.m_col
= nmLV
->iSubItem
;
1996 case LVN_DELETEALLITEMS
:
1998 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1999 event
.m_itemIndex
= -1;
2002 case LVN_DELETEITEM
:
2004 // this should be prevented by the post-processing code below,
2005 // but "just in case"
2008 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2009 event
.m_itemIndex
= iItem
;
2010 // delete the assoicated internal data
2011 wxDeleteInternalData(this, iItem
);
2014 case LVN_SETDISPINFO
:
2016 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
2017 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2018 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2022 case LVN_INSERTITEM
:
2023 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2024 event
.m_itemIndex
= iItem
;
2027 case LVN_ITEMCHANGED
:
2028 // we translate this catch all message into more interesting
2029 // (and more easy to process) wxWindows events
2031 // first of all, we deal with the state change events only and
2032 // only for valid items (item == -1 for the virtual list
2034 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2036 // temp vars for readability
2037 const UINT stOld
= nmLV
->uOldState
;
2038 const UINT stNew
= nmLV
->uNewState
;
2040 event
.m_item
.SetId(iItem
);
2041 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2044 GetItem(event
.m_item
);
2046 // has the focus changed?
2047 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2049 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2050 event
.m_itemIndex
= iItem
;
2053 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2055 if ( eventType
!= wxEVT_NULL
)
2057 // focus and selection have both changed: send the
2058 // focus event from here and the selection one
2060 event
.SetEventType(eventType
);
2061 (void)GetEventHandler()->ProcessEvent(event
);
2063 else // no focus event to send
2065 // then need to set m_itemIndex as it wasn't done
2067 event
.m_itemIndex
= iItem
;
2070 eventType
= stNew
& LVIS_SELECTED
2071 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2072 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2076 if ( eventType
== wxEVT_NULL
)
2078 // not an interesting event for us
2086 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2087 WORD wVKey
= info
->wVKey
;
2089 // get the current selection
2090 long lItem
= GetNextItem(-1,
2092 wxLIST_STATE_SELECTED
);
2094 // <Enter> or <Space> activate the selected item if any (but
2095 // not with Shift and/or Ctrl as then they have a predefined
2096 // meaning for the list view)
2098 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2099 !(wxIsShiftDown() || wxIsCtrlDown()) )
2101 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2105 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2107 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2108 // value which should be used as is
2109 int code
= wxCharCodeMSWToWX(wVKey
);
2110 event
.m_code
= code
? code
: wVKey
;
2114 event
.m_item
.m_itemId
= lItem
;
2118 // fill the other fields too
2119 event
.m_item
.m_text
= GetItemText(lItem
);
2120 event
.m_item
.m_data
= GetItemData(lItem
);
2126 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2128 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2133 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2134 // if it happened on an item (and not on empty place)
2141 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2142 event
.m_itemIndex
= iItem
;
2143 event
.m_item
.m_text
= GetItemText(iItem
);
2144 event
.m_item
.m_data
= GetItemData(iItem
);
2147 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2148 case GN_CONTEXTMENU
:
2149 #endif //__WXWINCE__
2151 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2152 // don't do anything else
2153 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2158 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2159 LV_HITTESTINFO lvhti
;
2160 wxZeroMemory(lvhti
);
2162 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2163 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2164 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2166 #endif //__WXWINCE__
2167 ::GetCursorPos(&(lvhti
.pt
));
2168 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2169 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2171 if ( lvhti
.flags
& LVHT_ONITEM
)
2173 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2174 event
.m_itemIndex
= lvhti
.iItem
;
2175 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2176 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2181 #ifdef NM_CUSTOMDRAW
2183 *result
= OnCustomDraw(lParam
);
2186 #endif // _WIN32_IE >= 0x300
2188 case LVN_ODCACHEHINT
:
2190 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2192 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2194 // we get some really stupid cache hints like ones for
2195 // items in range 0..0 for an empty control or, after
2196 // deleting an item, for items in invalid range -- filter
2198 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2201 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2203 const long iMax
= GetItemCount();
2204 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2209 case LVN_GETDISPINFO
:
2212 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2214 LV_ITEM
& lvi
= info
->item
;
2215 long item
= lvi
.iItem
;
2217 if ( lvi
.mask
& LVIF_TEXT
)
2219 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2220 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2223 // see comment at the end of wxListCtrl::GetColumn()
2224 #ifdef NM_CUSTOMDRAW
2225 if ( lvi
.mask
& LVIF_IMAGE
)
2227 lvi
.iImage
= OnGetItemImage(item
);
2229 #endif // NM_CUSTOMDRAW
2231 // a little dose of healthy paranoia: as we never use
2232 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2233 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2234 _T("we don't support state callbacks yet!") );
2241 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2246 // where did this one come from?
2250 // process the event
2251 // -----------------
2253 event
.SetEventType(eventType
);
2255 bool processed
= GetEventHandler()->ProcessEvent(event
);
2259 switch ( nmhdr
->code
)
2261 case LVN_DELETEALLITEMS
:
2262 // always return TRUE to suppress all additional LVN_DELETEITEM
2263 // notifications - this makes deleting all items from a list ctrl
2268 case LVN_ENDLABELEDITA
:
2269 case LVN_ENDLABELEDITW
:
2270 // logic here is inversed compared to all the other messages
2271 *result
= event
.IsAllowed();
2273 // don't keep a stale wxTextCtrl around
2276 // EDIT control will be deleted by the list control itself so
2277 // prevent us from deleting it as well
2278 m_textCtrl
->UnsubclassWin();
2279 m_textCtrl
->SetHWND(0);
2288 *result
= !event
.IsAllowed();
2293 // see comment at the end of wxListCtrl::GetColumn()
2294 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2296 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2298 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2299 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2300 switch ( nmcd
.dwDrawStage
)
2303 // if we've got any items with non standard attributes,
2304 // notify us before painting each item
2306 // for virtual controls, always suppose that we have attributes as
2307 // there is no way to check for this
2308 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2311 case CDDS_ITEMPREPAINT
:
2313 size_t item
= (size_t)nmcd
.dwItemSpec
;
2314 if ( item
>= (size_t)GetItemCount() )
2316 // we get this message with item == 0 for an empty control,
2317 // we must ignore it as calling OnGetItemAttr() would be
2319 return CDRF_DODEFAULT
;
2322 wxListItemAttr
*attr
=
2323 IsVirtual() ? OnGetItemAttr(item
)
2324 : wxGetInternalDataAttr(this, item
);
2328 // nothing to do for this item
2329 return CDRF_DODEFAULT
;
2333 wxColour colText
, colBack
;
2334 if ( attr
->HasFont() )
2336 wxFont font
= attr
->GetFont();
2337 hFont
= (HFONT
)font
.GetResourceHandle();
2344 if ( attr
->HasTextColour() )
2346 colText
= attr
->GetTextColour();
2350 colText
= GetTextColour();
2353 if ( attr
->HasBackgroundColour() )
2355 colBack
= attr
->GetBackgroundColour();
2359 colBack
= GetBackgroundColour();
2362 lplvcd
->clrText
= wxColourToRGB(colText
);
2363 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2365 // note that if we wanted to set colours for
2366 // individual columns (subitems), we would have
2367 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2370 ::SelectObject(nmcd
.hdc
, hFont
);
2372 return CDRF_NEWFONT
;
2375 // fall through to return CDRF_DODEFAULT
2378 return CDRF_DODEFAULT
;
2382 #endif // NM_CUSTOMDRAW supported
2384 // Necessary for drawing hrules and vrules, if specified
2385 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2389 wxControl::OnPaint(event
);
2391 // Reset the device origin since it may have been set
2392 dc
.SetDeviceOrigin(0, 0);
2394 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2395 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2397 if (!drawHRules
&& !drawVRules
)
2399 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2402 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2404 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2406 wxSize clientSize
= GetClientSize();
2410 int itemCount
= GetItemCount();
2414 long top
= GetTopItem();
2415 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2417 if (GetItemRect(i
, itemRect
))
2419 cy
= itemRect
.GetTop();
2420 if (i
!= 0) // Don't draw the first one
2422 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2425 if (i
== itemCount
- 1)
2427 cy
= itemRect
.GetBottom();
2428 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2434 if (drawVRules
&& (i
> -1))
2436 wxRect firstItemRect
;
2437 GetItemRect(0, firstItemRect
);
2439 if (GetItemRect(i
, itemRect
))
2442 int x
= itemRect
.GetX();
2443 for (col
= 0; col
< GetColumnCount(); col
++)
2445 int colWidth
= GetColumnWidth(col
);
2447 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2453 // ----------------------------------------------------------------------------
2454 // virtual list controls
2455 // ----------------------------------------------------------------------------
2457 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2459 // this is a pure virtual function, in fact - which is not really pure
2460 // because the controls which are not virtual don't need to implement it
2461 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2463 return wxEmptyString
;
2466 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2469 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2474 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2476 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2477 _T("invalid item index in OnGetItemAttr()") );
2479 // no attributes by default
2483 void wxListCtrl::SetItemCount(long count
)
2485 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2487 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2488 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2490 wxLogLastError(_T("ListView_SetItemCount"));
2493 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2494 wxT("m_count should match ListView_GetItemCount"));
2497 void wxListCtrl::RefreshItem(long item
)
2499 // strangely enough, ListView_Update() results in much more flicker here
2500 // than a dumb Refresh() -- why?
2502 if ( !ListView_Update(GetHwnd(), item
) )
2504 wxLogLastError(_T("ListView_Update"));
2508 GetItemRect(item
, rect
);
2513 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2515 wxRect rect1
, rect2
;
2516 GetItemRect(itemFrom
, rect1
);
2517 GetItemRect(itemTo
, rect2
);
2519 wxRect rect
= rect1
;
2520 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2525 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2528 it
.mask
= LVIF_PARAM
;
2531 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2533 return (wxListItemInternalData
*) it
.lParam
;
2538 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2540 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2543 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2545 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2552 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2554 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2558 memset(&item
, 0, sizeof(item
));
2559 item
.iItem
= itemId
;
2560 item
.mask
= LVIF_PARAM
;
2561 item
.lParam
= (LPARAM
) 0;
2562 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2567 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2571 wxListItemInternalData
*internaldata
=
2572 (wxListItemInternalData
*) lvItem
.lParam
;
2575 info
.m_data
= internaldata
->lParam
;
2579 info
.m_stateMask
= 0;
2580 info
.m_itemId
= lvItem
.iItem
;
2582 long oldMask
= lvItem
.mask
;
2584 bool needText
= FALSE
;
2585 if (hwndListCtrl
!= 0)
2587 if ( lvItem
.mask
& LVIF_TEXT
)
2594 lvItem
.pszText
= new wxChar
[513];
2595 lvItem
.cchTextMax
= 512;
2597 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2598 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2601 if ( lvItem
.mask
& LVIF_STATE
)
2603 info
.m_mask
|= wxLIST_MASK_STATE
;
2605 if ( lvItem
.stateMask
& LVIS_CUT
)
2607 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2608 if ( lvItem
.state
& LVIS_CUT
)
2609 info
.m_state
|= wxLIST_STATE_CUT
;
2611 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2613 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2614 if ( lvItem
.state
& LVIS_DROPHILITED
)
2615 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2617 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2619 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2620 if ( lvItem
.state
& LVIS_FOCUSED
)
2621 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2623 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2625 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2626 if ( lvItem
.state
& LVIS_SELECTED
)
2627 info
.m_state
|= wxLIST_STATE_SELECTED
;
2631 if ( lvItem
.mask
& LVIF_TEXT
)
2633 info
.m_mask
|= wxLIST_MASK_TEXT
;
2634 info
.m_text
= lvItem
.pszText
;
2636 if ( lvItem
.mask
& LVIF_IMAGE
)
2638 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2639 info
.m_image
= lvItem
.iImage
;
2641 if ( lvItem
.mask
& LVIF_PARAM
)
2642 info
.m_mask
|= wxLIST_MASK_DATA
;
2643 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2644 info
.m_mask
|= wxLIST_SET_ITEM
;
2645 info
.m_col
= lvItem
.iSubItem
;
2650 delete[] lvItem
.pszText
;
2652 lvItem
.mask
= oldMask
;
2655 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2657 if (stateMask
& wxLIST_STATE_CUT
)
2659 lvItem
.stateMask
|= LVIS_CUT
;
2660 if (state
& wxLIST_STATE_CUT
)
2661 lvItem
.state
|= LVIS_CUT
;
2663 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2665 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2666 if (state
& wxLIST_STATE_DROPHILITED
)
2667 lvItem
.state
|= LVIS_DROPHILITED
;
2669 if (stateMask
& wxLIST_STATE_FOCUSED
)
2671 lvItem
.stateMask
|= LVIS_FOCUSED
;
2672 if (state
& wxLIST_STATE_FOCUSED
)
2673 lvItem
.state
|= LVIS_FOCUSED
;
2675 if (stateMask
& wxLIST_STATE_SELECTED
)
2677 lvItem
.stateMask
|= LVIS_SELECTED
;
2678 if (state
& wxLIST_STATE_SELECTED
)
2679 lvItem
.state
|= LVIS_SELECTED
;
2683 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2684 const wxListItem
& info
,
2687 lvItem
.iItem
= (int) info
.m_itemId
;
2689 lvItem
.iImage
= info
.m_image
;
2690 lvItem
.stateMask
= 0;
2693 lvItem
.iSubItem
= info
.m_col
;
2695 if (info
.m_mask
& wxLIST_MASK_STATE
)
2697 lvItem
.mask
|= LVIF_STATE
;
2699 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2702 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2704 lvItem
.mask
|= LVIF_TEXT
;
2705 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2707 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2711 // pszText is not const, hence the cast
2712 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2713 if ( lvItem
.pszText
)
2714 lvItem
.cchTextMax
= info
.m_text
.Length();
2716 lvItem
.cchTextMax
= 0;
2719 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2720 lvItem
.mask
|= LVIF_IMAGE
;
2723 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2726 wxZeroMemory(lvCol
);
2728 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2730 lvCol
.mask
|= LVCF_TEXT
;
2731 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2734 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2736 lvCol
.mask
|= LVCF_FMT
;
2738 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2739 lvCol
.fmt
= LVCFMT_LEFT
;
2740 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2741 lvCol
.fmt
= LVCFMT_RIGHT
;
2742 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2743 lvCol
.fmt
= LVCFMT_CENTER
;
2746 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2748 lvCol
.mask
|= LVCF_WIDTH
;
2749 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2750 lvCol
.cx
= LVSCW_AUTOSIZE
;
2751 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2752 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2754 lvCol
.cx
= item
.m_width
;
2757 // see comment at the end of wxListCtrl::GetColumn()
2758 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2759 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2761 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2763 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2765 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2766 // seem to be generally nicer than on the left and the generic
2767 // version only draws them on the right (we don't have a flag to
2768 // specify the image location anyhow)
2770 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2771 // make any difference in my tests -- but maybe we should?
2772 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2774 lvCol
.iImage
= item
.m_image
;
2776 //else: it doesn't support item images anyhow
2778 #endif // _WIN32_IE >= 0x0300
2781 #endif // wxUSE_LISTCTRL