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
->UnsubclassWin();
309 m_textCtrl
->SetHWND(0);
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
->UnsubclassWin();
1076 m_textCtrl
->SetHWND(0);
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
))
1093 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
1095 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
1099 m_textCtrl->UnsubclassWin();
1100 m_textCtrl->SetHWND(0);
1110 // Ensures this item is visible
1111 bool wxListCtrl::EnsureVisible(long item
)
1113 return (ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0);
1116 // Find an item whose label matches this string, starting from the item after 'start'
1117 // or the beginning if 'start' is -1.
1118 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1120 LV_FINDINFO findInfo
;
1122 findInfo
.flags
= LVFI_STRING
;
1124 findInfo
.flags
|= LVFI_PARTIAL
;
1127 // ListView_FindItem() excludes the first item from search and to look
1128 // through all the items you need to start from -1 which is unnatural and
1129 // inconsistent with the generic version - so we adjust the index
1132 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1135 // Find an item whose data matches this data, starting from the item after 'start'
1136 // or the beginning if 'start' is -1.
1137 long wxListCtrl::FindItem(long start
, long data
)
1139 LV_FINDINFO findInfo
;
1141 findInfo
.flags
= LVFI_PARAM
;
1142 findInfo
.lParam
= data
;
1144 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1147 // Find an item nearest this position in the specified direction, starting from
1148 // the item after 'start' or the beginning if 'start' is -1.
1149 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1151 LV_FINDINFO findInfo
;
1153 findInfo
.flags
= LVFI_NEARESTXY
;
1154 findInfo
.pt
.x
= pt
.x
;
1155 findInfo
.pt
.y
= pt
.y
;
1156 findInfo
.vkDirection
= VK_RIGHT
;
1158 if ( direction
== wxLIST_FIND_UP
)
1159 findInfo
.vkDirection
= VK_UP
;
1160 else if ( direction
== wxLIST_FIND_DOWN
)
1161 findInfo
.vkDirection
= VK_DOWN
;
1162 else if ( direction
== wxLIST_FIND_LEFT
)
1163 findInfo
.vkDirection
= VK_LEFT
;
1164 else if ( direction
== wxLIST_FIND_RIGHT
)
1165 findInfo
.vkDirection
= VK_RIGHT
;
1167 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1170 // Determines which item (if any) is at the specified point,
1171 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1172 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1174 LV_HITTESTINFO hitTestInfo
;
1175 hitTestInfo
.pt
.x
= (int) point
.x
;
1176 hitTestInfo
.pt
.y
= (int) point
.y
;
1178 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1181 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1182 flags
|= wxLIST_HITTEST_ABOVE
;
1183 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1184 flags
|= wxLIST_HITTEST_BELOW
;
1185 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1186 flags
|= wxLIST_HITTEST_NOWHERE
;
1187 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1188 flags
|= wxLIST_HITTEST_ONITEMICON
;
1189 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1190 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1191 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1192 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1193 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1194 flags
|= wxLIST_HITTEST_TOLEFT
;
1195 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1196 flags
|= wxLIST_HITTEST_TORIGHT
;
1198 return (long) hitTestInfo
.iItem
;
1201 // Inserts an item, returning the index of the new item if successful,
1203 long wxListCtrl::InsertItem(wxListItem
& info
)
1205 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1208 wxConvertToMSWListItem(this, info
, item
);
1210 // check whether it has any custom attributes
1211 if ( info
.HasAttributes() )
1214 wxListItemAttr
*attr
;
1215 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1219 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1221 else *attr
= *info
.GetAttributes();
1223 m_hasAnyAttr
= TRUE
;
1226 return (long) ListView_InsertItem(GetHwnd(), & item
);
1229 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1232 info
.m_text
= label
;
1233 info
.m_mask
= wxLIST_MASK_TEXT
;
1234 info
.m_itemId
= index
;
1235 return InsertItem(info
);
1238 // Inserts an image item
1239 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1242 info
.m_image
= imageIndex
;
1243 info
.m_mask
= wxLIST_MASK_IMAGE
;
1244 info
.m_itemId
= index
;
1245 return InsertItem(info
);
1248 // Inserts an image/string item
1249 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1252 info
.m_image
= imageIndex
;
1253 info
.m_text
= label
;
1254 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1255 info
.m_itemId
= index
;
1256 return InsertItem(info
);
1259 // For list view mode (only), inserts a column.
1260 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1265 lvCol
.pszText
= NULL
;
1267 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1269 lvCol
.mask
|= LVCF_TEXT
;
1270 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1271 lvCol
.cchTextMax
= 0; // Ignored
1273 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1275 lvCol
.mask
|= LVCF_FMT
;
1277 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1278 lvCol
.fmt
= LVCFMT_LEFT
;
1279 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1280 lvCol
.fmt
= LVCFMT_RIGHT
;
1281 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1282 lvCol
.fmt
= LVCFMT_CENTER
;
1285 lvCol
.mask
|= LVCF_WIDTH
;
1286 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1288 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1289 lvCol
.cx
= LVSCW_AUTOSIZE
;
1290 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1291 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1293 lvCol
.cx
= item
.m_width
;
1297 // always give some width to the new column: this one is compatible
1302 lvCol
.mask
|= LVCF_SUBITEM
;
1303 lvCol
.iSubItem
= col
;
1305 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1312 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1319 long wxListCtrl::InsertColumn(long col
,
1320 const wxString
& heading
,
1325 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1326 item
.m_text
= heading
;
1329 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1330 item
.m_width
= width
;
1332 item
.m_format
= format
;
1334 return InsertColumn(col
, item
);
1337 // Scrolls the list control. If in icon, small icon or report view mode,
1338 // x specifies the number of pixels to scroll. If in list view mode, x
1339 // specifies the number of columns to scroll.
1340 // If in icon, small icon or list view mode, y specifies the number of pixels
1341 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1342 bool wxListCtrl::ScrollList(int dx
, int dy
)
1344 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1349 // fn is a function which takes 3 long arguments: item1, item2, data.
1350 // item1 is the long data associated with a first item (NOT the index).
1351 // item2 is the long data associated with a second item (NOT the index).
1352 // data is the same value as passed to SortItems.
1353 // The return value is a negative number if the first item should precede the second
1354 // item, a positive number of the second item should precede the first,
1355 // or zero if the two items are equivalent.
1357 // data is arbitrary data to be passed to the sort function.
1358 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1360 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1363 // ----------------------------------------------------------------------------
1364 // message processing
1365 // ----------------------------------------------------------------------------
1367 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1369 if (cmd
== EN_UPDATE
)
1371 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1372 event
.SetEventObject( this );
1373 ProcessCommand(event
);
1376 else if (cmd
== EN_KILLFOCUS
)
1378 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1379 event
.SetEventObject( this );
1380 ProcessCommand(event
);
1387 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1389 // prepare the event
1390 // -----------------
1392 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1393 wxEventType eventType
= wxEVT_NULL
;
1395 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1397 // almost all messages use NM_LISTVIEW
1398 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1400 // this is true for almost all events
1401 event
.m_item
.m_data
= nmLV
->lParam
;
1403 switch ( nmhdr
->code
)
1405 case LVN_BEGINRDRAG
:
1406 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1410 if ( eventType
== wxEVT_NULL
)
1412 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1415 event
.m_itemIndex
= nmLV
->iItem
;
1416 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1417 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1420 case LVN_BEGINLABELEDIT
:
1422 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1423 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1424 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1428 case LVN_COLUMNCLICK
:
1429 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1430 event
.m_itemIndex
= -1;
1431 event
.m_col
= nmLV
->iSubItem
;
1434 case LVN_DELETEALLITEMS
:
1435 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1436 event
.m_itemIndex
= -1;
1442 case LVN_DELETEITEM
:
1443 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1444 event
.m_itemIndex
= nmLV
->iItem
;
1448 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1452 case LVN_ENDLABELEDIT
:
1454 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1455 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1456 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
);
1457 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1462 case LVN_SETDISPINFO
:
1464 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1465 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1466 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1470 case LVN_INSERTITEM
:
1471 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1472 event
.m_itemIndex
= nmLV
->iItem
;
1475 case LVN_ITEMCHANGED
:
1476 // This needs to be sent to wxListCtrl as a rather more concrete
1477 // event. For now, just detect a selection or deselection.
1478 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1480 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1481 event
.m_itemIndex
= nmLV
->iItem
;
1483 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1485 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1486 event
.m_itemIndex
= nmLV
->iItem
;
1496 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1497 WORD wVKey
= info
->wVKey
;
1499 // get the current selection
1500 long lItem
= GetNextItem(-1,
1502 wxLIST_STATE_SELECTED
);
1504 // <Enter> or <Space> activate the selected item if any (but
1505 // not with Shift and/or Ctrl as then they have a predefined
1506 // meaning for the list view)
1508 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1509 !(wxIsShiftDown() || wxIsCtrlDown()) )
1511 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1512 event
.m_itemIndex
= lItem
;
1516 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1517 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1522 // fill the other fields too
1523 event
.m_item
.m_text
= GetItemText(lItem
);
1524 event
.m_item
.m_data
= GetItemData(lItem
);
1530 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1532 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1537 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1538 // if it happened on an item (and not on empty place)
1539 if ( nmLV
->iItem
== -1 )
1545 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1546 event
.m_itemIndex
= nmLV
->iItem
;
1547 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1548 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1552 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1553 subclass and check for the actual WM_RBUTTONDOWN message,
1554 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1555 before firing off. We want to have notify events for both down
1558 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1559 // don't do anything else
1560 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1565 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1566 LV_HITTESTINFO lvhti
;
1567 wxZeroMemory(lvhti
);
1569 ::GetCursorPos(&(lvhti
.pt
));
1570 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1571 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1573 if ( lvhti
.flags
& LVHT_ONITEM
)
1575 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1576 event
.m_itemIndex
= lvhti
.iItem
;
1577 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1578 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1585 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1587 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1589 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1594 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1595 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1596 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1597 event
.m_itemIndex
= hdr
->iItem
;
1602 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1605 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1606 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1607 switch( nmcd
.dwDrawStage
)
1610 // if we've got any items with non standard attributes,
1611 // notify us before painting each item
1612 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1616 case CDDS_ITEMPREPAINT
:
1618 wxListItemAttr
*attr
=
1619 (wxListItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1623 // nothing to do for this item
1624 return CDRF_DODEFAULT
;
1628 wxColour colText
, colBack
;
1629 if ( attr
->HasFont() )
1631 wxFont font
= attr
->GetFont();
1632 hFont
= (HFONT
)font
.GetResourceHandle();
1639 if ( attr
->HasTextColour() )
1641 colText
= attr
->GetTextColour();
1645 colText
= GetTextColour();
1648 if ( attr
->HasBackgroundColour() )
1650 colBack
= attr
->GetBackgroundColour();
1654 colBack
= GetBackgroundColour();
1657 // note that if we wanted to set colours for
1658 // individual columns (subitems), we would have
1659 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1662 ::SelectObject(nmcd
.hdc
, hFont
);
1664 *result
= CDRF_NEWFONT
;
1668 *result
= CDRF_DODEFAULT
;
1671 lplvcd
->clrText
= wxColourToRGB(colText
);
1672 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1678 *result
= CDRF_DODEFAULT
;
1682 // break; // can never be reached
1683 #endif // _WIN32_IE >= 0x300
1685 case LVN_GETDISPINFO
:
1688 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1690 LV_ITEM
& lvi
= info
->item
;
1691 long item
= lvi
.iItem
;
1693 if ( lvi
.mask
& LVIF_TEXT
)
1695 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
1696 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
1699 if ( lvi
.mask
& LVIF_IMAGE
)
1701 lvi
.iImage
= OnGetItemImage(item
);
1704 // a little dose of healthy paranoia: as we never use
1705 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1706 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
1707 _T("we don't support state callbacks yet!") );
1714 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1717 // process the event
1718 // -----------------
1720 event
.SetEventObject( this );
1721 event
.SetEventType(eventType
);
1723 if ( !GetEventHandler()->ProcessEvent(event
) )
1729 switch ( nmhdr
->code
)
1731 case LVN_DELETEALLITEMS
:
1732 // always return TRUE to suppress all additional LVN_DELETEITEM
1733 // notifications - this makes deleting all items from a list ctrl
1739 case LVN_ENDLABELEDIT
:
1741 *result
= event
.IsAllowed();
1746 *result
= !event
.IsAllowed();
1751 // Necessary for drawing hrules and vrules, if specified
1752 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1756 wxControl::OnPaint(event
);
1758 // Reset the device origin since it may have been set
1759 dc
.SetDeviceOrigin(0, 0);
1761 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1762 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1764 if (!drawHRules
&& !drawVRules
)
1766 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1769 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1771 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1773 wxSize clientSize
= GetClientSize();
1777 int itemCount
= GetItemCount();
1779 for (i
= 0; i
< itemCount
; i
++)
1781 if (GetItemRect(i
, itemRect
))
1783 cy
= itemRect
.GetTop();
1784 if (i
!= 0) // Don't draw the first one
1786 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1789 if (i
== (GetItemCount() - 1))
1791 cy
= itemRect
.GetBottom();
1792 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1796 i
= (GetItemCount() - 1);
1797 if (drawVRules
&& (i
> -1))
1799 wxRect firstItemRect
;
1800 GetItemRect(0, firstItemRect
);
1802 if (GetItemRect(i
, itemRect
))
1805 int x
= itemRect
.GetX();
1806 for (col
= 0; col
< GetColumnCount(); col
++)
1808 int colWidth
= GetColumnWidth(col
);
1810 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1816 // ----------------------------------------------------------------------------
1817 // virtual list controls
1818 // ----------------------------------------------------------------------------
1820 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
1822 // this is a pure virtual function, in fact - which is not really pure
1823 // because the controls which are not virtual don't need to implement it
1824 wxFAIL_MSG( _T("not supposed to be called") );
1826 return wxEmptyString
;
1829 int wxListCtrl::OnGetItemImage(long item
) const
1832 wxFAIL_MSG( _T("not supposed to be called") );
1837 void wxListCtrl::SetItemCount(long count
)
1839 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1841 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, 0) )
1843 wxLogLastError(_T("ListView_SetItemCount"));
1847 // ----------------------------------------------------------------------------
1849 // ----------------------------------------------------------------------------
1851 // List item structure
1852 wxListItem::wxListItem()
1862 m_format
= wxLIST_FORMAT_CENTRE
;
1868 void wxListItem::Clear()
1877 m_format
= wxLIST_FORMAT_CENTRE
;
1879 m_text
= wxEmptyString
;
1881 if (m_attr
) delete m_attr
;
1885 void wxListItem::ClearAttributes()
1887 if (m_attr
) delete m_attr
;
1891 static void wxConvertFromMSWListItem(const wxListCtrl
*WXUNUSED(ctrl
), wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1893 info
.m_data
= lvItem
.lParam
;
1896 info
.m_stateMask
= 0;
1897 info
.m_itemId
= lvItem
.iItem
;
1899 long oldMask
= lvItem
.mask
;
1901 bool needText
= FALSE
;
1902 if (getFullInfo
!= 0)
1904 if ( lvItem
.mask
& LVIF_TEXT
)
1911 lvItem
.pszText
= new wxChar
[513];
1912 lvItem
.cchTextMax
= 512;
1914 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1915 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1916 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1919 if ( lvItem
.mask
& LVIF_STATE
)
1921 info
.m_mask
|= wxLIST_MASK_STATE
;
1923 if ( lvItem
.stateMask
& LVIS_CUT
)
1925 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1926 if ( lvItem
.state
& LVIS_CUT
)
1927 info
.m_state
|= wxLIST_STATE_CUT
;
1929 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1931 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1932 if ( lvItem
.state
& LVIS_DROPHILITED
)
1933 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1935 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1937 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1938 if ( lvItem
.state
& LVIS_FOCUSED
)
1939 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1941 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1943 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1944 if ( lvItem
.state
& LVIS_SELECTED
)
1945 info
.m_state
|= wxLIST_STATE_SELECTED
;
1949 if ( lvItem
.mask
& LVIF_TEXT
)
1951 info
.m_mask
|= wxLIST_MASK_TEXT
;
1952 info
.m_text
= lvItem
.pszText
;
1954 if ( lvItem
.mask
& LVIF_IMAGE
)
1956 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1957 info
.m_image
= lvItem
.iImage
;
1959 if ( lvItem
.mask
& LVIF_PARAM
)
1960 info
.m_mask
|= wxLIST_MASK_DATA
;
1961 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1962 info
.m_mask
|= wxLIST_SET_ITEM
;
1963 info
.m_col
= lvItem
.iSubItem
;
1968 delete[] lvItem
.pszText
;
1970 lvItem
.mask
= oldMask
;
1973 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1975 lvItem
.iItem
= (int) info
.m_itemId
;
1977 lvItem
.iImage
= info
.m_image
;
1978 lvItem
.lParam
= info
.m_data
;
1979 lvItem
.stateMask
= 0;
1982 lvItem
.iSubItem
= info
.m_col
;
1984 if (info
.m_mask
& wxLIST_MASK_STATE
)
1986 lvItem
.mask
|= LVIF_STATE
;
1987 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1989 lvItem
.stateMask
|= LVIS_CUT
;
1990 if (info
.m_state
& wxLIST_STATE_CUT
)
1991 lvItem
.state
|= LVIS_CUT
;
1993 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1995 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1996 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1997 lvItem
.state
|= LVIS_DROPHILITED
;
1999 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
2001 lvItem
.stateMask
|= LVIS_FOCUSED
;
2002 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
2003 lvItem
.state
|= LVIS_FOCUSED
;
2005 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
2007 lvItem
.stateMask
|= LVIS_SELECTED
;
2008 if (info
.m_state
& wxLIST_STATE_SELECTED
)
2009 lvItem
.state
|= LVIS_SELECTED
;
2013 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2015 lvItem
.mask
|= LVIF_TEXT
;
2016 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2018 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2022 // pszText is not const, hence the cast
2023 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2024 if ( lvItem
.pszText
)
2025 lvItem
.cchTextMax
= info
.m_text
.Length();
2027 lvItem
.cchTextMax
= 0;
2030 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2031 lvItem
.mask
|= LVIF_IMAGE
;
2032 if (info
.m_mask
& wxLIST_MASK_DATA
)
2033 lvItem
.mask
|= LVIF_PARAM
;
2036 // ----------------------------------------------------------------------------
2038 // ----------------------------------------------------------------------------
2040 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2042 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2043 : wxNotifyEvent(commandType
, id
)
2049 m_cancelled
= FALSE
;
2052 #endif // wxUSE_LISTCTRL