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"
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 #ifdef __GNUWIN32_OLD__
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
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
72 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
95 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
96 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
98 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
99 EVT_PAINT(wxListCtrl::OnPaint
)
102 // ============================================================================
104 // ============================================================================
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 void wxListEvent::CopyObject(wxObject
& object_dest
) const
112 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
114 wxNotifyEvent::CopyObject(object_dest
);
116 obj
->m_code
= m_code
;
117 obj
->m_itemIndex
= m_itemIndex
;
118 obj
->m_oldItemIndex
= m_oldItemIndex
;
120 obj
->m_cancelled
= m_cancelled
;
121 obj
->m_pointDrag
= m_pointDrag
;
122 obj
->m_item
.m_mask
= m_item
.m_mask
;
123 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
124 obj
->m_item
.m_col
= m_item
.m_col
;
125 obj
->m_item
.m_state
= m_item
.m_state
;
126 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
127 obj
->m_item
.m_text
= m_item
.m_text
;
128 obj
->m_item
.m_image
= m_item
.m_image
;
129 obj
->m_item
.m_data
= m_item
.m_data
;
130 obj
->m_item
.m_format
= m_item
.m_format
;
131 obj
->m_item
.m_width
= m_item
.m_width
;
133 if ( m_item
.HasAttributes() )
135 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
136 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
137 obj
->m_item
.SetFont(m_item
.GetFont());
141 // ----------------------------------------------------------------------------
142 // wxListCtrl construction
143 // ----------------------------------------------------------------------------
145 void wxListCtrl::Init()
147 m_imageListNormal
= NULL
;
148 m_imageListSmall
= NULL
;
149 m_imageListState
= NULL
;
150 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
154 m_hasAnyAttr
= FALSE
;
157 bool wxListCtrl::Create(wxWindow
*parent
,
162 const wxValidator
& validator
,
163 const wxString
& name
)
166 SetValidator(validator
);
167 #endif // wxUSE_VALIDATORS
176 m_windowStyle
= style
;
189 m_windowId
= (id
== -1) ? NewControlId() : id
;
191 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
192 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
193 if ( wxStyleHasBorder(m_windowStyle
) )
195 m_baseStyle
= wstyle
;
197 if ( !DoCreateControl(x
, y
, width
, height
) )
201 parent
->AddChild(this);
206 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
208 DWORD wstyle
= m_baseStyle
;
211 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
213 // Even with extended styles, need to combine with WS_BORDER
214 // for them to look right.
218 long oldStyle
= 0; // Dummy
219 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
221 // Create the ListView control.
222 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
227 GetWinHwnd(GetParent()),
234 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
239 // for comctl32.dll v 4.70+ we want to have this attribute because it's
240 // prettier (and also because wxGTK does it like this)
241 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
243 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
244 0, LVS_EX_FULLROWSELECT
);
247 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
248 SetForegroundColour(GetParent()->GetForegroundColour());
255 void wxListCtrl::UpdateStyle()
259 // The new window view style
261 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
262 dwStyleNew
|= m_baseStyle
;
264 // Get the current window style.
265 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
267 // Only set the window style if the view bits have changed.
268 if ( dwStyleOld
!= dwStyleNew
)
270 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
275 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
279 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
281 delete (wxListItemAttr
*)node
->Data();
287 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
290 m_hasAnyAttr
= FALSE
;
294 wxListCtrl::~wxListCtrl()
296 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
300 m_textCtrl
->UnsubclassWin();
301 m_textCtrl
->SetHWND(0);
306 if (m_ownsImageListNormal
) delete m_imageListNormal
;
307 if (m_ownsImageListSmall
) delete m_imageListSmall
;
308 if (m_ownsImageListState
) delete m_imageListState
;
311 // ----------------------------------------------------------------------------
312 // set/get/change style
313 // ----------------------------------------------------------------------------
315 // Add or remove a single window style
316 void wxListCtrl::SetSingleStyle(long style
, bool add
)
318 long flag
= GetWindowStyleFlag();
320 // Get rid of conflicting styles
323 if ( style
& wxLC_MASK_TYPE
)
324 flag
= flag
& ~wxLC_MASK_TYPE
;
325 if ( style
& wxLC_MASK_ALIGN
)
326 flag
= flag
& ~wxLC_MASK_ALIGN
;
327 if ( style
& wxLC_MASK_SORT
)
328 flag
= flag
& ~wxLC_MASK_SORT
;
344 m_windowStyle
= flag
;
349 // Set the whole window style
350 void wxListCtrl::SetWindowStyleFlag(long flag
)
352 m_windowStyle
= flag
;
357 // Can be just a single style, or a bitlist
358 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
361 if ( style
& wxLC_ICON
)
363 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
364 oldStyle
-= LVS_SMALLICON
;
365 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
366 oldStyle
-= LVS_REPORT
;
367 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
368 oldStyle
-= LVS_LIST
;
372 if ( style
& wxLC_SMALL_ICON
)
374 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
375 oldStyle
-= LVS_ICON
;
376 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
377 oldStyle
-= LVS_REPORT
;
378 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
379 oldStyle
-= LVS_LIST
;
380 wstyle
|= LVS_SMALLICON
;
383 if ( style
& wxLC_LIST
)
385 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
386 oldStyle
-= LVS_ICON
;
387 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
388 oldStyle
-= LVS_REPORT
;
389 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
390 oldStyle
-= LVS_SMALLICON
;
394 if ( style
& wxLC_REPORT
)
396 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
397 oldStyle
-= LVS_ICON
;
398 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
399 oldStyle
-= LVS_LIST
;
400 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
401 oldStyle
-= LVS_SMALLICON
;
403 wstyle
|= LVS_REPORT
;
406 if ( style
& wxLC_ALIGN_LEFT
)
408 if ( oldStyle
& LVS_ALIGNTOP
)
409 oldStyle
-= LVS_ALIGNTOP
;
410 wstyle
|= LVS_ALIGNLEFT
;
413 if ( style
& wxLC_ALIGN_TOP
)
415 if ( oldStyle
& LVS_ALIGNLEFT
)
416 oldStyle
-= LVS_ALIGNLEFT
;
417 wstyle
|= LVS_ALIGNTOP
;
420 if ( style
& wxLC_AUTOARRANGE
)
421 wstyle
|= LVS_AUTOARRANGE
;
423 // Apparently, no such style (documentation wrong?)
425 if ( style & wxLC_BUTTON )
426 wstyle |= LVS_BUTTON;
429 if ( style
& wxLC_NO_SORT_HEADER
)
430 wstyle
|= LVS_NOSORTHEADER
;
432 if ( style
& wxLC_NO_HEADER
)
433 wstyle
|= LVS_NOCOLUMNHEADER
;
435 if ( style
& wxLC_EDIT_LABELS
)
436 wstyle
|= LVS_EDITLABELS
;
438 if ( style
& wxLC_SINGLE_SEL
)
439 wstyle
|= LVS_SINGLESEL
;
441 if ( style
& wxLC_SORT_ASCENDING
)
443 if ( oldStyle
& LVS_SORTDESCENDING
)
444 oldStyle
-= LVS_SORTDESCENDING
;
445 wstyle
|= LVS_SORTASCENDING
;
448 if ( style
& wxLC_SORT_DESCENDING
)
450 if ( oldStyle
& LVS_SORTASCENDING
)
451 oldStyle
-= LVS_SORTASCENDING
;
452 wstyle
|= LVS_SORTDESCENDING
;
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 // Sets the foreground, i.e. text, colour
463 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
465 if ( !wxWindow::SetForegroundColour(col
) )
468 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
473 // Sets the background colour
474 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
476 if ( !wxWindow::SetBackgroundColour(col
) )
479 // we set the same colour for both the "empty" background and the items
481 COLORREF color
= wxColourToRGB(col
);
482 ListView_SetBkColor(GetHwnd(), color
);
483 ListView_SetTextBkColor(GetHwnd(), color
);
488 // Gets information about this column
489 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
494 lvCol
.pszText
= NULL
;
496 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
498 lvCol
.mask
|= LVCF_TEXT
;
499 lvCol
.pszText
= new wxChar
[513];
500 lvCol
.cchTextMax
= 512;
503 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
505 // item.m_subItem = lvCol.iSubItem;
506 item
.m_width
= lvCol
.cx
;
508 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
510 item
.m_text
= lvCol
.pszText
;
511 delete[] lvCol
.pszText
;
514 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
516 if (lvCol
.fmt
== LVCFMT_LEFT
)
517 item
.m_format
= wxLIST_FORMAT_LEFT
;
518 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
519 item
.m_format
= wxLIST_FORMAT_RIGHT
;
520 else if (lvCol
.fmt
== LVCFMT_CENTER
)
521 item
.m_format
= wxLIST_FORMAT_CENTRE
;
527 // Sets information about this column
528 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
533 lvCol
.pszText
= NULL
;
535 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
537 lvCol
.mask
|= LVCF_TEXT
;
538 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
539 lvCol
.cchTextMax
= 0; // Ignored
541 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
543 lvCol
.mask
|= LVCF_FMT
;
545 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
546 lvCol
.fmt
= LVCFMT_LEFT
;
547 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
548 lvCol
.fmt
= LVCFMT_RIGHT
;
549 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
550 lvCol
.fmt
= LVCFMT_CENTER
;
553 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
555 lvCol
.mask
|= LVCF_WIDTH
;
556 lvCol
.cx
= item
.m_width
;
558 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
559 lvCol
.cx
= LVSCW_AUTOSIZE
;
560 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
561 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
563 lvCol
.mask
|= LVCF_SUBITEM
;
564 lvCol
.iSubItem
= col
;
565 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
568 // Gets the column width
569 int wxListCtrl::GetColumnWidth(int col
) const
571 return ListView_GetColumnWidth(GetHwnd(), col
);
574 // Sets the column width
575 bool wxListCtrl::SetColumnWidth(int col
, int width
)
578 if ( m_windowStyle
& wxLC_LIST
)
582 if ( width2
== wxLIST_AUTOSIZE
)
583 width2
= LVSCW_AUTOSIZE
;
584 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
585 width2
= LVSCW_AUTOSIZE_USEHEADER
;
587 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
590 // Gets the number of items that can fit vertically in the
591 // visible area of the list control (list or report view)
592 // or the total number of items in the list control (icon
593 // or small icon view)
594 int wxListCtrl::GetCountPerPage() const
596 return ListView_GetCountPerPage(GetHwnd());
599 // Gets the edit control for editing labels.
600 wxTextCtrl
* wxListCtrl::GetEditControl() const
605 // Gets information about the item
606 bool wxListCtrl::GetItem(wxListItem
& info
) const
609 wxZeroMemory(lvItem
);
611 lvItem
.iItem
= info
.m_itemId
;
612 lvItem
.iSubItem
= info
.m_col
;
614 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
616 lvItem
.mask
|= LVIF_TEXT
;
617 lvItem
.pszText
= new wxChar
[513];
618 lvItem
.cchTextMax
= 512;
622 lvItem
.pszText
= NULL
;
625 if (info
.m_mask
& wxLIST_MASK_DATA
)
626 lvItem
.mask
|= LVIF_PARAM
;
628 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
629 lvItem
.mask
|= LVIF_IMAGE
;
631 if ( info
.m_mask
& wxLIST_MASK_STATE
)
633 lvItem
.mask
|= LVIF_STATE
;
634 // the other bits are hardly interesting anyhow
635 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
638 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
641 wxLogError(_("Couldn't retrieve information about list control item %d."),
646 wxConvertFromMSWListItem(this, info
, lvItem
);
650 delete[] lvItem
.pszText
;
655 // Sets information about the item
656 bool wxListCtrl::SetItem(wxListItem
& info
)
659 wxConvertToMSWListItem(this, info
, item
);
662 if ( !ListView_SetItem(GetHwnd(), &item
) )
664 wxLogDebug(_T("ListView_SetItem() failed"));
669 // we need to update the item immediately to show the new image
670 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
672 // check whether it has any custom attributes
673 if ( info
.HasAttributes() )
675 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
678 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
680 *attr
= *info
.GetAttributes();
684 // if the colour has changed, we must redraw the item
690 // we need this to make the change visible right now
691 ListView_Update(GetHwnd(), item
.iItem
);
697 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
701 info
.m_mask
= wxLIST_MASK_TEXT
;
702 info
.m_itemId
= index
;
706 info
.m_image
= imageId
;
707 info
.m_mask
|= wxLIST_MASK_IMAGE
;
709 return SetItem(info
);
713 // Gets the item state
714 int wxListCtrl::GetItemState(long item
, long stateMask
) const
718 info
.m_mask
= wxLIST_MASK_STATE
;
719 info
.m_stateMask
= stateMask
;
720 info
.m_itemId
= item
;
728 // Sets the item state
729 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
733 info
.m_mask
= wxLIST_MASK_STATE
;
734 info
.m_state
= state
;
735 info
.m_stateMask
= stateMask
;
736 info
.m_itemId
= item
;
738 return SetItem(info
);
741 // Sets the item image
742 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
746 info
.m_mask
= wxLIST_MASK_IMAGE
;
747 info
.m_image
= image
;
748 info
.m_itemId
= item
;
750 return SetItem(info
);
753 // Gets the item text
754 wxString
wxListCtrl::GetItemText(long item
) const
758 info
.m_mask
= wxLIST_MASK_TEXT
;
759 info
.m_itemId
= item
;
766 // Sets the item text
767 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
771 info
.m_mask
= wxLIST_MASK_TEXT
;
772 info
.m_itemId
= item
;
778 // Gets the item data
779 long wxListCtrl::GetItemData(long item
) const
783 info
.m_mask
= wxLIST_MASK_DATA
;
784 info
.m_itemId
= item
;
791 // Sets the item data
792 bool wxListCtrl::SetItemData(long item
, long data
)
796 info
.m_mask
= wxLIST_MASK_DATA
;
797 info
.m_itemId
= item
;
800 return SetItem(info
);
803 // Gets the item rectangle
804 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
808 int code2
= LVIR_BOUNDS
;
809 if ( code
== wxLIST_RECT_BOUNDS
)
811 else if ( code
== wxLIST_RECT_ICON
)
813 else if ( code
== wxLIST_RECT_LABEL
)
817 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
819 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
824 rect
.width
= rect2
.right
- rect2
.left
;
825 rect
.height
= rect2
.bottom
- rect2
.top
;
829 // Gets the item position
830 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
834 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
836 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
840 // Sets the item position.
841 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
843 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
846 // Gets the number of items in the list control
847 int wxListCtrl::GetItemCount() const
849 return ListView_GetItemCount(GetHwnd());
852 // Retrieves the spacing between icons in pixels.
853 // If small is TRUE, gets the spacing for the small icon
854 // view, otherwise the large icon view.
855 int wxListCtrl::GetItemSpacing(bool isSmall
) const
857 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
860 // Gets the number of selected items in the list control
861 int wxListCtrl::GetSelectedItemCount() const
863 return ListView_GetSelectedCount(GetHwnd());
866 // Gets the text colour of the listview
867 wxColour
wxListCtrl::GetTextColour() const
869 COLORREF ref
= ListView_GetTextColor(GetHwnd());
870 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
874 // Sets the text colour of the listview
875 void wxListCtrl::SetTextColour(const wxColour
& col
)
877 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
880 // Gets the index of the topmost visible item when in
881 // list or report view
882 long wxListCtrl::GetTopItem() const
884 return (long) ListView_GetTopIndex(GetHwnd());
887 // Searches for an item, starting from 'item'.
888 // 'geometry' is one of
889 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
890 // 'state' is a state bit flag, one or more of
891 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
892 // item can be -1 to find the first item that matches the
894 // Returns the item or -1 if unsuccessful.
895 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
899 if ( geom
== wxLIST_NEXT_ABOVE
)
901 if ( geom
== wxLIST_NEXT_ALL
)
903 if ( geom
== wxLIST_NEXT_BELOW
)
905 if ( geom
== wxLIST_NEXT_LEFT
)
906 flags
|= LVNI_TOLEFT
;
907 if ( geom
== wxLIST_NEXT_RIGHT
)
908 flags
|= LVNI_TORIGHT
;
910 if ( state
& wxLIST_STATE_CUT
)
912 if ( state
& wxLIST_STATE_DROPHILITED
)
913 flags
|= LVNI_DROPHILITED
;
914 if ( state
& wxLIST_STATE_FOCUSED
)
915 flags
|= LVNI_FOCUSED
;
916 if ( state
& wxLIST_STATE_SELECTED
)
917 flags
|= LVNI_SELECTED
;
919 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
923 wxImageList
*wxListCtrl::GetImageList(int which
) const
925 if ( which
== wxIMAGE_LIST_NORMAL
)
927 return m_imageListNormal
;
929 else if ( which
== wxIMAGE_LIST_SMALL
)
931 return m_imageListSmall
;
933 else if ( which
== wxIMAGE_LIST_STATE
)
935 return m_imageListState
;
940 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
943 if ( which
== wxIMAGE_LIST_NORMAL
)
945 flags
= LVSIL_NORMAL
;
946 if (m_ownsImageListNormal
) delete m_imageListNormal
;
947 m_imageListNormal
= imageList
;
948 m_ownsImageListNormal
= FALSE
;
950 else if ( which
== wxIMAGE_LIST_SMALL
)
953 if (m_ownsImageListSmall
) delete m_imageListSmall
;
954 m_imageListSmall
= imageList
;
955 m_ownsImageListSmall
= FALSE
;
957 else if ( which
== wxIMAGE_LIST_STATE
)
960 if (m_ownsImageListState
) delete m_imageListState
;
961 m_imageListState
= imageList
;
962 m_ownsImageListState
= FALSE
;
964 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
967 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
969 SetImageList(imageList
, which
);
970 if ( which
== wxIMAGE_LIST_NORMAL
)
971 m_ownsImageListNormal
= TRUE
;
972 else if ( which
== wxIMAGE_LIST_SMALL
)
973 m_ownsImageListSmall
= TRUE
;
974 else if ( which
== wxIMAGE_LIST_STATE
)
975 m_ownsImageListState
= TRUE
;
978 // ----------------------------------------------------------------------------
980 // ----------------------------------------------------------------------------
982 // Arranges the items
983 bool wxListCtrl::Arrange(int flag
)
986 if ( flag
== wxLIST_ALIGN_LEFT
)
987 code
= LVA_ALIGNLEFT
;
988 else if ( flag
== wxLIST_ALIGN_TOP
)
990 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
992 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
993 code
= LVA_SNAPTOGRID
;
995 return (ListView_Arrange(GetHwnd(), code
) != 0);
999 bool wxListCtrl::DeleteItem(long item
)
1001 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1004 // Deletes all items
1005 bool wxListCtrl::DeleteAllItems()
1007 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1010 // Deletes all items
1011 bool wxListCtrl::DeleteAllColumns()
1013 while ( m_colCount
> 0 )
1015 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1017 wxLogLastError(wxT("ListView_DeleteColumn"));
1025 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1031 bool wxListCtrl::DeleteColumn(int col
)
1033 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1035 if ( success
&& (m_colCount
> 0) )
1040 // Clears items, and columns if there are any.
1041 void wxListCtrl::ClearAll()
1044 if ( m_colCount
> 0 )
1048 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1050 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1052 // VS: ListView_EditLabel requires that the list has focus.
1054 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1058 m_textCtrl
->UnsubclassWin();
1059 m_textCtrl
->SetHWND(0);
1064 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1065 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1066 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1071 // End label editing, optionally cancelling the edit
1072 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1076 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
1078 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
1082 m_textCtrl->UnsubclassWin();
1083 m_textCtrl->SetHWND(0);
1093 // Ensures this item is visible
1094 bool wxListCtrl::EnsureVisible(long item
)
1096 return (ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0);
1099 // Find an item whose label matches this string, starting from the item after 'start'
1100 // or the beginning if 'start' is -1.
1101 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1103 LV_FINDINFO findInfo
;
1105 findInfo
.flags
= LVFI_STRING
;
1107 findInfo
.flags
|= LVFI_PARTIAL
;
1110 // ListView_FindItem() excludes the first item from search and to look
1111 // through all the items you need to start from -1 which is unnatural and
1112 // inconsistent with the generic version - so we adjust the index
1115 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1118 // Find an item whose data matches this data, starting from the item after 'start'
1119 // or the beginning if 'start' is -1.
1120 long wxListCtrl::FindItem(long start
, long data
)
1122 LV_FINDINFO findInfo
;
1124 findInfo
.flags
= LVFI_PARAM
;
1125 findInfo
.lParam
= data
;
1127 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1130 // Find an item nearest this position in the specified direction, starting from
1131 // the item after 'start' or the beginning if 'start' is -1.
1132 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1134 LV_FINDINFO findInfo
;
1136 findInfo
.flags
= LVFI_NEARESTXY
;
1137 findInfo
.pt
.x
= pt
.x
;
1138 findInfo
.pt
.y
= pt
.y
;
1139 findInfo
.vkDirection
= VK_RIGHT
;
1141 if ( direction
== wxLIST_FIND_UP
)
1142 findInfo
.vkDirection
= VK_UP
;
1143 else if ( direction
== wxLIST_FIND_DOWN
)
1144 findInfo
.vkDirection
= VK_DOWN
;
1145 else if ( direction
== wxLIST_FIND_LEFT
)
1146 findInfo
.vkDirection
= VK_LEFT
;
1147 else if ( direction
== wxLIST_FIND_RIGHT
)
1148 findInfo
.vkDirection
= VK_RIGHT
;
1150 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1153 // Determines which item (if any) is at the specified point,
1154 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1155 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1157 LV_HITTESTINFO hitTestInfo
;
1158 hitTestInfo
.pt
.x
= (int) point
.x
;
1159 hitTestInfo
.pt
.y
= (int) point
.y
;
1161 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1164 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1165 flags
|= wxLIST_HITTEST_ABOVE
;
1166 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1167 flags
|= wxLIST_HITTEST_BELOW
;
1168 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1169 flags
|= wxLIST_HITTEST_NOWHERE
;
1170 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1171 flags
|= wxLIST_HITTEST_ONITEMICON
;
1172 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1173 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1174 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1175 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1176 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1177 flags
|= wxLIST_HITTEST_TOLEFT
;
1178 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1179 flags
|= wxLIST_HITTEST_TORIGHT
;
1181 return (long) hitTestInfo
.iItem
;
1184 // Inserts an item, returning the index of the new item if successful,
1186 long wxListCtrl::InsertItem(wxListItem
& info
)
1189 wxConvertToMSWListItem(this, info
, item
);
1191 // check whether it has any custom attributes
1192 if ( info
.HasAttributes() )
1195 wxListItemAttr
*attr
;
1196 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1200 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1202 else *attr
= *info
.GetAttributes();
1204 m_hasAnyAttr
= TRUE
;
1207 return (long) ListView_InsertItem(GetHwnd(), & item
);
1210 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1213 info
.m_text
= label
;
1214 info
.m_mask
= wxLIST_MASK_TEXT
;
1215 info
.m_itemId
= index
;
1216 return InsertItem(info
);
1219 // Inserts an image item
1220 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1223 info
.m_image
= imageIndex
;
1224 info
.m_mask
= wxLIST_MASK_IMAGE
;
1225 info
.m_itemId
= index
;
1226 return InsertItem(info
);
1229 // Inserts an image/string item
1230 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1233 info
.m_image
= imageIndex
;
1234 info
.m_text
= label
;
1235 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1236 info
.m_itemId
= index
;
1237 return InsertItem(info
);
1240 // For list view mode (only), inserts a column.
1241 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1246 lvCol
.pszText
= NULL
;
1248 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1250 lvCol
.mask
|= LVCF_TEXT
;
1251 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1252 lvCol
.cchTextMax
= 0; // Ignored
1254 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1256 lvCol
.mask
|= LVCF_FMT
;
1258 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1259 lvCol
.fmt
= LVCFMT_LEFT
;
1260 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1261 lvCol
.fmt
= LVCFMT_RIGHT
;
1262 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1263 lvCol
.fmt
= LVCFMT_CENTER
;
1266 lvCol
.mask
|= LVCF_WIDTH
;
1267 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1269 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1270 lvCol
.cx
= LVSCW_AUTOSIZE
;
1271 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1272 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1274 lvCol
.cx
= item
.m_width
;
1278 // always give some width to the new column: this one is compatible
1283 lvCol
.mask
|= LVCF_SUBITEM
;
1284 lvCol
.iSubItem
= col
;
1286 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1293 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1300 long wxListCtrl::InsertColumn(long col
,
1301 const wxString
& heading
,
1306 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1307 item
.m_text
= heading
;
1310 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1311 item
.m_width
= width
;
1313 item
.m_format
= format
;
1315 return InsertColumn(col
, item
);
1318 // Scrolls the list control. If in icon, small icon or report view mode,
1319 // x specifies the number of pixels to scroll. If in list view mode, x
1320 // specifies the number of columns to scroll.
1321 // If in icon, small icon or list view mode, y specifies the number of pixels
1322 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1323 bool wxListCtrl::ScrollList(int dx
, int dy
)
1325 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1330 // fn is a function which takes 3 long arguments: item1, item2, data.
1331 // item1 is the long data associated with a first item (NOT the index).
1332 // item2 is the long data associated with a second item (NOT the index).
1333 // data is the same value as passed to SortItems.
1334 // The return value is a negative number if the first item should precede the second
1335 // item, a positive number of the second item should precede the first,
1336 // or zero if the two items are equivalent.
1338 // data is arbitrary data to be passed to the sort function.
1339 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1341 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1344 // ----------------------------------------------------------------------------
1345 // message processing
1346 // ----------------------------------------------------------------------------
1348 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1350 if (cmd
== EN_UPDATE
)
1352 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1353 event
.SetEventObject( this );
1354 ProcessCommand(event
);
1357 else if (cmd
== EN_KILLFOCUS
)
1359 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1360 event
.SetEventObject( this );
1361 ProcessCommand(event
);
1368 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1370 // prepare the event
1371 // -----------------
1373 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1374 wxEventType eventType
= wxEVT_NULL
;
1376 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1378 // almost all messages use NM_LISTVIEW
1379 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1381 // this is true for almost all events
1382 event
.m_item
.m_data
= nmLV
->lParam
;
1384 switch ( nmhdr
->code
)
1386 case LVN_BEGINRDRAG
:
1387 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1391 if ( eventType
== wxEVT_NULL
)
1393 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1396 event
.m_itemIndex
= nmLV
->iItem
;
1397 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1398 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1401 case LVN_BEGINLABELEDIT
:
1403 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1404 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1405 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1409 case LVN_COLUMNCLICK
:
1410 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1411 event
.m_itemIndex
= -1;
1412 event
.m_col
= nmLV
->iSubItem
;
1415 case LVN_DELETEALLITEMS
:
1416 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1417 event
.m_itemIndex
= -1;
1423 case LVN_DELETEITEM
:
1424 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1425 event
.m_itemIndex
= nmLV
->iItem
;
1429 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1433 case LVN_ENDLABELEDIT
:
1435 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1436 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1437 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
);
1438 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1443 case LVN_SETDISPINFO
:
1445 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1446 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1447 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1451 case LVN_GETDISPINFO
:
1452 // this provokes stack overflow: indeed, wxConvertFromMSWListItem()
1453 // sends us WM_NOTIFY! As it doesn't do anything for now, just leave
1457 // TODO: some text buffering here, I think
1458 // TODO: API for getting Windows to retrieve values
1460 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1461 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1462 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1469 case LVN_INSERTITEM
:
1470 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1471 event
.m_itemIndex
= nmLV
->iItem
;
1474 case LVN_ITEMCHANGED
:
1475 // This needs to be sent to wxListCtrl as a rather more concrete
1476 // event. For now, just detect a selection or deselection.
1477 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1479 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1480 event
.m_itemIndex
= nmLV
->iItem
;
1482 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1484 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1485 event
.m_itemIndex
= nmLV
->iItem
;
1495 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1496 WORD wVKey
= info
->wVKey
;
1498 // get the current selection
1499 long lItem
= GetNextItem(-1,
1501 wxLIST_STATE_SELECTED
);
1503 // <Enter> or <Space> activate the selected item if any (but
1504 // not with Shift and/or Ctrl as then they have a predefined
1505 // meaning for the list view)
1507 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1508 !(wxIsShiftDown() || wxIsCtrlDown()) )
1510 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1511 event
.m_itemIndex
= lItem
;
1515 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1516 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1521 // fill the other fields too
1522 event
.m_item
.m_text
= GetItemText(lItem
);
1523 event
.m_item
.m_data
= GetItemData(lItem
);
1529 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1531 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1536 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1537 // if it happened on an item (and not on empty place)
1538 if ( nmLV
->iItem
== -1 )
1544 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1545 event
.m_itemIndex
= nmLV
->iItem
;
1546 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1547 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1551 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1552 subclass and check for the actual WM_RBUTTONDOWN message,
1553 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1554 before firing off. We want to have notify events for both down
1557 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1558 // don't do anything else
1559 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1564 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1565 LV_HITTESTINFO lvhti
;
1566 wxZeroMemory(lvhti
);
1568 ::GetCursorPos(&(lvhti
.pt
));
1569 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1570 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1572 if ( lvhti
.flags
& LVHT_ONITEM
)
1574 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1575 event
.m_itemIndex
= lvhti
.iItem
;
1576 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1577 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1584 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1586 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1588 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1593 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1594 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1595 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1596 event
.m_itemIndex
= hdr
->iItem
;
1601 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1604 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1605 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1606 switch( nmcd
.dwDrawStage
)
1609 // if we've got any items with non standard attributes,
1610 // notify us before painting each item
1611 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1615 case CDDS_ITEMPREPAINT
:
1617 wxListItemAttr
*attr
=
1618 (wxListItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1622 // nothing to do for this item
1623 return CDRF_DODEFAULT
;
1627 wxColour colText
, colBack
;
1628 if ( attr
->HasFont() )
1630 wxFont font
= attr
->GetFont();
1631 hFont
= (HFONT
)font
.GetResourceHandle();
1638 if ( attr
->HasTextColour() )
1640 colText
= attr
->GetTextColour();
1644 colText
= GetTextColour();
1647 if ( attr
->HasBackgroundColour() )
1649 colBack
= attr
->GetBackgroundColour();
1653 colBack
= GetBackgroundColour();
1656 // note that if we wanted to set colours for
1657 // individual columns (subitems), we would have
1658 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1661 ::SelectObject(nmcd
.hdc
, hFont
);
1663 *result
= CDRF_NEWFONT
;
1667 *result
= CDRF_DODEFAULT
;
1670 lplvcd
->clrText
= wxColourToRGB(colText
);
1671 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1677 *result
= CDRF_DODEFAULT
;
1681 // break; // can never be reached
1682 #endif // _WIN32_IE >= 0x300
1685 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1688 // process the event
1689 // -----------------
1691 event
.SetEventObject( this );
1692 event
.SetEventType(eventType
);
1694 if ( !GetEventHandler()->ProcessEvent(event
) )
1700 switch ( nmhdr
->code
)
1702 case LVN_DELETEALLITEMS
:
1703 // always return TRUE to suppress all additional LVN_DELETEITEM
1704 // notifications - this makes deleting all items from a list ctrl
1710 case LVN_GETDISPINFO
:
1712 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1713 if ( info
->item
.mask
& LVIF_TEXT
)
1715 if ( !event
.m_item
.m_text
.IsNull() )
1717 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1718 info
->item
.cchTextMax
= wxStrlen(info
->item
.pszText
) + 1;
1721 // wxConvertToMSWListItem(this, event.m_item, info->item);
1724 case LVN_ENDLABELEDIT
:
1726 *result
= event
.IsAllowed();
1731 *result
= !event
.IsAllowed();
1736 wxChar
*wxListCtrl::AddPool(const wxString
& str
)
1738 // Remove the first element if 3 strings exist
1739 if ( m_stringPool
.Number() == 3 )
1741 wxNode
*node
= m_stringPool
.First();
1742 delete[] (char *)node
->Data();
1745 wxNode
*node
= m_stringPool
.Add(WXSTRINGCAST str
);
1746 return (wxChar
*)node
->Data();
1749 // Necessary for drawing hrules and vrules, if specified
1750 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1754 wxControl::OnPaint(event
);
1756 // Reset the device origin since it may have been set
1757 dc
.SetDeviceOrigin(0, 0);
1759 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1760 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1762 if (!drawHRules
&& !drawVRules
)
1764 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1767 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1769 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1771 wxSize clientSize
= GetClientSize();
1775 int itemCount
= GetItemCount();
1777 for (i
= 0; i
< itemCount
; i
++)
1779 if (GetItemRect(i
, itemRect
))
1781 cy
= itemRect
.GetTop();
1782 if (i
!= 0) // Don't draw the first one
1784 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1787 if (i
== (GetItemCount() - 1))
1789 cy
= itemRect
.GetBottom();
1790 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1794 i
= (GetItemCount() - 1);
1795 if (drawVRules
&& (i
> -1))
1797 wxRect firstItemRect
;
1798 GetItemRect(0, firstItemRect
);
1800 if (GetItemRect(i
, itemRect
))
1803 int x
= itemRect
.GetX();
1804 for (col
= 0; col
< GetColumnCount(); col
++)
1806 int colWidth
= GetColumnWidth(col
);
1808 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1814 // ----------------------------------------------------------------------------
1816 // ----------------------------------------------------------------------------
1818 // List item structure
1819 wxListItem::wxListItem()
1829 m_format
= wxLIST_FORMAT_CENTRE
;
1835 void wxListItem::Clear()
1844 m_format
= wxLIST_FORMAT_CENTRE
;
1846 m_text
= wxEmptyString
;
1848 if (m_attr
) delete m_attr
;
1852 void wxListItem::ClearAttributes()
1854 if (m_attr
) delete m_attr
;
1858 static void wxConvertFromMSWListItem(const wxListCtrl
*WXUNUSED(ctrl
), wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1860 info
.m_data
= lvItem
.lParam
;
1863 info
.m_stateMask
= 0;
1864 info
.m_itemId
= lvItem
.iItem
;
1866 long oldMask
= lvItem
.mask
;
1868 bool needText
= FALSE
;
1869 if (getFullInfo
!= 0)
1871 if ( lvItem
.mask
& LVIF_TEXT
)
1878 lvItem
.pszText
= new wxChar
[513];
1879 lvItem
.cchTextMax
= 512;
1881 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1882 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1883 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1886 if ( lvItem
.mask
& LVIF_STATE
)
1888 info
.m_mask
|= wxLIST_MASK_STATE
;
1890 if ( lvItem
.stateMask
& LVIS_CUT
)
1892 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1893 if ( lvItem
.state
& LVIS_CUT
)
1894 info
.m_state
|= wxLIST_STATE_CUT
;
1896 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1898 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1899 if ( lvItem
.state
& LVIS_DROPHILITED
)
1900 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1902 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1904 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1905 if ( lvItem
.state
& LVIS_FOCUSED
)
1906 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1908 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1910 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1911 if ( lvItem
.state
& LVIS_SELECTED
)
1912 info
.m_state
|= wxLIST_STATE_SELECTED
;
1916 if ( lvItem
.mask
& LVIF_TEXT
)
1918 info
.m_mask
|= wxLIST_MASK_TEXT
;
1919 info
.m_text
= lvItem
.pszText
;
1921 if ( lvItem
.mask
& LVIF_IMAGE
)
1923 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1924 info
.m_image
= lvItem
.iImage
;
1926 if ( lvItem
.mask
& LVIF_PARAM
)
1927 info
.m_mask
|= wxLIST_MASK_DATA
;
1928 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1929 info
.m_mask
|= wxLIST_SET_ITEM
;
1930 info
.m_col
= lvItem
.iSubItem
;
1935 delete[] lvItem
.pszText
;
1937 lvItem
.mask
= oldMask
;
1940 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1942 lvItem
.iItem
= (int) info
.m_itemId
;
1944 lvItem
.iImage
= info
.m_image
;
1945 lvItem
.lParam
= info
.m_data
;
1946 lvItem
.stateMask
= 0;
1949 lvItem
.iSubItem
= info
.m_col
;
1951 if (info
.m_mask
& wxLIST_MASK_STATE
)
1953 lvItem
.mask
|= LVIF_STATE
;
1954 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1956 lvItem
.stateMask
|= LVIS_CUT
;
1957 if (info
.m_state
& wxLIST_STATE_CUT
)
1958 lvItem
.state
|= LVIS_CUT
;
1960 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1962 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1963 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1964 lvItem
.state
|= LVIS_DROPHILITED
;
1966 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1968 lvItem
.stateMask
|= LVIS_FOCUSED
;
1969 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1970 lvItem
.state
|= LVIS_FOCUSED
;
1972 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1974 lvItem
.stateMask
|= LVIS_SELECTED
;
1975 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1976 lvItem
.state
|= LVIS_SELECTED
;
1980 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1982 lvItem
.mask
|= LVIF_TEXT
;
1983 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1985 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1989 lvItem
.pszText
= WXSTRINGCAST info
.m_text
;
1990 if ( lvItem
.pszText
)
1991 lvItem
.cchTextMax
= info
.m_text
.Length();
1993 lvItem
.cchTextMax
= 0;
1996 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1997 lvItem
.mask
|= LVIF_IMAGE
;
1998 if (info
.m_mask
& wxLIST_MASK_DATA
)
1999 lvItem
.mask
|= LVIF_PARAM
;
2002 // ----------------------------------------------------------------------------
2004 // ----------------------------------------------------------------------------
2006 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2008 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2009 : wxNotifyEvent(commandType
, id
)
2015 m_cancelled
= FALSE
;