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 #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
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
;
194 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
195 wstyle
|= WS_CLIPSIBLINGS
;
197 if ( wxStyleHasBorder(m_windowStyle
) )
199 m_baseStyle
= wstyle
;
201 if ( !DoCreateControl(x
, y
, width
, height
) )
205 parent
->AddChild(this);
210 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
212 DWORD wstyle
= m_baseStyle
;
215 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
217 // Even with extended styles, need to combine with WS_BORDER
218 // for them to look right.
222 long oldStyle
= 0; // Dummy
223 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
225 // Create the ListView control.
226 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
231 GetWinHwnd(GetParent()),
238 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
243 // for comctl32.dll v 4.70+ we want to have this attribute because it's
244 // prettier (and also because wxGTK does it like this)
245 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
247 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
248 0, LVS_EX_FULLROWSELECT
);
251 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
252 SetForegroundColour(GetParent()->GetForegroundColour());
259 void wxListCtrl::UpdateStyle()
263 // The new window view style
265 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
266 dwStyleNew
|= m_baseStyle
;
268 // Get the current window style.
269 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
271 // Only set the window style if the view bits have changed.
272 if ( dwStyleOld
!= dwStyleNew
)
274 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
279 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
283 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
285 delete (wxListItemAttr
*)node
->Data();
291 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
294 m_hasAnyAttr
= FALSE
;
298 wxListCtrl::~wxListCtrl()
300 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
304 m_textCtrl
->UnsubclassWin();
305 m_textCtrl
->SetHWND(0);
310 if (m_ownsImageListNormal
) delete m_imageListNormal
;
311 if (m_ownsImageListSmall
) delete m_imageListSmall
;
312 if (m_ownsImageListState
) delete m_imageListState
;
315 // ----------------------------------------------------------------------------
316 // set/get/change style
317 // ----------------------------------------------------------------------------
319 // Add or remove a single window style
320 void wxListCtrl::SetSingleStyle(long style
, bool add
)
322 long flag
= GetWindowStyleFlag();
324 // Get rid of conflicting styles
327 if ( style
& wxLC_MASK_TYPE
)
328 flag
= flag
& ~wxLC_MASK_TYPE
;
329 if ( style
& wxLC_MASK_ALIGN
)
330 flag
= flag
& ~wxLC_MASK_ALIGN
;
331 if ( style
& wxLC_MASK_SORT
)
332 flag
= flag
& ~wxLC_MASK_SORT
;
348 m_windowStyle
= flag
;
353 // Set the whole window style
354 void wxListCtrl::SetWindowStyleFlag(long flag
)
356 m_windowStyle
= flag
;
361 // Can be just a single style, or a bitlist
362 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
365 if ( style
& wxLC_ICON
)
367 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
368 oldStyle
-= LVS_SMALLICON
;
369 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
370 oldStyle
-= LVS_REPORT
;
371 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
372 oldStyle
-= LVS_LIST
;
376 if ( style
& wxLC_SMALL_ICON
)
378 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
379 oldStyle
-= LVS_ICON
;
380 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
381 oldStyle
-= LVS_REPORT
;
382 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
383 oldStyle
-= LVS_LIST
;
384 wstyle
|= LVS_SMALLICON
;
387 if ( style
& wxLC_LIST
)
389 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
390 oldStyle
-= LVS_ICON
;
391 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
392 oldStyle
-= LVS_REPORT
;
393 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
394 oldStyle
-= LVS_SMALLICON
;
398 if ( style
& wxLC_REPORT
)
400 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
401 oldStyle
-= LVS_ICON
;
402 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
403 oldStyle
-= LVS_LIST
;
404 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
405 oldStyle
-= LVS_SMALLICON
;
407 wstyle
|= LVS_REPORT
;
410 if ( style
& wxLC_ALIGN_LEFT
)
412 if ( oldStyle
& LVS_ALIGNTOP
)
413 oldStyle
-= LVS_ALIGNTOP
;
414 wstyle
|= LVS_ALIGNLEFT
;
417 if ( style
& wxLC_ALIGN_TOP
)
419 if ( oldStyle
& LVS_ALIGNLEFT
)
420 oldStyle
-= LVS_ALIGNLEFT
;
421 wstyle
|= LVS_ALIGNTOP
;
424 if ( style
& wxLC_AUTOARRANGE
)
425 wstyle
|= LVS_AUTOARRANGE
;
427 // Apparently, no such style (documentation wrong?)
429 if ( style & wxLC_BUTTON )
430 wstyle |= LVS_BUTTON;
433 if ( style
& wxLC_NO_SORT_HEADER
)
434 wstyle
|= LVS_NOSORTHEADER
;
436 if ( style
& wxLC_NO_HEADER
)
437 wstyle
|= LVS_NOCOLUMNHEADER
;
439 if ( style
& wxLC_EDIT_LABELS
)
440 wstyle
|= LVS_EDITLABELS
;
442 if ( style
& wxLC_SINGLE_SEL
)
443 wstyle
|= LVS_SINGLESEL
;
445 if ( style
& wxLC_SORT_ASCENDING
)
447 if ( oldStyle
& LVS_SORTDESCENDING
)
448 oldStyle
-= LVS_SORTDESCENDING
;
449 wstyle
|= LVS_SORTASCENDING
;
452 if ( style
& wxLC_SORT_DESCENDING
)
454 if ( oldStyle
& LVS_SORTASCENDING
)
455 oldStyle
-= LVS_SORTASCENDING
;
456 wstyle
|= LVS_SORTDESCENDING
;
462 // ----------------------------------------------------------------------------
464 // ----------------------------------------------------------------------------
466 // Sets the foreground, i.e. text, colour
467 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
469 if ( !wxWindow::SetForegroundColour(col
) )
472 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
477 // Sets the background colour
478 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
480 if ( !wxWindow::SetBackgroundColour(col
) )
483 // we set the same colour for both the "empty" background and the items
485 COLORREF color
= wxColourToRGB(col
);
486 ListView_SetBkColor(GetHwnd(), color
);
487 ListView_SetTextBkColor(GetHwnd(), color
);
492 // Gets information about this column
493 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
498 lvCol
.pszText
= NULL
;
500 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
502 lvCol
.mask
|= LVCF_TEXT
;
503 lvCol
.pszText
= new wxChar
[513];
504 lvCol
.cchTextMax
= 512;
507 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
509 // item.m_subItem = lvCol.iSubItem;
510 item
.m_width
= lvCol
.cx
;
512 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
514 item
.m_text
= lvCol
.pszText
;
515 delete[] lvCol
.pszText
;
518 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
520 if (lvCol
.fmt
== LVCFMT_LEFT
)
521 item
.m_format
= wxLIST_FORMAT_LEFT
;
522 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
523 item
.m_format
= wxLIST_FORMAT_RIGHT
;
524 else if (lvCol
.fmt
== LVCFMT_CENTER
)
525 item
.m_format
= wxLIST_FORMAT_CENTRE
;
531 // Sets information about this column
532 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
537 lvCol
.pszText
= NULL
;
539 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
541 lvCol
.mask
|= LVCF_TEXT
;
542 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
543 lvCol
.cchTextMax
= 0; // Ignored
545 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
547 lvCol
.mask
|= LVCF_FMT
;
549 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
550 lvCol
.fmt
= LVCFMT_LEFT
;
551 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
552 lvCol
.fmt
= LVCFMT_RIGHT
;
553 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
554 lvCol
.fmt
= LVCFMT_CENTER
;
557 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
559 lvCol
.mask
|= LVCF_WIDTH
;
560 lvCol
.cx
= item
.m_width
;
562 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
563 lvCol
.cx
= LVSCW_AUTOSIZE
;
564 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
565 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
567 lvCol
.mask
|= LVCF_SUBITEM
;
568 lvCol
.iSubItem
= col
;
569 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
572 // Gets the column width
573 int wxListCtrl::GetColumnWidth(int col
) const
575 return ListView_GetColumnWidth(GetHwnd(), col
);
578 // Sets the column width
579 bool wxListCtrl::SetColumnWidth(int col
, int width
)
582 if ( m_windowStyle
& wxLC_LIST
)
586 if ( width2
== wxLIST_AUTOSIZE
)
587 width2
= LVSCW_AUTOSIZE
;
588 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
589 width2
= LVSCW_AUTOSIZE_USEHEADER
;
591 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
594 // Gets the number of items that can fit vertically in the
595 // visible area of the list control (list or report view)
596 // or the total number of items in the list control (icon
597 // or small icon view)
598 int wxListCtrl::GetCountPerPage() const
600 return ListView_GetCountPerPage(GetHwnd());
603 // Gets the edit control for editing labels.
604 wxTextCtrl
* wxListCtrl::GetEditControl() const
609 // Gets information about the item
610 bool wxListCtrl::GetItem(wxListItem
& info
) const
613 wxZeroMemory(lvItem
);
615 lvItem
.iItem
= info
.m_itemId
;
616 lvItem
.iSubItem
= info
.m_col
;
618 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
620 lvItem
.mask
|= LVIF_TEXT
;
621 lvItem
.pszText
= new wxChar
[513];
622 lvItem
.cchTextMax
= 512;
626 lvItem
.pszText
= NULL
;
629 if (info
.m_mask
& wxLIST_MASK_DATA
)
630 lvItem
.mask
|= LVIF_PARAM
;
632 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
633 lvItem
.mask
|= LVIF_IMAGE
;
635 if ( info
.m_mask
& wxLIST_MASK_STATE
)
637 lvItem
.mask
|= LVIF_STATE
;
638 // the other bits are hardly interesting anyhow
639 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
642 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
645 wxLogError(_("Couldn't retrieve information about list control item %d."),
650 wxConvertFromMSWListItem(this, info
, lvItem
);
654 delete[] lvItem
.pszText
;
659 // Sets information about the item
660 bool wxListCtrl::SetItem(wxListItem
& info
)
663 wxConvertToMSWListItem(this, info
, item
);
666 if ( !ListView_SetItem(GetHwnd(), &item
) )
668 wxLogDebug(_T("ListView_SetItem() failed"));
673 // we need to update the item immediately to show the new image
674 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
676 // check whether it has any custom attributes
677 if ( info
.HasAttributes() )
679 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
682 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
684 *attr
= *info
.GetAttributes();
688 // if the colour has changed, we must redraw the item
694 // we need this to make the change visible right now
695 ListView_Update(GetHwnd(), item
.iItem
);
701 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
705 info
.m_mask
= wxLIST_MASK_TEXT
;
706 info
.m_itemId
= index
;
710 info
.m_image
= imageId
;
711 info
.m_mask
|= wxLIST_MASK_IMAGE
;
713 return SetItem(info
);
717 // Gets the item state
718 int wxListCtrl::GetItemState(long item
, long stateMask
) const
722 info
.m_mask
= wxLIST_MASK_STATE
;
723 info
.m_stateMask
= stateMask
;
724 info
.m_itemId
= item
;
732 // Sets the item state
733 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
737 info
.m_mask
= wxLIST_MASK_STATE
;
738 info
.m_state
= state
;
739 info
.m_stateMask
= stateMask
;
740 info
.m_itemId
= item
;
742 return SetItem(info
);
745 // Sets the item image
746 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
750 info
.m_mask
= wxLIST_MASK_IMAGE
;
751 info
.m_image
= image
;
752 info
.m_itemId
= item
;
754 return SetItem(info
);
757 // Gets the item text
758 wxString
wxListCtrl::GetItemText(long item
) const
762 info
.m_mask
= wxLIST_MASK_TEXT
;
763 info
.m_itemId
= item
;
770 // Sets the item text
771 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
775 info
.m_mask
= wxLIST_MASK_TEXT
;
776 info
.m_itemId
= item
;
782 // Gets the item data
783 long wxListCtrl::GetItemData(long item
) const
787 info
.m_mask
= wxLIST_MASK_DATA
;
788 info
.m_itemId
= item
;
795 // Sets the item data
796 bool wxListCtrl::SetItemData(long item
, long data
)
800 info
.m_mask
= wxLIST_MASK_DATA
;
801 info
.m_itemId
= item
;
804 return SetItem(info
);
807 // Gets the item rectangle
808 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
812 int code2
= LVIR_BOUNDS
;
813 if ( code
== wxLIST_RECT_BOUNDS
)
815 else if ( code
== wxLIST_RECT_ICON
)
817 else if ( code
== wxLIST_RECT_LABEL
)
821 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
823 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
828 rect
.width
= rect2
.right
- rect2
.left
;
829 rect
.height
= rect2
.bottom
- rect2
.top
;
833 // Gets the item position
834 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
838 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
840 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
844 // Sets the item position.
845 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
847 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
850 // Gets the number of items in the list control
851 int wxListCtrl::GetItemCount() const
853 return ListView_GetItemCount(GetHwnd());
856 // Retrieves the spacing between icons in pixels.
857 // If small is TRUE, gets the spacing for the small icon
858 // view, otherwise the large icon view.
859 int wxListCtrl::GetItemSpacing(bool isSmall
) const
861 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
864 // Gets the number of selected items in the list control
865 int wxListCtrl::GetSelectedItemCount() const
867 return ListView_GetSelectedCount(GetHwnd());
870 // Gets the text colour of the listview
871 wxColour
wxListCtrl::GetTextColour() const
873 COLORREF ref
= ListView_GetTextColor(GetHwnd());
874 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
878 // Sets the text colour of the listview
879 void wxListCtrl::SetTextColour(const wxColour
& col
)
881 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
884 // Gets the index of the topmost visible item when in
885 // list or report view
886 long wxListCtrl::GetTopItem() const
888 return (long) ListView_GetTopIndex(GetHwnd());
891 // Searches for an item, starting from 'item'.
892 // 'geometry' is one of
893 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
894 // 'state' is a state bit flag, one or more of
895 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
896 // item can be -1 to find the first item that matches the
898 // Returns the item or -1 if unsuccessful.
899 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
903 if ( geom
== wxLIST_NEXT_ABOVE
)
905 if ( geom
== wxLIST_NEXT_ALL
)
907 if ( geom
== wxLIST_NEXT_BELOW
)
909 if ( geom
== wxLIST_NEXT_LEFT
)
910 flags
|= LVNI_TOLEFT
;
911 if ( geom
== wxLIST_NEXT_RIGHT
)
912 flags
|= LVNI_TORIGHT
;
914 if ( state
& wxLIST_STATE_CUT
)
916 if ( state
& wxLIST_STATE_DROPHILITED
)
917 flags
|= LVNI_DROPHILITED
;
918 if ( state
& wxLIST_STATE_FOCUSED
)
919 flags
|= LVNI_FOCUSED
;
920 if ( state
& wxLIST_STATE_SELECTED
)
921 flags
|= LVNI_SELECTED
;
923 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
927 wxImageList
*wxListCtrl::GetImageList(int which
) const
929 if ( which
== wxIMAGE_LIST_NORMAL
)
931 return m_imageListNormal
;
933 else if ( which
== wxIMAGE_LIST_SMALL
)
935 return m_imageListSmall
;
937 else if ( which
== wxIMAGE_LIST_STATE
)
939 return m_imageListState
;
944 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
947 if ( which
== wxIMAGE_LIST_NORMAL
)
949 flags
= LVSIL_NORMAL
;
950 if (m_ownsImageListNormal
) delete m_imageListNormal
;
951 m_imageListNormal
= imageList
;
952 m_ownsImageListNormal
= FALSE
;
954 else if ( which
== wxIMAGE_LIST_SMALL
)
957 if (m_ownsImageListSmall
) delete m_imageListSmall
;
958 m_imageListSmall
= imageList
;
959 m_ownsImageListSmall
= FALSE
;
961 else if ( which
== wxIMAGE_LIST_STATE
)
964 if (m_ownsImageListState
) delete m_imageListState
;
965 m_imageListState
= imageList
;
966 m_ownsImageListState
= FALSE
;
968 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
971 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
973 SetImageList(imageList
, which
);
974 if ( which
== wxIMAGE_LIST_NORMAL
)
975 m_ownsImageListNormal
= TRUE
;
976 else if ( which
== wxIMAGE_LIST_SMALL
)
977 m_ownsImageListSmall
= TRUE
;
978 else if ( which
== wxIMAGE_LIST_STATE
)
979 m_ownsImageListState
= TRUE
;
982 // ----------------------------------------------------------------------------
984 // ----------------------------------------------------------------------------
986 // Arranges the items
987 bool wxListCtrl::Arrange(int flag
)
990 if ( flag
== wxLIST_ALIGN_LEFT
)
991 code
= LVA_ALIGNLEFT
;
992 else if ( flag
== wxLIST_ALIGN_TOP
)
994 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
996 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
997 code
= LVA_SNAPTOGRID
;
999 return (ListView_Arrange(GetHwnd(), code
) != 0);
1003 bool wxListCtrl::DeleteItem(long item
)
1005 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1008 // Deletes all items
1009 bool wxListCtrl::DeleteAllItems()
1011 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1014 // Deletes all items
1015 bool wxListCtrl::DeleteAllColumns()
1017 while ( m_colCount
> 0 )
1019 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1021 wxLogLastError(wxT("ListView_DeleteColumn"));
1029 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1035 bool wxListCtrl::DeleteColumn(int col
)
1037 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1039 if ( success
&& (m_colCount
> 0) )
1044 // Clears items, and columns if there are any.
1045 void wxListCtrl::ClearAll()
1048 if ( m_colCount
> 0 )
1052 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1054 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1056 // VS: ListView_EditLabel requires that the list has focus.
1058 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1062 m_textCtrl
->UnsubclassWin();
1063 m_textCtrl
->SetHWND(0);
1068 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1069 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1070 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1075 // End label editing, optionally cancelling the edit
1076 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1080 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
1082 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
1086 m_textCtrl->UnsubclassWin();
1087 m_textCtrl->SetHWND(0);
1097 // Ensures this item is visible
1098 bool wxListCtrl::EnsureVisible(long item
)
1100 return (ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0);
1103 // Find an item whose label matches this string, starting from the item after 'start'
1104 // or the beginning if 'start' is -1.
1105 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1107 LV_FINDINFO findInfo
;
1109 findInfo
.flags
= LVFI_STRING
;
1111 findInfo
.flags
|= LVFI_PARTIAL
;
1114 // ListView_FindItem() excludes the first item from search and to look
1115 // through all the items you need to start from -1 which is unnatural and
1116 // inconsistent with the generic version - so we adjust the index
1119 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1122 // Find an item whose data matches this data, starting from the item after 'start'
1123 // or the beginning if 'start' is -1.
1124 long wxListCtrl::FindItem(long start
, long data
)
1126 LV_FINDINFO findInfo
;
1128 findInfo
.flags
= LVFI_PARAM
;
1129 findInfo
.lParam
= data
;
1131 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1134 // Find an item nearest this position in the specified direction, starting from
1135 // the item after 'start' or the beginning if 'start' is -1.
1136 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1138 LV_FINDINFO findInfo
;
1140 findInfo
.flags
= LVFI_NEARESTXY
;
1141 findInfo
.pt
.x
= pt
.x
;
1142 findInfo
.pt
.y
= pt
.y
;
1143 findInfo
.vkDirection
= VK_RIGHT
;
1145 if ( direction
== wxLIST_FIND_UP
)
1146 findInfo
.vkDirection
= VK_UP
;
1147 else if ( direction
== wxLIST_FIND_DOWN
)
1148 findInfo
.vkDirection
= VK_DOWN
;
1149 else if ( direction
== wxLIST_FIND_LEFT
)
1150 findInfo
.vkDirection
= VK_LEFT
;
1151 else if ( direction
== wxLIST_FIND_RIGHT
)
1152 findInfo
.vkDirection
= VK_RIGHT
;
1154 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1157 // Determines which item (if any) is at the specified point,
1158 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1159 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1161 LV_HITTESTINFO hitTestInfo
;
1162 hitTestInfo
.pt
.x
= (int) point
.x
;
1163 hitTestInfo
.pt
.y
= (int) point
.y
;
1165 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1168 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1169 flags
|= wxLIST_HITTEST_ABOVE
;
1170 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1171 flags
|= wxLIST_HITTEST_BELOW
;
1172 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1173 flags
|= wxLIST_HITTEST_NOWHERE
;
1174 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1175 flags
|= wxLIST_HITTEST_ONITEMICON
;
1176 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1177 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1178 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1179 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1180 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1181 flags
|= wxLIST_HITTEST_TOLEFT
;
1182 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1183 flags
|= wxLIST_HITTEST_TORIGHT
;
1185 return (long) hitTestInfo
.iItem
;
1188 // Inserts an item, returning the index of the new item if successful,
1190 long wxListCtrl::InsertItem(wxListItem
& info
)
1193 wxConvertToMSWListItem(this, info
, item
);
1195 // check whether it has any custom attributes
1196 if ( info
.HasAttributes() )
1199 wxListItemAttr
*attr
;
1200 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1204 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1206 else *attr
= *info
.GetAttributes();
1208 m_hasAnyAttr
= TRUE
;
1211 return (long) ListView_InsertItem(GetHwnd(), & item
);
1214 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1217 info
.m_text
= label
;
1218 info
.m_mask
= wxLIST_MASK_TEXT
;
1219 info
.m_itemId
= index
;
1220 return InsertItem(info
);
1223 // Inserts an image item
1224 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1227 info
.m_image
= imageIndex
;
1228 info
.m_mask
= wxLIST_MASK_IMAGE
;
1229 info
.m_itemId
= index
;
1230 return InsertItem(info
);
1233 // Inserts an image/string item
1234 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1237 info
.m_image
= imageIndex
;
1238 info
.m_text
= label
;
1239 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1240 info
.m_itemId
= index
;
1241 return InsertItem(info
);
1244 // For list view mode (only), inserts a column.
1245 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1250 lvCol
.pszText
= NULL
;
1252 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1254 lvCol
.mask
|= LVCF_TEXT
;
1255 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1256 lvCol
.cchTextMax
= 0; // Ignored
1258 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1260 lvCol
.mask
|= LVCF_FMT
;
1262 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1263 lvCol
.fmt
= LVCFMT_LEFT
;
1264 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1265 lvCol
.fmt
= LVCFMT_RIGHT
;
1266 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1267 lvCol
.fmt
= LVCFMT_CENTER
;
1270 lvCol
.mask
|= LVCF_WIDTH
;
1271 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1273 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1274 lvCol
.cx
= LVSCW_AUTOSIZE
;
1275 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1276 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1278 lvCol
.cx
= item
.m_width
;
1282 // always give some width to the new column: this one is compatible
1287 lvCol
.mask
|= LVCF_SUBITEM
;
1288 lvCol
.iSubItem
= col
;
1290 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1297 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1304 long wxListCtrl::InsertColumn(long col
,
1305 const wxString
& heading
,
1310 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1311 item
.m_text
= heading
;
1314 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1315 item
.m_width
= width
;
1317 item
.m_format
= format
;
1319 return InsertColumn(col
, item
);
1322 // Scrolls the list control. If in icon, small icon or report view mode,
1323 // x specifies the number of pixels to scroll. If in list view mode, x
1324 // specifies the number of columns to scroll.
1325 // If in icon, small icon or list view mode, y specifies the number of pixels
1326 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1327 bool wxListCtrl::ScrollList(int dx
, int dy
)
1329 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1334 // fn is a function which takes 3 long arguments: item1, item2, data.
1335 // item1 is the long data associated with a first item (NOT the index).
1336 // item2 is the long data associated with a second item (NOT the index).
1337 // data is the same value as passed to SortItems.
1338 // The return value is a negative number if the first item should precede the second
1339 // item, a positive number of the second item should precede the first,
1340 // or zero if the two items are equivalent.
1342 // data is arbitrary data to be passed to the sort function.
1343 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1345 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1348 // ----------------------------------------------------------------------------
1349 // message processing
1350 // ----------------------------------------------------------------------------
1352 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1354 if (cmd
== EN_UPDATE
)
1356 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1357 event
.SetEventObject( this );
1358 ProcessCommand(event
);
1361 else if (cmd
== EN_KILLFOCUS
)
1363 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1364 event
.SetEventObject( this );
1365 ProcessCommand(event
);
1372 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1374 // prepare the event
1375 // -----------------
1377 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1378 wxEventType eventType
= wxEVT_NULL
;
1380 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1382 // almost all messages use NM_LISTVIEW
1383 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1385 // this is true for almost all events
1386 event
.m_item
.m_data
= nmLV
->lParam
;
1388 switch ( nmhdr
->code
)
1390 case LVN_BEGINRDRAG
:
1391 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1395 if ( eventType
== wxEVT_NULL
)
1397 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1400 event
.m_itemIndex
= nmLV
->iItem
;
1401 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1402 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1405 case LVN_BEGINLABELEDIT
:
1407 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1408 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1409 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1413 case LVN_COLUMNCLICK
:
1414 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1415 event
.m_itemIndex
= -1;
1416 event
.m_col
= nmLV
->iSubItem
;
1419 case LVN_DELETEALLITEMS
:
1420 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1421 event
.m_itemIndex
= -1;
1427 case LVN_DELETEITEM
:
1428 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1429 event
.m_itemIndex
= nmLV
->iItem
;
1433 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1437 case LVN_ENDLABELEDIT
:
1439 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1440 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1441 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
);
1442 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1447 case LVN_SETDISPINFO
:
1449 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1450 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1451 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1455 case LVN_GETDISPINFO
:
1456 // this provokes stack overflow: indeed, wxConvertFromMSWListItem()
1457 // sends us WM_NOTIFY! As it doesn't do anything for now, just leave
1461 // TODO: some text buffering here, I think
1462 // TODO: API for getting Windows to retrieve values
1464 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1465 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1466 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1473 case LVN_INSERTITEM
:
1474 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1475 event
.m_itemIndex
= nmLV
->iItem
;
1478 case LVN_ITEMCHANGED
:
1479 // This needs to be sent to wxListCtrl as a rather more concrete
1480 // event. For now, just detect a selection or deselection.
1481 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1483 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1484 event
.m_itemIndex
= nmLV
->iItem
;
1486 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1488 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1489 event
.m_itemIndex
= nmLV
->iItem
;
1499 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1500 WORD wVKey
= info
->wVKey
;
1502 // get the current selection
1503 long lItem
= GetNextItem(-1,
1505 wxLIST_STATE_SELECTED
);
1507 // <Enter> or <Space> activate the selected item if any (but
1508 // not with Shift and/or Ctrl as then they have a predefined
1509 // meaning for the list view)
1511 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1512 !(wxIsShiftDown() || wxIsCtrlDown()) )
1514 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1515 event
.m_itemIndex
= lItem
;
1519 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1520 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1525 // fill the other fields too
1526 event
.m_item
.m_text
= GetItemText(lItem
);
1527 event
.m_item
.m_data
= GetItemData(lItem
);
1533 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1535 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1540 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1541 // if it happened on an item (and not on empty place)
1542 if ( nmLV
->iItem
== -1 )
1548 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1549 event
.m_itemIndex
= nmLV
->iItem
;
1550 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1551 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1555 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1556 subclass and check for the actual WM_RBUTTONDOWN message,
1557 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1558 before firing off. We want to have notify events for both down
1561 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1562 // don't do anything else
1563 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1568 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1569 LV_HITTESTINFO lvhti
;
1570 wxZeroMemory(lvhti
);
1572 ::GetCursorPos(&(lvhti
.pt
));
1573 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1574 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1576 if ( lvhti
.flags
& LVHT_ONITEM
)
1578 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1579 event
.m_itemIndex
= lvhti
.iItem
;
1580 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1581 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1588 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1590 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1592 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1597 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1598 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1599 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1600 event
.m_itemIndex
= hdr
->iItem
;
1605 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1608 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1609 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1610 switch( nmcd
.dwDrawStage
)
1613 // if we've got any items with non standard attributes,
1614 // notify us before painting each item
1615 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1619 case CDDS_ITEMPREPAINT
:
1621 wxListItemAttr
*attr
=
1622 (wxListItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1626 // nothing to do for this item
1627 return CDRF_DODEFAULT
;
1631 wxColour colText
, colBack
;
1632 if ( attr
->HasFont() )
1634 wxFont font
= attr
->GetFont();
1635 hFont
= (HFONT
)font
.GetResourceHandle();
1642 if ( attr
->HasTextColour() )
1644 colText
= attr
->GetTextColour();
1648 colText
= GetTextColour();
1651 if ( attr
->HasBackgroundColour() )
1653 colBack
= attr
->GetBackgroundColour();
1657 colBack
= GetBackgroundColour();
1660 // note that if we wanted to set colours for
1661 // individual columns (subitems), we would have
1662 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1665 ::SelectObject(nmcd
.hdc
, hFont
);
1667 *result
= CDRF_NEWFONT
;
1671 *result
= CDRF_DODEFAULT
;
1674 lplvcd
->clrText
= wxColourToRGB(colText
);
1675 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1681 *result
= CDRF_DODEFAULT
;
1685 // break; // can never be reached
1686 #endif // _WIN32_IE >= 0x300
1689 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1692 // process the event
1693 // -----------------
1695 event
.SetEventObject( this );
1696 event
.SetEventType(eventType
);
1698 if ( !GetEventHandler()->ProcessEvent(event
) )
1704 switch ( nmhdr
->code
)
1706 case LVN_DELETEALLITEMS
:
1707 // always return TRUE to suppress all additional LVN_DELETEITEM
1708 // notifications - this makes deleting all items from a list ctrl
1714 case LVN_GETDISPINFO
:
1716 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1717 if ( info
->item
.mask
& LVIF_TEXT
)
1719 if ( !event
.m_item
.m_text
.IsNull() )
1721 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1722 info
->item
.cchTextMax
= wxStrlen(info
->item
.pszText
) + 1;
1725 // wxConvertToMSWListItem(this, event.m_item, info->item);
1728 case LVN_ENDLABELEDIT
:
1730 *result
= event
.IsAllowed();
1735 *result
= !event
.IsAllowed();
1740 wxChar
*wxListCtrl::AddPool(const wxString
& str
)
1742 // Remove the first element if 3 strings exist
1743 if ( m_stringPool
.Number() == 3 )
1745 wxNode
*node
= m_stringPool
.First();
1746 delete[] (char *)node
->Data();
1749 wxNode
*node
= m_stringPool
.Add(WXSTRINGCAST str
);
1750 return (wxChar
*)node
->Data();
1753 // Necessary for drawing hrules and vrules, if specified
1754 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1758 wxControl::OnPaint(event
);
1760 // Reset the device origin since it may have been set
1761 dc
.SetDeviceOrigin(0, 0);
1763 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1764 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1766 if (!drawHRules
&& !drawVRules
)
1768 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1771 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1773 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1775 wxSize clientSize
= GetClientSize();
1779 int itemCount
= GetItemCount();
1781 for (i
= 0; i
< itemCount
; i
++)
1783 if (GetItemRect(i
, itemRect
))
1785 cy
= itemRect
.GetTop();
1786 if (i
!= 0) // Don't draw the first one
1788 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1791 if (i
== (GetItemCount() - 1))
1793 cy
= itemRect
.GetBottom();
1794 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1798 i
= (GetItemCount() - 1);
1799 if (drawVRules
&& (i
> -1))
1801 wxRect firstItemRect
;
1802 GetItemRect(0, firstItemRect
);
1804 if (GetItemRect(i
, itemRect
))
1807 int x
= itemRect
.GetX();
1808 for (col
= 0; col
< GetColumnCount(); col
++)
1810 int colWidth
= GetColumnWidth(col
);
1812 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1818 // ----------------------------------------------------------------------------
1820 // ----------------------------------------------------------------------------
1822 // List item structure
1823 wxListItem::wxListItem()
1833 m_format
= wxLIST_FORMAT_CENTRE
;
1839 void wxListItem::Clear()
1848 m_format
= wxLIST_FORMAT_CENTRE
;
1850 m_text
= wxEmptyString
;
1852 if (m_attr
) delete m_attr
;
1856 void wxListItem::ClearAttributes()
1858 if (m_attr
) delete m_attr
;
1862 static void wxConvertFromMSWListItem(const wxListCtrl
*WXUNUSED(ctrl
), wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1864 info
.m_data
= lvItem
.lParam
;
1867 info
.m_stateMask
= 0;
1868 info
.m_itemId
= lvItem
.iItem
;
1870 long oldMask
= lvItem
.mask
;
1872 bool needText
= FALSE
;
1873 if (getFullInfo
!= 0)
1875 if ( lvItem
.mask
& LVIF_TEXT
)
1882 lvItem
.pszText
= new wxChar
[513];
1883 lvItem
.cchTextMax
= 512;
1885 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1886 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1887 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1890 if ( lvItem
.mask
& LVIF_STATE
)
1892 info
.m_mask
|= wxLIST_MASK_STATE
;
1894 if ( lvItem
.stateMask
& LVIS_CUT
)
1896 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1897 if ( lvItem
.state
& LVIS_CUT
)
1898 info
.m_state
|= wxLIST_STATE_CUT
;
1900 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1902 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1903 if ( lvItem
.state
& LVIS_DROPHILITED
)
1904 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1906 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1908 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1909 if ( lvItem
.state
& LVIS_FOCUSED
)
1910 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1912 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1914 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1915 if ( lvItem
.state
& LVIS_SELECTED
)
1916 info
.m_state
|= wxLIST_STATE_SELECTED
;
1920 if ( lvItem
.mask
& LVIF_TEXT
)
1922 info
.m_mask
|= wxLIST_MASK_TEXT
;
1923 info
.m_text
= lvItem
.pszText
;
1925 if ( lvItem
.mask
& LVIF_IMAGE
)
1927 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1928 info
.m_image
= lvItem
.iImage
;
1930 if ( lvItem
.mask
& LVIF_PARAM
)
1931 info
.m_mask
|= wxLIST_MASK_DATA
;
1932 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1933 info
.m_mask
|= wxLIST_SET_ITEM
;
1934 info
.m_col
= lvItem
.iSubItem
;
1939 delete[] lvItem
.pszText
;
1941 lvItem
.mask
= oldMask
;
1944 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1946 lvItem
.iItem
= (int) info
.m_itemId
;
1948 lvItem
.iImage
= info
.m_image
;
1949 lvItem
.lParam
= info
.m_data
;
1950 lvItem
.stateMask
= 0;
1953 lvItem
.iSubItem
= info
.m_col
;
1955 if (info
.m_mask
& wxLIST_MASK_STATE
)
1957 lvItem
.mask
|= LVIF_STATE
;
1958 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1960 lvItem
.stateMask
|= LVIS_CUT
;
1961 if (info
.m_state
& wxLIST_STATE_CUT
)
1962 lvItem
.state
|= LVIS_CUT
;
1964 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1966 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1967 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1968 lvItem
.state
|= LVIS_DROPHILITED
;
1970 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1972 lvItem
.stateMask
|= LVIS_FOCUSED
;
1973 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1974 lvItem
.state
|= LVIS_FOCUSED
;
1976 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1978 lvItem
.stateMask
|= LVIS_SELECTED
;
1979 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1980 lvItem
.state
|= LVIS_SELECTED
;
1984 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1986 lvItem
.mask
|= LVIF_TEXT
;
1987 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1989 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1993 lvItem
.pszText
= info
.m_text
;
1994 if ( lvItem
.pszText
)
1995 lvItem
.cchTextMax
= info
.m_text
.Length();
1997 lvItem
.cchTextMax
= 0;
2000 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2001 lvItem
.mask
|= LVIF_IMAGE
;
2002 if (info
.m_mask
& wxLIST_MASK_DATA
)
2003 lvItem
.mask
|= LVIF_PARAM
;
2006 // ----------------------------------------------------------------------------
2008 // ----------------------------------------------------------------------------
2010 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2012 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2013 : wxNotifyEvent(commandType
, id
)
2019 m_cancelled
= FALSE
;