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());
1569 // and remember that we have some now...
1570 m_hasAnyAttr
= TRUE
;
1574 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1577 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1578 wxT("m_count should match ListView_GetItemCount"));
1583 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1586 info
.m_text
= label
;
1587 info
.m_mask
= wxLIST_MASK_TEXT
;
1588 info
.m_itemId
= index
;
1589 return InsertItem(info
);
1592 // Inserts an image item
1593 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1596 info
.m_image
= imageIndex
;
1597 info
.m_mask
= wxLIST_MASK_IMAGE
;
1598 info
.m_itemId
= index
;
1599 return InsertItem(info
);
1602 // Inserts an image/string item
1603 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1606 info
.m_image
= imageIndex
;
1607 info
.m_text
= label
;
1608 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1609 info
.m_itemId
= index
;
1610 return InsertItem(info
);
1613 // For list view mode (only), inserts a column.
1614 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1617 wxConvertToMSWListCol(col
, item
, lvCol
);
1619 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1621 // always give some width to the new column: this one is compatible
1622 // with the generic version
1623 lvCol
.mask
|= LVCF_WIDTH
;
1627 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1632 else // failed to insert?
1634 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1641 long wxListCtrl::InsertColumn(long col
,
1642 const wxString
& heading
,
1647 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1648 item
.m_text
= heading
;
1651 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1652 item
.m_width
= width
;
1654 item
.m_format
= format
;
1656 return InsertColumn(col
, item
);
1659 // scroll the control by the given number of pixels (exception: in list view,
1660 // dx is interpreted as number of columns)
1661 bool wxListCtrl::ScrollList(int dx
, int dy
)
1663 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1665 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1675 // fn is a function which takes 3 long arguments: item1, item2, data.
1676 // item1 is the long data associated with a first item (NOT the index).
1677 // item2 is the long data associated with a second item (NOT the index).
1678 // data is the same value as passed to SortItems.
1679 // The return value is a negative number if the first item should precede the second
1680 // item, a positive number of the second item should precede the first,
1681 // or zero if the two items are equivalent.
1683 // data is arbitrary data to be passed to the sort function.
1685 // Internal structures for proxying the user compare function
1686 // so that we can pass it the *real* user data
1688 // translate lParam data and call user func
1689 struct wxInternalDataSort
1691 wxListCtrlCompare user_fn
;
1695 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1697 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1699 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1700 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1702 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1703 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1705 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1709 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1711 struct wxInternalDataSort internalData
;
1712 internalData
.user_fn
= fn
;
1713 internalData
.data
= data
;
1715 // WPARAM cast is needed for mingw/cygwin
1716 if ( !ListView_SortItems(GetHwnd(),
1717 wxInternalDataCompareFunc
,
1718 (WPARAM
) &internalData
) )
1720 wxLogDebug(_T("ListView_SortItems() failed"));
1730 // ----------------------------------------------------------------------------
1731 // message processing
1732 // ----------------------------------------------------------------------------
1734 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1736 if (cmd
== EN_UPDATE
)
1738 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1739 event
.SetEventObject( this );
1740 ProcessCommand(event
);
1743 else if (cmd
== EN_KILLFOCUS
)
1745 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1746 event
.SetEventObject( this );
1747 ProcessCommand(event
);
1754 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1757 // prepare the event
1758 // -----------------
1760 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1761 event
.SetEventObject(this);
1763 wxEventType eventType
= wxEVT_NULL
;
1765 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1767 // if your compiler is as broken as this, you should really change it: this
1768 // code is needed for normal operation! #ifdef below is only useful for
1769 // automatic rebuilds which are done with a very old compiler version
1770 #ifdef HDN_BEGINTRACKA
1772 // check for messages from the header (in report view)
1773 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1775 // is it a message from the header?
1776 if ( nmhdr
->hwndFrom
== hwndHdr
)
1778 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1780 event
.m_itemIndex
= -1;
1782 switch ( nmhdr
->code
)
1784 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1785 // TRACK messages even to ANSI programs: on my system I get
1786 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1788 // work around is to simply catch both versions and hope that it
1789 // works (why should this message exist in ANSI and Unicode is
1790 // beyond me as it doesn't deal with strings at all...)
1792 // note that fr HDN_TRACK another possibility could be to use
1793 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1794 // something other than the item width changes so we'd have to
1795 // filter out the unwanted events then
1796 case HDN_BEGINTRACKA
:
1797 case HDN_BEGINTRACKW
:
1798 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1803 if ( eventType
== wxEVT_NULL
)
1804 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1809 if ( eventType
== wxEVT_NULL
)
1810 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1812 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1813 event
.m_col
= nmHDR
->iItem
;
1816 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1817 case GN_CONTEXTMENU
:
1818 #endif //__WXWINCE__
1821 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1824 // find the column clicked: we have to search for it
1825 // ourselves as the notification message doesn't provide
1828 // where did the click occur?
1830 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1831 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1832 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1834 #endif //__WXWINCE__
1835 if ( !::GetCursorPos(&ptClick
) )
1837 wxLogLastError(_T("GetCursorPos"));
1840 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1842 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1845 event
.m_pointDrag
.x
= ptClick
.x
;
1846 event
.m_pointDrag
.y
= ptClick
.y
;
1848 int colCount
= Header_GetItemCount(hwndHdr
);
1851 for ( int col
= 0; col
< colCount
; col
++ )
1853 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1855 if ( ::PtInRect(&rect
, ptClick
) )
1865 case HDN_GETDISPINFOW
:
1867 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1868 // This is a fix for a strange bug under XP.
1869 // Normally, info->iItem is a valid index, but
1870 // sometimes this is a silly (large) number
1871 // and when we return FALSE via wxControl::MSWOnNotify
1872 // to indicate that it hasn't yet been processed,
1873 // there's a GPF in Windows.
1874 // By returning TRUE here, we avoid further processing
1875 // of this strange message.
1876 if ( info
->iItem
>= GetColumnCount() )
1882 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1886 #endif // defined(HDN_BEGINTRACKA)
1887 if ( nmhdr
->hwndFrom
== GetHwnd() )
1889 // almost all messages use NM_LISTVIEW
1890 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1892 const int iItem
= nmLV
->iItem
;
1895 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1896 // ignored for efficiency. It is done here because the internal data is in the
1897 // process of being deleted so we don't want to try and access it below.
1898 if ( m_ignoreChangeMessages
&&
1899 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1905 // If we have a valid item then check if there is a data value
1906 // associated with it and put it in the event.
1907 if ( iItem
>= 0 && iItem
< GetItemCount() )
1909 wxListItemInternalData
*internaldata
=
1910 wxGetInternalData(GetHwnd(), iItem
);
1913 event
.m_item
.m_data
= internaldata
->lParam
;
1917 switch ( nmhdr
->code
)
1919 case LVN_BEGINRDRAG
:
1920 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1924 if ( eventType
== wxEVT_NULL
)
1926 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1929 event
.m_itemIndex
= iItem
;
1930 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1931 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1934 // NB: we have to handle both *A and *W versions here because some
1935 // versions of comctl32.dll send ANSI messages even to the
1937 case LVN_BEGINLABELEDITA
:
1938 case LVN_BEGINLABELEDITW
:
1941 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1943 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1945 else // LVN_BEGINLABELEDITW
1947 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1950 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1951 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1952 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1956 case LVN_ENDLABELEDITA
:
1957 case LVN_ENDLABELEDITW
:
1960 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1962 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1964 else // LVN_ENDLABELEDITW
1966 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1969 // was editing cancelled?
1970 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1971 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1973 // don't keep a stale wxTextCtrl around
1976 // EDIT control will be deleted by the list control itself so
1977 // prevent us from deleting it as well
1978 m_textCtrl
->UnsubclassWin();
1979 m_textCtrl
->SetHWND(0);
1984 event
.SetEditCanceled(true);
1987 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1988 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1989 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1993 case LVN_COLUMNCLICK
:
1994 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1995 event
.m_itemIndex
= -1;
1996 event
.m_col
= nmLV
->iSubItem
;
1999 case LVN_DELETEALLITEMS
:
2001 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
2002 event
.m_itemIndex
= -1;
2005 case LVN_DELETEITEM
:
2007 // this should be prevented by the post-processing code below,
2008 // but "just in case"
2011 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2012 event
.m_itemIndex
= iItem
;
2013 // delete the assoicated internal data
2014 wxDeleteInternalData(this, iItem
);
2017 case LVN_SETDISPINFO
:
2019 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
2020 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2021 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2025 case LVN_INSERTITEM
:
2026 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2027 event
.m_itemIndex
= iItem
;
2030 case LVN_ITEMCHANGED
:
2031 // we translate this catch all message into more interesting
2032 // (and more easy to process) wxWindows events
2034 // first of all, we deal with the state change events only and
2035 // only for valid items (item == -1 for the virtual list
2037 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2039 // temp vars for readability
2040 const UINT stOld
= nmLV
->uOldState
;
2041 const UINT stNew
= nmLV
->uNewState
;
2043 event
.m_item
.SetId(iItem
);
2044 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2047 GetItem(event
.m_item
);
2049 // has the focus changed?
2050 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2052 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2053 event
.m_itemIndex
= iItem
;
2056 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2058 if ( eventType
!= wxEVT_NULL
)
2060 // focus and selection have both changed: send the
2061 // focus event from here and the selection one
2063 event
.SetEventType(eventType
);
2064 (void)GetEventHandler()->ProcessEvent(event
);
2066 else // no focus event to send
2068 // then need to set m_itemIndex as it wasn't done
2070 event
.m_itemIndex
= iItem
;
2073 eventType
= stNew
& LVIS_SELECTED
2074 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2075 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2079 if ( eventType
== wxEVT_NULL
)
2081 // not an interesting event for us
2089 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2090 WORD wVKey
= info
->wVKey
;
2092 // get the current selection
2093 long lItem
= GetNextItem(-1,
2095 wxLIST_STATE_SELECTED
);
2097 // <Enter> or <Space> activate the selected item if any (but
2098 // not with Shift and/or Ctrl as then they have a predefined
2099 // meaning for the list view)
2101 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2102 !(wxIsShiftDown() || wxIsCtrlDown()) )
2104 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2108 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2110 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2111 // value which should be used as is
2112 int code
= wxCharCodeMSWToWX(wVKey
);
2113 event
.m_code
= code
? code
: wVKey
;
2117 event
.m_item
.m_itemId
= lItem
;
2121 // fill the other fields too
2122 event
.m_item
.m_text
= GetItemText(lItem
);
2123 event
.m_item
.m_data
= GetItemData(lItem
);
2129 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2131 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2136 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2137 // if it happened on an item (and not on empty place)
2144 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2145 event
.m_itemIndex
= iItem
;
2146 event
.m_item
.m_text
= GetItemText(iItem
);
2147 event
.m_item
.m_data
= GetItemData(iItem
);
2150 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2151 case GN_CONTEXTMENU
:
2152 #endif //__WXWINCE__
2154 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2155 // don't do anything else
2156 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2161 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2162 LV_HITTESTINFO lvhti
;
2163 wxZeroMemory(lvhti
);
2165 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2166 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2167 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2169 #endif //__WXWINCE__
2170 ::GetCursorPos(&(lvhti
.pt
));
2171 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2172 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2174 if ( lvhti
.flags
& LVHT_ONITEM
)
2176 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2177 event
.m_itemIndex
= lvhti
.iItem
;
2178 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2179 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2184 #ifdef NM_CUSTOMDRAW
2186 *result
= OnCustomDraw(lParam
);
2189 #endif // _WIN32_IE >= 0x300
2191 case LVN_ODCACHEHINT
:
2193 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2195 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2197 // we get some really stupid cache hints like ones for
2198 // items in range 0..0 for an empty control or, after
2199 // deleting an item, for items in invalid range -- filter
2201 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2204 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2206 const long iMax
= GetItemCount();
2207 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2212 case LVN_GETDISPINFO
:
2215 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2217 LV_ITEM
& lvi
= info
->item
;
2218 long item
= lvi
.iItem
;
2220 if ( lvi
.mask
& LVIF_TEXT
)
2222 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2223 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2226 // see comment at the end of wxListCtrl::GetColumn()
2227 #ifdef NM_CUSTOMDRAW
2228 if ( lvi
.mask
& LVIF_IMAGE
)
2230 lvi
.iImage
= OnGetItemImage(item
);
2232 #endif // NM_CUSTOMDRAW
2234 // a little dose of healthy paranoia: as we never use
2235 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2236 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2237 _T("we don't support state callbacks yet!") );
2244 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2249 // where did this one come from?
2253 // process the event
2254 // -----------------
2256 event
.SetEventType(eventType
);
2258 bool processed
= GetEventHandler()->ProcessEvent(event
);
2262 switch ( nmhdr
->code
)
2264 case LVN_DELETEALLITEMS
:
2265 // always return TRUE to suppress all additional LVN_DELETEITEM
2266 // notifications - this makes deleting all items from a list ctrl
2271 case LVN_ENDLABELEDITA
:
2272 case LVN_ENDLABELEDITW
:
2273 // logic here is inversed compared to all the other messages
2274 *result
= event
.IsAllowed();
2276 // don't keep a stale wxTextCtrl around
2279 // EDIT control will be deleted by the list control itself so
2280 // prevent us from deleting it as well
2281 m_textCtrl
->UnsubclassWin();
2282 m_textCtrl
->SetHWND(0);
2291 *result
= !event
.IsAllowed();
2296 // see comment at the end of wxListCtrl::GetColumn()
2297 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2299 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2301 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2302 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2303 switch ( nmcd
.dwDrawStage
)
2306 // if we've got any items with non standard attributes,
2307 // notify us before painting each item
2309 // for virtual controls, always suppose that we have attributes as
2310 // there is no way to check for this
2311 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2314 case CDDS_ITEMPREPAINT
:
2316 size_t item
= (size_t)nmcd
.dwItemSpec
;
2317 if ( item
>= (size_t)GetItemCount() )
2319 // we get this message with item == 0 for an empty control,
2320 // we must ignore it as calling OnGetItemAttr() would be
2322 return CDRF_DODEFAULT
;
2325 wxListItemAttr
*attr
=
2326 IsVirtual() ? OnGetItemAttr(item
)
2327 : wxGetInternalDataAttr(this, item
);
2331 // nothing to do for this item
2332 return CDRF_DODEFAULT
;
2336 wxColour colText
, colBack
;
2337 if ( attr
->HasFont() )
2339 wxFont font
= attr
->GetFont();
2340 hFont
= (HFONT
)font
.GetResourceHandle();
2347 if ( attr
->HasTextColour() )
2349 colText
= attr
->GetTextColour();
2353 colText
= GetTextColour();
2356 if ( attr
->HasBackgroundColour() )
2358 colBack
= attr
->GetBackgroundColour();
2362 colBack
= GetBackgroundColour();
2365 lplvcd
->clrText
= wxColourToRGB(colText
);
2366 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2368 // note that if we wanted to set colours for
2369 // individual columns (subitems), we would have
2370 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2373 ::SelectObject(nmcd
.hdc
, hFont
);
2375 return CDRF_NEWFONT
;
2378 // fall through to return CDRF_DODEFAULT
2381 return CDRF_DODEFAULT
;
2385 #endif // NM_CUSTOMDRAW supported
2387 // Necessary for drawing hrules and vrules, if specified
2388 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2392 wxControl::OnPaint(event
);
2394 // Reset the device origin since it may have been set
2395 dc
.SetDeviceOrigin(0, 0);
2397 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2398 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2400 if (!drawHRules
&& !drawVRules
)
2402 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2405 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2407 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2409 wxSize clientSize
= GetClientSize();
2413 int itemCount
= GetItemCount();
2417 long top
= GetTopItem();
2418 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2420 if (GetItemRect(i
, itemRect
))
2422 cy
= itemRect
.GetTop();
2423 if (i
!= 0) // Don't draw the first one
2425 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2428 if (i
== itemCount
- 1)
2430 cy
= itemRect
.GetBottom();
2431 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2437 if (drawVRules
&& (i
> -1))
2439 wxRect firstItemRect
;
2440 GetItemRect(0, firstItemRect
);
2442 if (GetItemRect(i
, itemRect
))
2445 int x
= itemRect
.GetX();
2446 for (col
= 0; col
< GetColumnCount(); col
++)
2448 int colWidth
= GetColumnWidth(col
);
2450 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2456 // ----------------------------------------------------------------------------
2457 // virtual list controls
2458 // ----------------------------------------------------------------------------
2460 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2462 // this is a pure virtual function, in fact - which is not really pure
2463 // because the controls which are not virtual don't need to implement it
2464 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2466 return wxEmptyString
;
2469 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2472 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2477 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2479 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2480 _T("invalid item index in OnGetItemAttr()") );
2482 // no attributes by default
2486 void wxListCtrl::SetItemCount(long count
)
2488 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2490 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2491 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2493 wxLogLastError(_T("ListView_SetItemCount"));
2496 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2497 wxT("m_count should match ListView_GetItemCount"));
2500 void wxListCtrl::RefreshItem(long item
)
2502 // strangely enough, ListView_Update() results in much more flicker here
2503 // than a dumb Refresh() -- why?
2505 if ( !ListView_Update(GetHwnd(), item
) )
2507 wxLogLastError(_T("ListView_Update"));
2511 GetItemRect(item
, rect
);
2516 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2518 wxRect rect1
, rect2
;
2519 GetItemRect(itemFrom
, rect1
);
2520 GetItemRect(itemTo
, rect2
);
2522 wxRect rect
= rect1
;
2523 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2528 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2531 it
.mask
= LVIF_PARAM
;
2534 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2536 return (wxListItemInternalData
*) it
.lParam
;
2541 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2543 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2546 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2548 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2555 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2557 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2561 memset(&item
, 0, sizeof(item
));
2562 item
.iItem
= itemId
;
2563 item
.mask
= LVIF_PARAM
;
2564 item
.lParam
= (LPARAM
) 0;
2565 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2570 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2574 wxListItemInternalData
*internaldata
=
2575 (wxListItemInternalData
*) lvItem
.lParam
;
2578 info
.m_data
= internaldata
->lParam
;
2582 info
.m_stateMask
= 0;
2583 info
.m_itemId
= lvItem
.iItem
;
2585 long oldMask
= lvItem
.mask
;
2587 bool needText
= FALSE
;
2588 if (hwndListCtrl
!= 0)
2590 if ( lvItem
.mask
& LVIF_TEXT
)
2597 lvItem
.pszText
= new wxChar
[513];
2598 lvItem
.cchTextMax
= 512;
2600 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2601 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2604 if ( lvItem
.mask
& LVIF_STATE
)
2606 info
.m_mask
|= wxLIST_MASK_STATE
;
2608 if ( lvItem
.stateMask
& LVIS_CUT
)
2610 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2611 if ( lvItem
.state
& LVIS_CUT
)
2612 info
.m_state
|= wxLIST_STATE_CUT
;
2614 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2616 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2617 if ( lvItem
.state
& LVIS_DROPHILITED
)
2618 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2620 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2622 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2623 if ( lvItem
.state
& LVIS_FOCUSED
)
2624 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2626 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2628 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2629 if ( lvItem
.state
& LVIS_SELECTED
)
2630 info
.m_state
|= wxLIST_STATE_SELECTED
;
2634 if ( lvItem
.mask
& LVIF_TEXT
)
2636 info
.m_mask
|= wxLIST_MASK_TEXT
;
2637 info
.m_text
= lvItem
.pszText
;
2639 if ( lvItem
.mask
& LVIF_IMAGE
)
2641 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2642 info
.m_image
= lvItem
.iImage
;
2644 if ( lvItem
.mask
& LVIF_PARAM
)
2645 info
.m_mask
|= wxLIST_MASK_DATA
;
2646 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2647 info
.m_mask
|= wxLIST_SET_ITEM
;
2648 info
.m_col
= lvItem
.iSubItem
;
2653 delete[] lvItem
.pszText
;
2655 lvItem
.mask
= oldMask
;
2658 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2660 if (stateMask
& wxLIST_STATE_CUT
)
2662 lvItem
.stateMask
|= LVIS_CUT
;
2663 if (state
& wxLIST_STATE_CUT
)
2664 lvItem
.state
|= LVIS_CUT
;
2666 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2668 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2669 if (state
& wxLIST_STATE_DROPHILITED
)
2670 lvItem
.state
|= LVIS_DROPHILITED
;
2672 if (stateMask
& wxLIST_STATE_FOCUSED
)
2674 lvItem
.stateMask
|= LVIS_FOCUSED
;
2675 if (state
& wxLIST_STATE_FOCUSED
)
2676 lvItem
.state
|= LVIS_FOCUSED
;
2678 if (stateMask
& wxLIST_STATE_SELECTED
)
2680 lvItem
.stateMask
|= LVIS_SELECTED
;
2681 if (state
& wxLIST_STATE_SELECTED
)
2682 lvItem
.state
|= LVIS_SELECTED
;
2686 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2687 const wxListItem
& info
,
2690 lvItem
.iItem
= (int) info
.m_itemId
;
2692 lvItem
.iImage
= info
.m_image
;
2693 lvItem
.stateMask
= 0;
2696 lvItem
.iSubItem
= info
.m_col
;
2698 if (info
.m_mask
& wxLIST_MASK_STATE
)
2700 lvItem
.mask
|= LVIF_STATE
;
2702 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2705 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2707 lvItem
.mask
|= LVIF_TEXT
;
2708 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2710 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2714 // pszText is not const, hence the cast
2715 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2716 if ( lvItem
.pszText
)
2717 lvItem
.cchTextMax
= info
.m_text
.Length();
2719 lvItem
.cchTextMax
= 0;
2722 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2723 lvItem
.mask
|= LVIF_IMAGE
;
2726 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2729 wxZeroMemory(lvCol
);
2731 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2733 lvCol
.mask
|= LVCF_TEXT
;
2734 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2737 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2739 lvCol
.mask
|= LVCF_FMT
;
2741 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2742 lvCol
.fmt
= LVCFMT_LEFT
;
2743 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2744 lvCol
.fmt
= LVCFMT_RIGHT
;
2745 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2746 lvCol
.fmt
= LVCFMT_CENTER
;
2749 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2751 lvCol
.mask
|= LVCF_WIDTH
;
2752 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2753 lvCol
.cx
= LVSCW_AUTOSIZE
;
2754 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2755 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2757 lvCol
.cx
= item
.m_width
;
2760 // see comment at the end of wxListCtrl::GetColumn()
2761 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2762 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2764 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2766 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2768 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2769 // seem to be generally nicer than on the left and the generic
2770 // version only draws them on the right (we don't have a flag to
2771 // specify the image location anyhow)
2773 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2774 // make any difference in my tests -- but maybe we should?
2775 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2777 lvCol
.iImage
= item
.m_image
;
2779 //else: it doesn't support item images anyhow
2781 #endif // _WIN32_IE >= 0x0300
2784 #endif // wxUSE_LISTCTRL