1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
49 #include "wx/msw/gnuwin32/extra.h"
56 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
60 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
63 #ifndef LVS_EX_FULLROWSELECT
64 #define LVS_EX_FULLROWSELECT 0x00000020
68 #define LVS_OWNERDATA 0x1000
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
76 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
99 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
100 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
102 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
103 EVT_PAINT(wxListCtrl::OnPaint
)
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 void wxListEvent::CopyObject(wxObject
& object_dest
) const
116 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
118 wxNotifyEvent::CopyObject(object_dest
);
120 obj
->m_code
= m_code
;
121 obj
->m_itemIndex
= m_itemIndex
;
122 obj
->m_oldItemIndex
= m_oldItemIndex
;
124 obj
->m_cancelled
= m_cancelled
;
125 obj
->m_pointDrag
= m_pointDrag
;
126 obj
->m_item
.m_mask
= m_item
.m_mask
;
127 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
128 obj
->m_item
.m_col
= m_item
.m_col
;
129 obj
->m_item
.m_state
= m_item
.m_state
;
130 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
131 obj
->m_item
.m_text
= m_item
.m_text
;
132 obj
->m_item
.m_image
= m_item
.m_image
;
133 obj
->m_item
.m_data
= m_item
.m_data
;
134 obj
->m_item
.m_format
= m_item
.m_format
;
135 obj
->m_item
.m_width
= m_item
.m_width
;
137 if ( m_item
.HasAttributes() )
139 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
140 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
141 obj
->m_item
.SetFont(m_item
.GetFont());
145 // ----------------------------------------------------------------------------
146 // wxListCtrl construction
147 // ----------------------------------------------------------------------------
149 void wxListCtrl::Init()
151 m_imageListNormal
= NULL
;
152 m_imageListSmall
= NULL
;
153 m_imageListState
= NULL
;
154 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
158 m_hasAnyAttr
= FALSE
;
161 bool wxListCtrl::Create(wxWindow
*parent
,
166 const wxValidator
& validator
,
167 const wxString
& name
)
170 SetValidator(validator
);
171 #endif // wxUSE_VALIDATORS
180 m_windowStyle
= style
;
193 m_windowId
= (id
== -1) ? NewControlId() : id
;
195 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
196 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
198 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
199 wstyle
|= WS_CLIPSIBLINGS
;
201 if ( wxStyleHasBorder(m_windowStyle
) )
203 m_baseStyle
= wstyle
;
205 if ( !DoCreateControl(x
, y
, width
, height
) )
209 parent
->AddChild(this);
214 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
216 DWORD wstyle
= m_baseStyle
;
219 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
221 // Even with extended styles, need to combine with WS_BORDER
222 // for them to look right.
226 long oldStyle
= 0; // Dummy
227 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
229 // Create the ListView control.
230 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
235 GetWinHwnd(GetParent()),
242 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
247 // for comctl32.dll v 4.70+ we want to have this attribute because it's
248 // prettier (and also because wxGTK does it like this)
249 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
251 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
252 0, LVS_EX_FULLROWSELECT
);
255 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
256 SetForegroundColour(GetParent()->GetForegroundColour());
263 void wxListCtrl::UpdateStyle()
267 // The new window view style
269 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
270 dwStyleNew
|= m_baseStyle
;
272 // Get the current window style.
273 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
275 // Only set the window style if the view bits have changed.
276 if ( dwStyleOld
!= dwStyleNew
)
278 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
283 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
287 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
289 delete (wxListItemAttr
*)node
->Data();
295 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
298 m_hasAnyAttr
= FALSE
;
302 wxListCtrl::~wxListCtrl()
304 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
308 m_textCtrl
->SetHWND(0);
309 m_textCtrl
->UnsubclassWin();
314 if (m_ownsImageListNormal
) delete m_imageListNormal
;
315 if (m_ownsImageListSmall
) delete m_imageListSmall
;
316 if (m_ownsImageListState
) delete m_imageListState
;
319 // ----------------------------------------------------------------------------
320 // set/get/change style
321 // ----------------------------------------------------------------------------
323 // Add or remove a single window style
324 void wxListCtrl::SetSingleStyle(long style
, bool add
)
326 long flag
= GetWindowStyleFlag();
328 // Get rid of conflicting styles
331 if ( style
& wxLC_MASK_TYPE
)
332 flag
= flag
& ~wxLC_MASK_TYPE
;
333 if ( style
& wxLC_MASK_ALIGN
)
334 flag
= flag
& ~wxLC_MASK_ALIGN
;
335 if ( style
& wxLC_MASK_SORT
)
336 flag
= flag
& ~wxLC_MASK_SORT
;
352 m_windowStyle
= flag
;
357 // Set the whole window style
358 void wxListCtrl::SetWindowStyleFlag(long flag
)
360 m_windowStyle
= flag
;
365 // Can be just a single style, or a bitlist
366 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
369 if ( style
& wxLC_ICON
)
371 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
372 oldStyle
-= LVS_SMALLICON
;
373 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
374 oldStyle
-= LVS_REPORT
;
375 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
376 oldStyle
-= LVS_LIST
;
380 if ( style
& wxLC_SMALL_ICON
)
382 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
383 oldStyle
-= LVS_ICON
;
384 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
385 oldStyle
-= LVS_REPORT
;
386 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
387 oldStyle
-= LVS_LIST
;
388 wstyle
|= LVS_SMALLICON
;
391 if ( style
& wxLC_LIST
)
393 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
394 oldStyle
-= LVS_ICON
;
395 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
396 oldStyle
-= LVS_REPORT
;
397 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
398 oldStyle
-= LVS_SMALLICON
;
402 if ( style
& wxLC_REPORT
)
404 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
405 oldStyle
-= LVS_ICON
;
406 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
407 oldStyle
-= LVS_LIST
;
408 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
409 oldStyle
-= LVS_SMALLICON
;
411 wstyle
|= LVS_REPORT
;
414 if ( style
& wxLC_ALIGN_LEFT
)
416 if ( oldStyle
& LVS_ALIGNTOP
)
417 oldStyle
-= LVS_ALIGNTOP
;
418 wstyle
|= LVS_ALIGNLEFT
;
421 if ( style
& wxLC_ALIGN_TOP
)
423 if ( oldStyle
& LVS_ALIGNLEFT
)
424 oldStyle
-= LVS_ALIGNLEFT
;
425 wstyle
|= LVS_ALIGNTOP
;
428 if ( style
& wxLC_AUTOARRANGE
)
429 wstyle
|= LVS_AUTOARRANGE
;
431 if ( style
& wxLC_NO_SORT_HEADER
)
432 wstyle
|= LVS_NOSORTHEADER
;
434 if ( style
& wxLC_NO_HEADER
)
435 wstyle
|= LVS_NOCOLUMNHEADER
;
437 if ( style
& wxLC_EDIT_LABELS
)
438 wstyle
|= LVS_EDITLABELS
;
440 if ( style
& wxLC_SINGLE_SEL
)
441 wstyle
|= LVS_SINGLESEL
;
443 if ( style
& wxLC_SORT_ASCENDING
)
445 if ( oldStyle
& LVS_SORTDESCENDING
)
446 oldStyle
-= LVS_SORTDESCENDING
;
447 wstyle
|= LVS_SORTASCENDING
;
450 if ( style
& wxLC_SORT_DESCENDING
)
452 if ( oldStyle
& LVS_SORTASCENDING
)
453 oldStyle
-= LVS_SORTASCENDING
;
454 wstyle
|= LVS_SORTDESCENDING
;
457 if ( style
& wxLC_VIRTUAL
)
459 int ver
= wxTheApp
->GetComCtl32Version();
462 wxLogWarning(_("Please install a newer version of comctl32.dll\n"
463 "(at least version 4.70 is required but you have "
465 "or this program won't operate correctly."),
466 ver
/ 100, ver
% 100);
469 wstyle
|= LVS_OWNERDATA
;
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 // Sets the foreground, i.e. text, colour
480 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
482 if ( !wxWindow::SetForegroundColour(col
) )
485 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
490 // Sets the background colour
491 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
493 if ( !wxWindow::SetBackgroundColour(col
) )
496 // we set the same colour for both the "empty" background and the items
498 COLORREF color
= wxColourToRGB(col
);
499 ListView_SetBkColor(GetHwnd(), color
);
500 ListView_SetTextBkColor(GetHwnd(), color
);
505 // Gets information about this column
506 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
511 lvCol
.pszText
= NULL
;
513 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
515 lvCol
.mask
|= LVCF_TEXT
;
516 lvCol
.pszText
= new wxChar
[513];
517 lvCol
.cchTextMax
= 512;
520 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
522 // item.m_subItem = lvCol.iSubItem;
523 item
.m_width
= lvCol
.cx
;
525 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
527 item
.m_text
= lvCol
.pszText
;
528 delete[] lvCol
.pszText
;
531 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
533 if (lvCol
.fmt
== LVCFMT_LEFT
)
534 item
.m_format
= wxLIST_FORMAT_LEFT
;
535 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
536 item
.m_format
= wxLIST_FORMAT_RIGHT
;
537 else if (lvCol
.fmt
== LVCFMT_CENTER
)
538 item
.m_format
= wxLIST_FORMAT_CENTRE
;
544 // Sets information about this column
545 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
550 lvCol
.pszText
= NULL
;
552 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
554 lvCol
.mask
|= LVCF_TEXT
;
555 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
556 lvCol
.cchTextMax
= 0; // Ignored
558 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
560 lvCol
.mask
|= LVCF_FMT
;
562 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
563 lvCol
.fmt
= LVCFMT_LEFT
;
564 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
565 lvCol
.fmt
= LVCFMT_RIGHT
;
566 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
567 lvCol
.fmt
= LVCFMT_CENTER
;
570 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
572 lvCol
.mask
|= LVCF_WIDTH
;
573 lvCol
.cx
= item
.m_width
;
575 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
576 lvCol
.cx
= LVSCW_AUTOSIZE
;
577 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
578 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
580 lvCol
.mask
|= LVCF_SUBITEM
;
581 lvCol
.iSubItem
= col
;
582 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
585 // Gets the column width
586 int wxListCtrl::GetColumnWidth(int col
) const
588 return ListView_GetColumnWidth(GetHwnd(), col
);
591 // Sets the column width
592 bool wxListCtrl::SetColumnWidth(int col
, int width
)
595 if ( m_windowStyle
& wxLC_LIST
)
599 if ( width2
== wxLIST_AUTOSIZE
)
600 width2
= LVSCW_AUTOSIZE
;
601 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
602 width2
= LVSCW_AUTOSIZE_USEHEADER
;
604 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
607 // Gets the number of items that can fit vertically in the
608 // visible area of the list control (list or report view)
609 // or the total number of items in the list control (icon
610 // or small icon view)
611 int wxListCtrl::GetCountPerPage() const
613 return ListView_GetCountPerPage(GetHwnd());
616 // Gets the edit control for editing labels.
617 wxTextCtrl
* wxListCtrl::GetEditControl() const
622 // Gets information about the item
623 bool wxListCtrl::GetItem(wxListItem
& info
) const
626 wxZeroMemory(lvItem
);
628 lvItem
.iItem
= info
.m_itemId
;
629 lvItem
.iSubItem
= info
.m_col
;
631 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
633 lvItem
.mask
|= LVIF_TEXT
;
634 lvItem
.pszText
= new wxChar
[513];
635 lvItem
.cchTextMax
= 512;
639 lvItem
.pszText
= NULL
;
642 if (info
.m_mask
& wxLIST_MASK_DATA
)
643 lvItem
.mask
|= LVIF_PARAM
;
645 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
646 lvItem
.mask
|= LVIF_IMAGE
;
648 if ( info
.m_mask
& wxLIST_MASK_STATE
)
650 lvItem
.mask
|= LVIF_STATE
;
651 // the other bits are hardly interesting anyhow
652 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
655 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
658 wxLogError(_("Couldn't retrieve information about list control item %d."),
663 wxConvertFromMSWListItem(this, info
, lvItem
);
667 delete[] lvItem
.pszText
;
672 // Sets information about the item
673 bool wxListCtrl::SetItem(wxListItem
& info
)
676 wxConvertToMSWListItem(this, info
, item
);
679 if ( !ListView_SetItem(GetHwnd(), &item
) )
681 wxLogDebug(_T("ListView_SetItem() failed"));
686 // we need to update the item immediately to show the new image
687 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
689 // check whether it has any custom attributes
690 if ( info
.HasAttributes() )
692 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
695 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
697 *attr
= *info
.GetAttributes();
701 // if the colour has changed, we must redraw the item
707 // we need this to make the change visible right now
708 ListView_Update(GetHwnd(), item
.iItem
);
714 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
718 info
.m_mask
= wxLIST_MASK_TEXT
;
719 info
.m_itemId
= index
;
723 info
.m_image
= imageId
;
724 info
.m_mask
|= wxLIST_MASK_IMAGE
;
726 return SetItem(info
);
730 // Gets the item state
731 int wxListCtrl::GetItemState(long item
, long stateMask
) const
735 info
.m_mask
= wxLIST_MASK_STATE
;
736 info
.m_stateMask
= stateMask
;
737 info
.m_itemId
= item
;
745 // Sets the item state
746 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
750 info
.m_mask
= wxLIST_MASK_STATE
;
751 info
.m_state
= state
;
752 info
.m_stateMask
= stateMask
;
753 info
.m_itemId
= item
;
755 return SetItem(info
);
758 // Sets the item image
759 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
763 info
.m_mask
= wxLIST_MASK_IMAGE
;
764 info
.m_image
= image
;
765 info
.m_itemId
= item
;
767 return SetItem(info
);
770 // Gets the item text
771 wxString
wxListCtrl::GetItemText(long item
) const
775 info
.m_mask
= wxLIST_MASK_TEXT
;
776 info
.m_itemId
= item
;
783 // Sets the item text
784 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
788 info
.m_mask
= wxLIST_MASK_TEXT
;
789 info
.m_itemId
= item
;
795 // Gets the item data
796 long wxListCtrl::GetItemData(long item
) const
800 info
.m_mask
= wxLIST_MASK_DATA
;
801 info
.m_itemId
= item
;
808 // Sets the item data
809 bool wxListCtrl::SetItemData(long item
, long data
)
813 info
.m_mask
= wxLIST_MASK_DATA
;
814 info
.m_itemId
= item
;
817 return SetItem(info
);
820 // Gets the item rectangle
821 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
825 int code2
= LVIR_BOUNDS
;
826 if ( code
== wxLIST_RECT_BOUNDS
)
828 else if ( code
== wxLIST_RECT_ICON
)
830 else if ( code
== wxLIST_RECT_LABEL
)
834 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
836 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
841 rect
.width
= rect2
.right
- rect2
.left
;
842 rect
.height
= rect2
.bottom
- rect2
.top
;
846 // Gets the item position
847 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
851 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
853 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
857 // Sets the item position.
858 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
860 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
863 // Gets the number of items in the list control
864 int wxListCtrl::GetItemCount() const
866 return ListView_GetItemCount(GetHwnd());
869 // Retrieves the spacing between icons in pixels.
870 // If small is TRUE, gets the spacing for the small icon
871 // view, otherwise the large icon view.
872 int wxListCtrl::GetItemSpacing(bool isSmall
) const
874 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
877 // Gets the number of selected items in the list control
878 int wxListCtrl::GetSelectedItemCount() const
880 return ListView_GetSelectedCount(GetHwnd());
883 // Gets the text colour of the listview
884 wxColour
wxListCtrl::GetTextColour() const
886 COLORREF ref
= ListView_GetTextColor(GetHwnd());
887 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
891 // Sets the text colour of the listview
892 void wxListCtrl::SetTextColour(const wxColour
& col
)
894 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
897 // Gets the index of the topmost visible item when in
898 // list or report view
899 long wxListCtrl::GetTopItem() const
901 return (long) ListView_GetTopIndex(GetHwnd());
904 // Searches for an item, starting from 'item'.
905 // 'geometry' is one of
906 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
907 // 'state' is a state bit flag, one or more of
908 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
909 // item can be -1 to find the first item that matches the
911 // Returns the item or -1 if unsuccessful.
912 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
916 if ( geom
== wxLIST_NEXT_ABOVE
)
918 if ( geom
== wxLIST_NEXT_ALL
)
920 if ( geom
== wxLIST_NEXT_BELOW
)
922 if ( geom
== wxLIST_NEXT_LEFT
)
923 flags
|= LVNI_TOLEFT
;
924 if ( geom
== wxLIST_NEXT_RIGHT
)
925 flags
|= LVNI_TORIGHT
;
927 if ( state
& wxLIST_STATE_CUT
)
929 if ( state
& wxLIST_STATE_DROPHILITED
)
930 flags
|= LVNI_DROPHILITED
;
931 if ( state
& wxLIST_STATE_FOCUSED
)
932 flags
|= LVNI_FOCUSED
;
933 if ( state
& wxLIST_STATE_SELECTED
)
934 flags
|= LVNI_SELECTED
;
936 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
940 wxImageList
*wxListCtrl::GetImageList(int which
) const
942 if ( which
== wxIMAGE_LIST_NORMAL
)
944 return m_imageListNormal
;
946 else if ( which
== wxIMAGE_LIST_SMALL
)
948 return m_imageListSmall
;
950 else if ( which
== wxIMAGE_LIST_STATE
)
952 return m_imageListState
;
957 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
960 if ( which
== wxIMAGE_LIST_NORMAL
)
962 flags
= LVSIL_NORMAL
;
963 if (m_ownsImageListNormal
) delete m_imageListNormal
;
964 m_imageListNormal
= imageList
;
965 m_ownsImageListNormal
= FALSE
;
967 else if ( which
== wxIMAGE_LIST_SMALL
)
970 if (m_ownsImageListSmall
) delete m_imageListSmall
;
971 m_imageListSmall
= imageList
;
972 m_ownsImageListSmall
= FALSE
;
974 else if ( which
== wxIMAGE_LIST_STATE
)
977 if (m_ownsImageListState
) delete m_imageListState
;
978 m_imageListState
= imageList
;
979 m_ownsImageListState
= FALSE
;
981 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
984 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
986 SetImageList(imageList
, which
);
987 if ( which
== wxIMAGE_LIST_NORMAL
)
988 m_ownsImageListNormal
= TRUE
;
989 else if ( which
== wxIMAGE_LIST_SMALL
)
990 m_ownsImageListSmall
= TRUE
;
991 else if ( which
== wxIMAGE_LIST_STATE
)
992 m_ownsImageListState
= TRUE
;
995 // ----------------------------------------------------------------------------
997 // ----------------------------------------------------------------------------
999 // Arranges the items
1000 bool wxListCtrl::Arrange(int flag
)
1003 if ( flag
== wxLIST_ALIGN_LEFT
)
1004 code
= LVA_ALIGNLEFT
;
1005 else if ( flag
== wxLIST_ALIGN_TOP
)
1006 code
= LVA_ALIGNTOP
;
1007 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1009 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1010 code
= LVA_SNAPTOGRID
;
1012 return (ListView_Arrange(GetHwnd(), code
) != 0);
1016 bool wxListCtrl::DeleteItem(long item
)
1018 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1021 // Deletes all items
1022 bool wxListCtrl::DeleteAllItems()
1024 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1027 // Deletes all items
1028 bool wxListCtrl::DeleteAllColumns()
1030 while ( m_colCount
> 0 )
1032 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1034 wxLogLastError(wxT("ListView_DeleteColumn"));
1042 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1048 bool wxListCtrl::DeleteColumn(int col
)
1050 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1052 if ( success
&& (m_colCount
> 0) )
1057 // Clears items, and columns if there are any.
1058 void wxListCtrl::ClearAll()
1061 if ( m_colCount
> 0 )
1065 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1067 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1069 // VS: ListView_EditLabel requires that the list has focus.
1071 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1075 m_textCtrl
->SetHWND(0);
1076 m_textCtrl
->UnsubclassWin();
1081 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1082 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1083 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1088 // End label editing, optionally cancelling the edit
1089 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1091 wxFAIL_MSG( _T("not implemented") );
1096 // Ensures this item is visible
1097 bool wxListCtrl::EnsureVisible(long item
)
1099 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1102 // Find an item whose label matches this string, starting from the item after 'start'
1103 // or the beginning if 'start' is -1.
1104 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1106 LV_FINDINFO findInfo
;
1108 findInfo
.flags
= LVFI_STRING
;
1110 findInfo
.flags
|= LVFI_PARTIAL
;
1113 // ListView_FindItem() excludes the first item from search and to look
1114 // through all the items you need to start from -1 which is unnatural and
1115 // inconsistent with the generic version - so we adjust the index
1118 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1121 // Find an item whose data matches this data, starting from the item after 'start'
1122 // or the beginning if 'start' is -1.
1123 long wxListCtrl::FindItem(long start
, long data
)
1125 LV_FINDINFO findInfo
;
1127 findInfo
.flags
= LVFI_PARAM
;
1128 findInfo
.lParam
= data
;
1130 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1133 // Find an item nearest this position in the specified direction, starting from
1134 // the item after 'start' or the beginning if 'start' is -1.
1135 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1137 LV_FINDINFO findInfo
;
1139 findInfo
.flags
= LVFI_NEARESTXY
;
1140 findInfo
.pt
.x
= pt
.x
;
1141 findInfo
.pt
.y
= pt
.y
;
1142 findInfo
.vkDirection
= VK_RIGHT
;
1144 if ( direction
== wxLIST_FIND_UP
)
1145 findInfo
.vkDirection
= VK_UP
;
1146 else if ( direction
== wxLIST_FIND_DOWN
)
1147 findInfo
.vkDirection
= VK_DOWN
;
1148 else if ( direction
== wxLIST_FIND_LEFT
)
1149 findInfo
.vkDirection
= VK_LEFT
;
1150 else if ( direction
== wxLIST_FIND_RIGHT
)
1151 findInfo
.vkDirection
= VK_RIGHT
;
1153 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1156 // Determines which item (if any) is at the specified point,
1157 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1158 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1160 LV_HITTESTINFO hitTestInfo
;
1161 hitTestInfo
.pt
.x
= (int) point
.x
;
1162 hitTestInfo
.pt
.y
= (int) point
.y
;
1164 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1167 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1168 flags
|= wxLIST_HITTEST_ABOVE
;
1169 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1170 flags
|= wxLIST_HITTEST_BELOW
;
1171 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1172 flags
|= wxLIST_HITTEST_NOWHERE
;
1173 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1174 flags
|= wxLIST_HITTEST_ONITEMICON
;
1175 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1176 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1177 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1178 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1179 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1180 flags
|= wxLIST_HITTEST_TOLEFT
;
1181 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1182 flags
|= wxLIST_HITTEST_TORIGHT
;
1184 return (long) hitTestInfo
.iItem
;
1187 // Inserts an item, returning the index of the new item if successful,
1189 long wxListCtrl::InsertItem(wxListItem
& info
)
1191 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1194 wxConvertToMSWListItem(this, info
, item
);
1196 // check whether it has any custom attributes
1197 if ( info
.HasAttributes() )
1200 wxListItemAttr
*attr
;
1201 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1205 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1207 else *attr
= *info
.GetAttributes();
1209 m_hasAnyAttr
= TRUE
;
1212 return (long) ListView_InsertItem(GetHwnd(), & item
);
1215 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1218 info
.m_text
= label
;
1219 info
.m_mask
= wxLIST_MASK_TEXT
;
1220 info
.m_itemId
= index
;
1221 return InsertItem(info
);
1224 // Inserts an image item
1225 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1228 info
.m_image
= imageIndex
;
1229 info
.m_mask
= wxLIST_MASK_IMAGE
;
1230 info
.m_itemId
= index
;
1231 return InsertItem(info
);
1234 // Inserts an image/string item
1235 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1238 info
.m_image
= imageIndex
;
1239 info
.m_text
= label
;
1240 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1241 info
.m_itemId
= index
;
1242 return InsertItem(info
);
1245 // For list view mode (only), inserts a column.
1246 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1251 lvCol
.pszText
= NULL
;
1253 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1255 lvCol
.mask
|= LVCF_TEXT
;
1256 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1257 lvCol
.cchTextMax
= 0; // Ignored
1259 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1261 lvCol
.mask
|= LVCF_FMT
;
1263 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1264 lvCol
.fmt
= LVCFMT_LEFT
;
1265 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1266 lvCol
.fmt
= LVCFMT_RIGHT
;
1267 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1268 lvCol
.fmt
= LVCFMT_CENTER
;
1271 lvCol
.mask
|= LVCF_WIDTH
;
1272 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1274 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1275 lvCol
.cx
= LVSCW_AUTOSIZE
;
1276 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1277 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1279 lvCol
.cx
= item
.m_width
;
1283 // always give some width to the new column: this one is compatible
1288 lvCol
.mask
|= LVCF_SUBITEM
;
1289 lvCol
.iSubItem
= col
;
1291 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1298 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1305 long wxListCtrl::InsertColumn(long col
,
1306 const wxString
& heading
,
1311 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1312 item
.m_text
= heading
;
1315 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1316 item
.m_width
= width
;
1318 item
.m_format
= format
;
1320 return InsertColumn(col
, item
);
1323 // Scrolls the list control. If in icon, small icon or report view mode,
1324 // x specifies the number of pixels to scroll. If in list view mode, x
1325 // specifies the number of columns to scroll.
1326 // If in icon, small icon or list view mode, y specifies the number of pixels
1327 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1328 bool wxListCtrl::ScrollList(int dx
, int dy
)
1330 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1335 // fn is a function which takes 3 long arguments: item1, item2, data.
1336 // item1 is the long data associated with a first item (NOT the index).
1337 // item2 is the long data associated with a second item (NOT the index).
1338 // data is the same value as passed to SortItems.
1339 // The return value is a negative number if the first item should precede the second
1340 // item, a positive number of the second item should precede the first,
1341 // or zero if the two items are equivalent.
1343 // data is arbitrary data to be passed to the sort function.
1344 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1346 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1349 // ----------------------------------------------------------------------------
1350 // message processing
1351 // ----------------------------------------------------------------------------
1353 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1355 if (cmd
== EN_UPDATE
)
1357 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1358 event
.SetEventObject( this );
1359 ProcessCommand(event
);
1362 else if (cmd
== EN_KILLFOCUS
)
1364 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1365 event
.SetEventObject( this );
1366 ProcessCommand(event
);
1373 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1375 // prepare the event
1376 // -----------------
1378 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1379 wxEventType eventType
= wxEVT_NULL
;
1381 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1383 // almost all messages use NM_LISTVIEW
1384 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1386 // this is true for almost all events
1387 event
.m_item
.m_data
= nmLV
->lParam
;
1389 switch ( nmhdr
->code
)
1391 case LVN_BEGINRDRAG
:
1392 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1396 if ( eventType
== wxEVT_NULL
)
1398 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1401 event
.m_itemIndex
= nmLV
->iItem
;
1402 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1403 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1406 case LVN_BEGINLABELEDIT
:
1408 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1409 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1410 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1411 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1415 case LVN_COLUMNCLICK
:
1416 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1417 event
.m_itemIndex
= -1;
1418 event
.m_col
= nmLV
->iSubItem
;
1421 case LVN_DELETEALLITEMS
:
1422 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1423 event
.m_itemIndex
= -1;
1429 case LVN_DELETEITEM
:
1430 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1431 event
.m_itemIndex
= nmLV
->iItem
;
1435 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1439 case LVN_ENDLABELEDIT
:
1441 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1442 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1443 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
);
1444 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1447 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1451 case LVN_SETDISPINFO
:
1453 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1454 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1455 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1459 case LVN_INSERTITEM
:
1460 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1461 event
.m_itemIndex
= nmLV
->iItem
;
1464 case LVN_ITEMCHANGED
:
1465 // This needs to be sent to wxListCtrl as a rather more concrete
1466 // event. For now, just detect a selection or deselection.
1467 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1469 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1470 event
.m_itemIndex
= nmLV
->iItem
;
1472 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1474 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1475 event
.m_itemIndex
= nmLV
->iItem
;
1485 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1486 WORD wVKey
= info
->wVKey
;
1488 // get the current selection
1489 long lItem
= GetNextItem(-1,
1491 wxLIST_STATE_SELECTED
);
1493 // <Enter> or <Space> activate the selected item if any (but
1494 // not with Shift and/or Ctrl as then they have a predefined
1495 // meaning for the list view)
1497 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1498 !(wxIsShiftDown() || wxIsCtrlDown()) )
1500 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1504 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1505 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1509 event
.m_item
.m_itemId
= lItem
;
1513 // fill the other fields too
1514 event
.m_item
.m_text
= GetItemText(lItem
);
1515 event
.m_item
.m_data
= GetItemData(lItem
);
1521 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1523 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1528 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1529 // if it happened on an item (and not on empty place)
1530 if ( nmLV
->iItem
== -1 )
1536 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1537 event
.m_itemIndex
= nmLV
->iItem
;
1538 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1539 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1543 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1544 subclass and check for the actual WM_RBUTTONDOWN message,
1545 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1546 before firing off. We want to have notify events for both down
1549 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1550 // don't do anything else
1551 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1556 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1557 LV_HITTESTINFO lvhti
;
1558 wxZeroMemory(lvhti
);
1560 ::GetCursorPos(&(lvhti
.pt
));
1561 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1562 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1564 if ( lvhti
.flags
& LVHT_ONITEM
)
1566 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1567 event
.m_itemIndex
= lvhti
.iItem
;
1568 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1569 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1576 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1578 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1580 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1585 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1586 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1587 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1588 event
.m_itemIndex
= hdr
->iItem
;
1593 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1595 *result
= OnCustomDraw(lParam
);
1598 #endif // _WIN32_IE >= 0x300
1600 case LVN_GETDISPINFO
:
1603 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1605 LV_ITEM
& lvi
= info
->item
;
1606 long item
= lvi
.iItem
;
1608 if ( lvi
.mask
& LVIF_TEXT
)
1610 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1611 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1614 if ( lvi
.mask
& LVIF_IMAGE
)
1616 lvi
.iImage
= OnGetItemImage(item
);
1619 // a little dose of healthy paranoia: as we never use
1620 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1621 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1622 _T("we don't support state callbacks yet!") );
1629 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1632 // process the event
1633 // -----------------
1635 event
.SetEventObject( this );
1636 event
.SetEventType(eventType
);
1638 if ( !GetEventHandler()->ProcessEvent(event
) )
1644 switch ( nmhdr
->code
)
1646 case LVN_DELETEALLITEMS
:
1647 // always return TRUE to suppress all additional LVN_DELETEITEM
1648 // notifications - this makes deleting all items from a list ctrl
1654 case LVN_ENDLABELEDIT
:
1656 *result
= event
.IsAllowed();
1661 *result
= !event
.IsAllowed();
1666 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1668 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
1670 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1671 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1672 switch ( nmcd
.dwDrawStage
)
1675 // if we've got any items with non standard attributes,
1676 // notify us before painting each item
1678 // for virtual controls, always suppose that we have attributes as
1679 // there is no way to check for this
1680 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1683 case CDDS_ITEMPREPAINT
:
1685 size_t item
= (size_t)nmcd
.dwItemSpec
;
1686 wxListItemAttr
*attr
=
1687 IsVirtual() ? OnGetItemAttr(item
)
1688 : (wxListItemAttr
*)m_attrs
.Get(item
);
1692 // nothing to do for this item
1693 return CDRF_DODEFAULT
;
1697 wxColour colText
, colBack
;
1698 if ( attr
->HasFont() )
1700 wxFont font
= attr
->GetFont();
1701 hFont
= (HFONT
)font
.GetResourceHandle();
1708 if ( attr
->HasTextColour() )
1710 colText
= attr
->GetTextColour();
1714 colText
= GetTextColour();
1717 if ( attr
->HasBackgroundColour() )
1719 colBack
= attr
->GetBackgroundColour();
1723 colBack
= GetBackgroundColour();
1726 lplvcd
->clrText
= wxColourToRGB(colText
);
1727 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1729 // note that if we wanted to set colours for
1730 // individual columns (subitems), we would have
1731 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1734 ::SelectObject(nmcd
.hdc
, hFont
);
1736 return CDRF_NEWFONT
;
1739 // fall through to return CDRF_DODEFAULT
1742 return CDRF_DODEFAULT
;
1746 #endif // NM_CUSTOMDRAW supported
1748 // Necessary for drawing hrules and vrules, if specified
1749 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1753 wxControl::OnPaint(event
);
1755 // Reset the device origin since it may have been set
1756 dc
.SetDeviceOrigin(0, 0);
1758 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1759 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1761 if (!drawHRules
&& !drawVRules
)
1763 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1766 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1768 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1770 wxSize clientSize
= GetClientSize();
1774 int itemCount
= GetItemCount();
1776 for (i
= 0; i
< itemCount
; i
++)
1778 if (GetItemRect(i
, itemRect
))
1780 cy
= itemRect
.GetTop();
1781 if (i
!= 0) // Don't draw the first one
1783 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1786 if (i
== (GetItemCount() - 1))
1788 cy
= itemRect
.GetBottom();
1789 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1793 i
= (GetItemCount() - 1);
1794 if (drawVRules
&& (i
> -1))
1796 wxRect firstItemRect
;
1797 GetItemRect(0, firstItemRect
);
1799 if (GetItemRect(i
, itemRect
))
1802 int x
= itemRect
.GetX();
1803 for (col
= 0; col
< GetColumnCount(); col
++)
1805 int colWidth
= GetColumnWidth(col
);
1807 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1813 // ----------------------------------------------------------------------------
1814 // virtual list controls
1815 // ----------------------------------------------------------------------------
1817 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
1819 // this is a pure virtual function, in fact - which is not really pure
1820 // because the controls which are not virtual don't need to implement it
1821 wxFAIL_MSG( _T("not supposed to be called") );
1823 return wxEmptyString
;
1826 int wxListCtrl::OnGetItemImage(long item
) const
1829 wxFAIL_MSG( _T("not supposed to be called") );
1834 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
1836 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
1837 _T("invalid item index in OnGetItemAttr()") );
1839 // no attributes by default
1843 void wxListCtrl::SetItemCount(long count
)
1845 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1847 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
1849 wxLogLastError(_T("ListView_SetItemCount"));
1853 // ----------------------------------------------------------------------------
1855 // ----------------------------------------------------------------------------
1857 // List item structure
1858 wxListItem::wxListItem()
1868 m_format
= wxLIST_FORMAT_CENTRE
;
1874 void wxListItem::Clear()
1883 m_format
= wxLIST_FORMAT_CENTRE
;
1885 m_text
= wxEmptyString
;
1887 if (m_attr
) delete m_attr
;
1891 void wxListItem::ClearAttributes()
1893 if (m_attr
) delete m_attr
;
1897 static void wxConvertFromMSWListItem(const wxListCtrl
*WXUNUSED(ctrl
), wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1899 info
.m_data
= lvItem
.lParam
;
1902 info
.m_stateMask
= 0;
1903 info
.m_itemId
= lvItem
.iItem
;
1905 long oldMask
= lvItem
.mask
;
1907 bool needText
= FALSE
;
1908 if (getFullInfo
!= 0)
1910 if ( lvItem
.mask
& LVIF_TEXT
)
1917 lvItem
.pszText
= new wxChar
[513];
1918 lvItem
.cchTextMax
= 512;
1920 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1921 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1922 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1925 if ( lvItem
.mask
& LVIF_STATE
)
1927 info
.m_mask
|= wxLIST_MASK_STATE
;
1929 if ( lvItem
.stateMask
& LVIS_CUT
)
1931 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1932 if ( lvItem
.state
& LVIS_CUT
)
1933 info
.m_state
|= wxLIST_STATE_CUT
;
1935 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1937 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1938 if ( lvItem
.state
& LVIS_DROPHILITED
)
1939 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1941 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1943 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1944 if ( lvItem
.state
& LVIS_FOCUSED
)
1945 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1947 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1949 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1950 if ( lvItem
.state
& LVIS_SELECTED
)
1951 info
.m_state
|= wxLIST_STATE_SELECTED
;
1955 if ( lvItem
.mask
& LVIF_TEXT
)
1957 info
.m_mask
|= wxLIST_MASK_TEXT
;
1958 info
.m_text
= lvItem
.pszText
;
1960 if ( lvItem
.mask
& LVIF_IMAGE
)
1962 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1963 info
.m_image
= lvItem
.iImage
;
1965 if ( lvItem
.mask
& LVIF_PARAM
)
1966 info
.m_mask
|= wxLIST_MASK_DATA
;
1967 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1968 info
.m_mask
|= wxLIST_SET_ITEM
;
1969 info
.m_col
= lvItem
.iSubItem
;
1974 delete[] lvItem
.pszText
;
1976 lvItem
.mask
= oldMask
;
1979 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1981 lvItem
.iItem
= (int) info
.m_itemId
;
1983 lvItem
.iImage
= info
.m_image
;
1984 lvItem
.lParam
= info
.m_data
;
1985 lvItem
.stateMask
= 0;
1988 lvItem
.iSubItem
= info
.m_col
;
1990 if (info
.m_mask
& wxLIST_MASK_STATE
)
1992 lvItem
.mask
|= LVIF_STATE
;
1993 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1995 lvItem
.stateMask
|= LVIS_CUT
;
1996 if (info
.m_state
& wxLIST_STATE_CUT
)
1997 lvItem
.state
|= LVIS_CUT
;
1999 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
2001 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2002 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
2003 lvItem
.state
|= LVIS_DROPHILITED
;
2005 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
2007 lvItem
.stateMask
|= LVIS_FOCUSED
;
2008 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
2009 lvItem
.state
|= LVIS_FOCUSED
;
2011 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
2013 lvItem
.stateMask
|= LVIS_SELECTED
;
2014 if (info
.m_state
& wxLIST_STATE_SELECTED
)
2015 lvItem
.state
|= LVIS_SELECTED
;
2019 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2021 lvItem
.mask
|= LVIF_TEXT
;
2022 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2024 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2028 // pszText is not const, hence the cast
2029 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2030 if ( lvItem
.pszText
)
2031 lvItem
.cchTextMax
= info
.m_text
.Length();
2033 lvItem
.cchTextMax
= 0;
2036 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2037 lvItem
.mask
|= LVIF_IMAGE
;
2038 if (info
.m_mask
& wxLIST_MASK_DATA
)
2039 lvItem
.mask
|= LVIF_PARAM
;
2042 // ----------------------------------------------------------------------------
2044 // ----------------------------------------------------------------------------
2046 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2048 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2049 : wxNotifyEvent(commandType
, id
)
2055 m_cancelled
= FALSE
;
2058 #endif // wxUSE_LISTCTRL