1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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(__CYGWIN10__)
49 #include "wx/msw/gnuwin32/extra.h"
54 #include "wx/msw/missing.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // convert our state and mask flags to LV_ITEM constants
61 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
63 // convert wxListItem to LV_ITEM
64 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
65 const wxListItem
& info
, LV_ITEM
& lvItem
);
67 // convert LV_ITEM to wxListItem
68 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
70 /* const */ LV_ITEM
& lvItem
);
72 // convert our wxListItem to LV_COLUMN
73 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
76 // ----------------------------------------------------------------------------
77 // private helper classes
78 // ----------------------------------------------------------------------------
80 // We have to handle both fooW and fooA notifications in several cases
81 // because of broken commctl.dll and/or unicows.dll. This class is used to
82 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
83 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
84 // by wxConvertToMSWListItem().
88 ~wxLV_ITEM() { delete m_buf
; }
89 operator LV_ITEM
&() const { return *m_item
; }
92 wxLV_ITEM(LV_ITEMW
&item
) : m_buf(NULL
), m_item(&item
) {}
93 wxLV_ITEM(LV_ITEMA
&item
)
95 m_item
= new LV_ITEM((LV_ITEM
&)item
);
96 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
98 m_buf
= new wxMB2WXbuf(wxConvLocal
.cMB2WX(item
.pszText
));
99 m_item
->pszText
= (wxChar
*)m_buf
->data();
107 #else // !wxUSE_UNICODE
108 wxLV_ITEM(LV_ITEMW
&item
)
110 m_item
= new LV_ITEM((LV_ITEM
&)item
);
112 // the code below doesn't compile without wxUSE_WCHAR_T and as I don't
113 // know if it's useful to have it at all (do we ever get Unicode
114 // notifications in ANSI mode? I don't think so...) I'm not going to
115 // write alternative implementation right now
117 // but if it is indeed used, we should simply directly use
118 // ::WideCharToMultiByte() here
120 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
122 m_buf
= new wxWC2WXbuf(wxConvLocal
.cWC2WX(item
.pszText
));
123 m_item
->pszText
= (wxChar
*)m_buf
->data();
126 #endif // wxUSE_WCHAR_T
129 wxLV_ITEM(LV_ITEMA
&item
) : m_buf(NULL
), m_item(&item
) {}
132 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
136 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
139 ///////////////////////////////////////////////////////
141 // The MSW version had problems with SetTextColour() et
142 // al as the wxListItemAttr's were stored keyed on the
143 // item index. If a item was inserted anywhere but the end
144 // of the list the the text attributes (colour etc) for
145 // the following items were out of sync.
148 // Under MSW the only way to associate data with a List
149 // item independant of its position in the list is to
150 // store a pointer to it in its lParam attribute. However
151 // user programs are already using this (via the
152 // SetItemData() GetItemData() calls).
154 // However what we can do is store a pointer to a
155 // structure which contains the attributes we want *and*
156 // a lParam for the users data, e.g.
158 // class wxListItemInternalData
161 // wxListItemAttr *attr;
162 // long lParam; // user data
165 // To conserve memory, a wxListItemInternalData is
166 // only allocated for a LV_ITEM if text attributes or
167 // user data(lparam) are being set.
170 // class wxListItemInternalData
171 class wxListItemInternalData
174 wxListItemAttr
*attr
;
175 LPARAM lParam
; // user data
177 wxListItemInternalData() : attr(NULL
), lParam(0) {}
178 ~wxListItemInternalData()
184 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
187 // Get the internal data structure
188 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
189 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
190 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
191 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
199 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
200 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
201 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
202 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
203 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
204 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
205 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
206 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
207 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
208 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
209 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
210 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
211 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
213 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
214 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
215 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
216 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
217 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
221 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
222 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
223 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
225 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
227 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
228 EVT_PAINT(wxListCtrl::OnPaint
)
231 // ============================================================================
233 // ============================================================================
235 // ----------------------------------------------------------------------------
236 // wxListCtrl construction
237 // ----------------------------------------------------------------------------
239 void wxListCtrl::Init()
241 m_imageListNormal
= NULL
;
242 m_imageListSmall
= NULL
;
243 m_imageListState
= NULL
;
244 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
248 m_ignoreChangeMessages
= FALSE
;
250 m_AnyInternalData
= FALSE
;
251 m_hasAnyAttr
= FALSE
;
254 bool wxListCtrl::Create(wxWindow
*parent
,
259 const wxValidator
& validator
,
260 const wxString
& name
)
263 SetValidator(validator
);
264 #endif // wxUSE_VALIDATORS
273 m_windowStyle
= style
;
286 m_windowId
= (id
== -1) ? NewControlId() : id
;
288 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
289 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
291 m_baseStyle
= wstyle
;
293 if ( !DoCreateControl(x
, y
, width
, height
) )
297 parent
->AddChild(this);
302 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
304 DWORD wstyle
= m_baseStyle
;
307 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
309 long oldStyle
= 0; // Dummy
310 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
311 wstyle
|= standardStyle
;
313 // Create the ListView control.
314 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
319 GetWinHwnd(GetParent()),
326 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
331 // for comctl32.dll v 4.70+ we want to have this attribute because it's
332 // prettier (and also because wxGTK does it like this)
333 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
335 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
336 0, LVS_EX_FULLROWSELECT
);
339 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
340 SetForegroundColour(GetParent()->GetForegroundColour());
347 void wxListCtrl::UpdateStyle()
351 // The new window view style
353 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
354 dwStyleNew
|= m_baseStyle
;
356 // Get the current window style.
357 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
359 // Only set the window style if the view bits have changed.
360 if ( dwStyleOld
!= dwStyleNew
)
362 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
367 void wxListCtrl::FreeAllInternalData()
369 if (m_AnyInternalData
)
371 int n
= GetItemCount();
374 m_ignoreChangeMessages
= TRUE
;
375 for (i
= 0; i
< n
; i
++)
376 wxDeleteInternalData(this, i
);
377 m_ignoreChangeMessages
= FALSE
;
379 m_AnyInternalData
= FALSE
;
383 wxListCtrl::~wxListCtrl()
385 FreeAllInternalData();
389 m_textCtrl
->UnsubclassWin();
390 m_textCtrl
->SetHWND(0);
395 if (m_ownsImageListNormal
) delete m_imageListNormal
;
396 if (m_ownsImageListSmall
) delete m_imageListSmall
;
397 if (m_ownsImageListState
) delete m_imageListState
;
400 // ----------------------------------------------------------------------------
401 // set/get/change style
402 // ----------------------------------------------------------------------------
404 // Add or remove a single window style
405 void wxListCtrl::SetSingleStyle(long style
, bool add
)
407 long flag
= GetWindowStyleFlag();
409 // Get rid of conflicting styles
412 if ( style
& wxLC_MASK_TYPE
)
413 flag
= flag
& ~wxLC_MASK_TYPE
;
414 if ( style
& wxLC_MASK_ALIGN
)
415 flag
= flag
& ~wxLC_MASK_ALIGN
;
416 if ( style
& wxLC_MASK_SORT
)
417 flag
= flag
& ~wxLC_MASK_SORT
;
433 m_windowStyle
= flag
;
438 // Set the whole window style
439 void wxListCtrl::SetWindowStyleFlag(long flag
)
441 m_windowStyle
= flag
;
446 // Can be just a single style, or a bitlist
447 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
450 if ( style
& wxLC_ICON
)
452 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
453 oldStyle
-= LVS_SMALLICON
;
454 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
455 oldStyle
-= LVS_REPORT
;
456 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
457 oldStyle
-= LVS_LIST
;
461 if ( style
& wxLC_SMALL_ICON
)
463 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
464 oldStyle
-= LVS_ICON
;
465 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
466 oldStyle
-= LVS_REPORT
;
467 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
468 oldStyle
-= LVS_LIST
;
469 wstyle
|= LVS_SMALLICON
;
472 if ( style
& wxLC_LIST
)
474 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
475 oldStyle
-= LVS_ICON
;
476 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
477 oldStyle
-= LVS_REPORT
;
478 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
479 oldStyle
-= LVS_SMALLICON
;
483 if ( style
& wxLC_REPORT
)
485 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
486 oldStyle
-= LVS_ICON
;
487 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
488 oldStyle
-= LVS_LIST
;
489 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
490 oldStyle
-= LVS_SMALLICON
;
492 wstyle
|= LVS_REPORT
;
495 if ( style
& wxLC_ALIGN_LEFT
)
497 if ( oldStyle
& LVS_ALIGNTOP
)
498 oldStyle
-= LVS_ALIGNTOP
;
499 wstyle
|= LVS_ALIGNLEFT
;
502 if ( style
& wxLC_ALIGN_TOP
)
504 if ( oldStyle
& LVS_ALIGNLEFT
)
505 oldStyle
-= LVS_ALIGNLEFT
;
506 wstyle
|= LVS_ALIGNTOP
;
509 if ( style
& wxLC_AUTOARRANGE
)
510 wstyle
|= LVS_AUTOARRANGE
;
512 if ( style
& wxLC_NO_SORT_HEADER
)
513 wstyle
|= LVS_NOSORTHEADER
;
515 if ( style
& wxLC_NO_HEADER
)
516 wstyle
|= LVS_NOCOLUMNHEADER
;
518 if ( style
& wxLC_EDIT_LABELS
)
519 wstyle
|= LVS_EDITLABELS
;
521 if ( style
& wxLC_SINGLE_SEL
)
522 wstyle
|= LVS_SINGLESEL
;
524 if ( style
& wxLC_SORT_ASCENDING
)
526 if ( oldStyle
& LVS_SORTDESCENDING
)
527 oldStyle
-= LVS_SORTDESCENDING
;
528 wstyle
|= LVS_SORTASCENDING
;
531 if ( style
& wxLC_SORT_DESCENDING
)
533 if ( oldStyle
& LVS_SORTASCENDING
)
534 oldStyle
-= LVS_SORTASCENDING
;
535 wstyle
|= LVS_SORTDESCENDING
;
538 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
539 if ( style
& wxLC_VIRTUAL
)
541 int ver
= wxTheApp
->GetComCtl32Version();
544 wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."),
545 ver
/ 100, ver
% 100);
548 wstyle
|= LVS_OWNERDATA
;
555 // ----------------------------------------------------------------------------
557 // ----------------------------------------------------------------------------
559 // Sets the foreground, i.e. text, colour
560 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
562 if ( !wxWindow::SetForegroundColour(col
) )
565 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
570 // Sets the background colour
571 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
573 if ( !wxWindow::SetBackgroundColour(col
) )
576 // we set the same colour for both the "empty" background and the items
578 COLORREF color
= wxColourToRGB(col
);
579 ListView_SetBkColor(GetHwnd(), color
);
580 ListView_SetTextBkColor(GetHwnd(), color
);
585 // Gets information about this column
586 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
591 lvCol
.mask
= LVCF_WIDTH
;
593 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
595 lvCol
.mask
|= LVCF_TEXT
;
596 lvCol
.pszText
= new wxChar
[513];
597 lvCol
.cchTextMax
= 512;
600 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
602 lvCol
.mask
|= LVCF_FMT
;
605 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
607 lvCol
.mask
|= LVCF_IMAGE
;
610 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
612 // item.m_subItem = lvCol.iSubItem;
613 item
.m_width
= lvCol
.cx
;
615 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
617 item
.m_text
= lvCol
.pszText
;
618 delete[] lvCol
.pszText
;
621 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
623 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
625 item
.m_format
= wxLIST_FORMAT_LEFT
;
628 item
.m_format
= wxLIST_FORMAT_RIGHT
;
631 item
.m_format
= wxLIST_FORMAT_CENTRE
;
634 item
.m_format
= -1; // Unknown?
639 // the column images were not supported in older versions but how to check
640 // for this? we can't use _WIN32_IE because we always define it to a very
641 // high value, so see if another symbol which is only defined starting from
642 // comctl32.dll 4.70 is available
643 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
644 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
646 item
.m_image
= lvCol
.iImage
;
648 #endif // LVCOLUMN::iImage exists
653 // Sets information about this column
654 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
657 wxConvertToMSWListCol(col
, item
, lvCol
);
659 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
662 // Gets the column width
663 int wxListCtrl::GetColumnWidth(int col
) const
665 return ListView_GetColumnWidth(GetHwnd(), col
);
668 // Sets the column width
669 bool wxListCtrl::SetColumnWidth(int col
, int width
)
672 if ( m_windowStyle
& wxLC_LIST
)
676 if ( width2
== wxLIST_AUTOSIZE
)
677 width2
= LVSCW_AUTOSIZE
;
678 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
679 width2
= LVSCW_AUTOSIZE_USEHEADER
;
681 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
684 // Gets the number of items that can fit vertically in the
685 // visible area of the list control (list or report view)
686 // or the total number of items in the list control (icon
687 // or small icon view)
688 int wxListCtrl::GetCountPerPage() const
690 return ListView_GetCountPerPage(GetHwnd());
693 // Gets the edit control for editing labels.
694 wxTextCtrl
* wxListCtrl::GetEditControl() const
699 // Gets information about the item
700 bool wxListCtrl::GetItem(wxListItem
& info
) const
703 wxZeroMemory(lvItem
);
705 lvItem
.iItem
= info
.m_itemId
;
706 lvItem
.iSubItem
= info
.m_col
;
708 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
710 lvItem
.mask
|= LVIF_TEXT
;
711 lvItem
.pszText
= new wxChar
[513];
712 lvItem
.cchTextMax
= 512;
716 lvItem
.pszText
= NULL
;
719 if (info
.m_mask
& wxLIST_MASK_DATA
)
720 lvItem
.mask
|= LVIF_PARAM
;
722 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
723 lvItem
.mask
|= LVIF_IMAGE
;
725 if ( info
.m_mask
& wxLIST_MASK_STATE
)
727 lvItem
.mask
|= LVIF_STATE
;
728 // the other bits are hardly interesting anyhow
729 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
732 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
735 wxLogError(_("Couldn't retrieve information about list control item %d."),
740 // give NULL as hwnd as we already have everything we need
741 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
745 delete[] lvItem
.pszText
;
750 // Sets information about the item
751 bool wxListCtrl::SetItem(wxListItem
& info
)
754 wxConvertToMSWListItem(this, info
, item
);
756 // we never update the lParam if it contains our pointer
757 // to the wxListItemInternalData structure
758 item
.mask
&= ~LVIF_PARAM
;
760 // check if setting attributes or lParam
761 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
763 // get internal item data
764 // perhaps a cache here ?
765 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
770 m_AnyInternalData
= TRUE
;
771 data
= new wxListItemInternalData();
772 item
.lParam
= (LPARAM
) data
;
773 item
.mask
|= LVIF_PARAM
;
778 if (info
.m_mask
& wxLIST_MASK_DATA
)
779 data
->lParam
= info
.m_data
;
782 if (info
.HasAttributes())
785 *data
->attr
= *info
.GetAttributes();
787 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
792 // we could be changing only the attribute in which case we don't need to
793 // call ListView_SetItem() at all
797 if ( !ListView_SetItem(GetHwnd(), &item
) )
799 wxLogDebug(_T("ListView_SetItem() failed"));
805 // we need to update the item immediately to show the new image
806 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
808 // check whether it has any custom attributes
809 if ( info
.HasAttributes() )
813 // if the colour has changed, we must redraw the item
819 // we need this to make the change visible right now
820 RefreshItem(item
.iItem
);
826 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
830 info
.m_mask
= wxLIST_MASK_TEXT
;
831 info
.m_itemId
= index
;
835 info
.m_image
= imageId
;
836 info
.m_mask
|= wxLIST_MASK_IMAGE
;
838 return SetItem(info
);
842 // Gets the item state
843 int wxListCtrl::GetItemState(long item
, long stateMask
) const
847 info
.m_mask
= wxLIST_MASK_STATE
;
848 info
.m_stateMask
= stateMask
;
849 info
.m_itemId
= item
;
857 // Sets the item state
858 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
860 // NB: don't use SetItem() here as it doesn't work with the virtual list
863 wxZeroMemory(lvItem
);
865 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
867 // for the virtual list controls we need to refresh the previously focused
868 // item manually when changing focus without changing selection
869 // programmatically because otherwise it keeps its focus rectangle until
870 // next repaint (yet another comctl32 bug)
873 (stateMask
& wxLIST_STATE_FOCUSED
) &&
874 (state
& wxLIST_STATE_FOCUSED
) )
876 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
883 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
884 (WPARAM
)item
, (LPARAM
)&lvItem
) )
886 wxLogLastError(_T("ListView_SetItemState"));
891 if ( focusOld
!= -1 )
893 // no need to refresh the item if it was previously selected, it would
894 // only result in annoying flicker
895 if ( !(GetItemState(focusOld
,
896 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
898 RefreshItem(focusOld
);
905 // Sets the item image
906 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
910 info
.m_mask
= wxLIST_MASK_IMAGE
;
911 info
.m_image
= image
;
912 info
.m_itemId
= item
;
914 return SetItem(info
);
917 // Gets the item text
918 wxString
wxListCtrl::GetItemText(long item
) const
922 info
.m_mask
= wxLIST_MASK_TEXT
;
923 info
.m_itemId
= item
;
926 return wxEmptyString
;
930 // Sets the item text
931 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
935 info
.m_mask
= wxLIST_MASK_TEXT
;
936 info
.m_itemId
= item
;
942 // Gets the item data
943 long wxListCtrl::GetItemData(long item
) const
947 info
.m_mask
= wxLIST_MASK_DATA
;
948 info
.m_itemId
= item
;
955 // Sets the item data
956 bool wxListCtrl::SetItemData(long item
, long data
)
960 info
.m_mask
= wxLIST_MASK_DATA
;
961 info
.m_itemId
= item
;
964 return SetItem(info
);
967 // Gets the item rectangle
968 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
973 if ( code
== wxLIST_RECT_BOUNDS
)
974 codeWin
= LVIR_BOUNDS
;
975 else if ( code
== wxLIST_RECT_ICON
)
977 else if ( code
== wxLIST_RECT_LABEL
)
978 codeWin
= LVIR_LABEL
;
981 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
983 codeWin
= LVIR_BOUNDS
;
986 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
988 rect
.x
= rectWin
.left
;
989 rect
.y
= rectWin
.top
;
990 rect
.width
= rectWin
.right
- rectWin
.left
;
991 rect
.height
= rectWin
.bottom
- rectWin
.top
;
996 // Gets the item position
997 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1001 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1003 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1007 // Sets the item position.
1008 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1010 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1013 // Gets the number of items in the list control
1014 int wxListCtrl::GetItemCount() const
1019 // Retrieves the spacing between icons in pixels.
1020 // If small is TRUE, gets the spacing for the small icon
1021 // view, otherwise the large icon view.
1022 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1024 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1027 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1030 info
.m_itemId
= item
;
1031 info
.SetTextColour( col
);
1035 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1038 info
.m_itemId
= item
;
1040 return info
.GetTextColour();
1043 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1046 info
.m_itemId
= item
;
1047 info
.SetBackgroundColour( col
);
1051 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1054 info
.m_itemId
= item
;
1056 return info
.GetBackgroundColour();
1059 // Gets the number of selected items in the list control
1060 int wxListCtrl::GetSelectedItemCount() const
1062 return ListView_GetSelectedCount(GetHwnd());
1065 // Gets the text colour of the listview
1066 wxColour
wxListCtrl::GetTextColour() const
1068 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1069 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1073 // Sets the text colour of the listview
1074 void wxListCtrl::SetTextColour(const wxColour
& col
)
1076 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1079 // Gets the index of the topmost visible item when in
1080 // list or report view
1081 long wxListCtrl::GetTopItem() const
1083 return (long) ListView_GetTopIndex(GetHwnd());
1086 // Searches for an item, starting from 'item'.
1087 // 'geometry' is one of
1088 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1089 // 'state' is a state bit flag, one or more of
1090 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1091 // item can be -1 to find the first item that matches the
1093 // Returns the item or -1 if unsuccessful.
1094 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1098 if ( geom
== wxLIST_NEXT_ABOVE
)
1099 flags
|= LVNI_ABOVE
;
1100 if ( geom
== wxLIST_NEXT_ALL
)
1102 if ( geom
== wxLIST_NEXT_BELOW
)
1103 flags
|= LVNI_BELOW
;
1104 if ( geom
== wxLIST_NEXT_LEFT
)
1105 flags
|= LVNI_TOLEFT
;
1106 if ( geom
== wxLIST_NEXT_RIGHT
)
1107 flags
|= LVNI_TORIGHT
;
1109 if ( state
& wxLIST_STATE_CUT
)
1111 if ( state
& wxLIST_STATE_DROPHILITED
)
1112 flags
|= LVNI_DROPHILITED
;
1113 if ( state
& wxLIST_STATE_FOCUSED
)
1114 flags
|= LVNI_FOCUSED
;
1115 if ( state
& wxLIST_STATE_SELECTED
)
1116 flags
|= LVNI_SELECTED
;
1118 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1122 wxImageList
*wxListCtrl::GetImageList(int which
) const
1124 if ( which
== wxIMAGE_LIST_NORMAL
)
1126 return m_imageListNormal
;
1128 else if ( which
== wxIMAGE_LIST_SMALL
)
1130 return m_imageListSmall
;
1132 else if ( which
== wxIMAGE_LIST_STATE
)
1134 return m_imageListState
;
1139 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1142 if ( which
== wxIMAGE_LIST_NORMAL
)
1144 flags
= LVSIL_NORMAL
;
1145 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1146 m_imageListNormal
= imageList
;
1147 m_ownsImageListNormal
= FALSE
;
1149 else if ( which
== wxIMAGE_LIST_SMALL
)
1151 flags
= LVSIL_SMALL
;
1152 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1153 m_imageListSmall
= imageList
;
1154 m_ownsImageListSmall
= FALSE
;
1156 else if ( which
== wxIMAGE_LIST_STATE
)
1158 flags
= LVSIL_STATE
;
1159 if (m_ownsImageListState
) delete m_imageListState
;
1160 m_imageListState
= imageList
;
1161 m_ownsImageListState
= FALSE
;
1163 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1166 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1168 SetImageList(imageList
, which
);
1169 if ( which
== wxIMAGE_LIST_NORMAL
)
1170 m_ownsImageListNormal
= TRUE
;
1171 else if ( which
== wxIMAGE_LIST_SMALL
)
1172 m_ownsImageListSmall
= TRUE
;
1173 else if ( which
== wxIMAGE_LIST_STATE
)
1174 m_ownsImageListState
= TRUE
;
1177 // ----------------------------------------------------------------------------
1179 // ----------------------------------------------------------------------------
1181 // Arranges the items
1182 bool wxListCtrl::Arrange(int flag
)
1185 if ( flag
== wxLIST_ALIGN_LEFT
)
1186 code
= LVA_ALIGNLEFT
;
1187 else if ( flag
== wxLIST_ALIGN_TOP
)
1188 code
= LVA_ALIGNTOP
;
1189 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1191 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1192 code
= LVA_SNAPTOGRID
;
1194 return (ListView_Arrange(GetHwnd(), code
) != 0);
1198 bool wxListCtrl::DeleteItem(long item
)
1200 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1202 wxLogLastError(_T("ListView_DeleteItem"));
1207 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1208 wxT("m_count should match ListView_GetItemCount"));
1210 // the virtual list control doesn't refresh itself correctly, help it
1213 // we need to refresh all the lines below the one which was deleted
1215 if ( item
> 0 && GetItemCount() )
1217 GetItemRect(item
- 1, rectItem
);
1222 rectItem
.height
= 0;
1225 wxRect rectWin
= GetRect();
1226 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1227 rectWin
.y
= rectItem
.GetBottom();
1229 RefreshRect(rectWin
);
1235 // Deletes all items
1236 bool wxListCtrl::DeleteAllItems()
1238 FreeAllInternalData();
1239 return ListView_DeleteAllItems(GetHwnd()) != 0;
1242 // Deletes all items
1243 bool wxListCtrl::DeleteAllColumns()
1245 while ( m_colCount
> 0 )
1247 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1249 wxLogLastError(wxT("ListView_DeleteColumn"));
1257 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1263 bool wxListCtrl::DeleteColumn(int col
)
1265 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1267 if ( success
&& (m_colCount
> 0) )
1272 // Clears items, and columns if there are any.
1273 void wxListCtrl::ClearAll()
1276 if ( m_colCount
> 0 )
1280 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1282 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1284 // ListView_EditLabel requires that the list has focus.
1287 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1290 // failed to start editing
1294 // [re]create the text control wrapping the HWND we got
1297 m_textCtrl
->UnsubclassWin();
1298 m_textCtrl
->SetHWND(0);
1302 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1303 m_textCtrl
->SetHWND(hWnd
);
1304 m_textCtrl
->SubclassWin(hWnd
);
1305 m_textCtrl
->SetParent(this);
1307 // we must disallow TABbing away from the control while the edit contol is
1308 // shown because this leaves it in some strange state (just try removing
1309 // this line and then pressing TAB while editing an item in listctrl
1311 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1316 // End label editing, optionally cancelling the edit
1317 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1319 wxFAIL_MSG( _T("not implemented") );
1324 // Ensures this item is visible
1325 bool wxListCtrl::EnsureVisible(long item
)
1327 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1330 // Find an item whose label matches this string, starting from the item after 'start'
1331 // or the beginning if 'start' is -1.
1332 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1334 LV_FINDINFO findInfo
;
1336 findInfo
.flags
= LVFI_STRING
;
1338 findInfo
.flags
|= LVFI_PARTIAL
;
1341 // ListView_FindItem() excludes the first item from search and to look
1342 // through all the items you need to start from -1 which is unnatural and
1343 // inconsistent with the generic version - so we adjust the index
1346 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1349 // Find an item whose data matches this data, starting from the item after 'start'
1350 // or the beginning if 'start' is -1.
1351 // NOTE : Lindsay Mathieson - 14-July-2002
1352 // No longer use ListView_FindItem as the data attribute is now stored
1353 // in a wxListItemInternalData structure refernced by the actual lParam
1354 long wxListCtrl::FindItem(long start
, long data
)
1356 long idx
= start
+ 1;
1357 long count
= GetItemCount();
1361 if (GetItemData(idx
) == data
)
1369 // Find an item nearest this position in the specified direction, starting from
1370 // the item after 'start' or the beginning if 'start' is -1.
1371 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1373 LV_FINDINFO findInfo
;
1375 findInfo
.flags
= LVFI_NEARESTXY
;
1376 findInfo
.pt
.x
= pt
.x
;
1377 findInfo
.pt
.y
= pt
.y
;
1378 findInfo
.vkDirection
= VK_RIGHT
;
1380 if ( direction
== wxLIST_FIND_UP
)
1381 findInfo
.vkDirection
= VK_UP
;
1382 else if ( direction
== wxLIST_FIND_DOWN
)
1383 findInfo
.vkDirection
= VK_DOWN
;
1384 else if ( direction
== wxLIST_FIND_LEFT
)
1385 findInfo
.vkDirection
= VK_LEFT
;
1386 else if ( direction
== wxLIST_FIND_RIGHT
)
1387 findInfo
.vkDirection
= VK_RIGHT
;
1389 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1392 // Determines which item (if any) is at the specified point,
1393 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1394 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1396 LV_HITTESTINFO hitTestInfo
;
1397 hitTestInfo
.pt
.x
= (int) point
.x
;
1398 hitTestInfo
.pt
.y
= (int) point
.y
;
1400 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1403 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1404 flags
|= wxLIST_HITTEST_ABOVE
;
1405 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1406 flags
|= wxLIST_HITTEST_BELOW
;
1407 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1408 flags
|= wxLIST_HITTEST_NOWHERE
;
1409 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1410 flags
|= wxLIST_HITTEST_ONITEMICON
;
1411 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1412 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1413 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1414 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1415 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1416 flags
|= wxLIST_HITTEST_TOLEFT
;
1417 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1418 flags
|= wxLIST_HITTEST_TORIGHT
;
1420 return (long) hitTestInfo
.iItem
;
1423 // Inserts an item, returning the index of the new item if successful,
1425 long wxListCtrl::InsertItem(wxListItem
& info
)
1427 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1430 wxConvertToMSWListItem(this, info
, item
);
1431 item
.mask
&= ~LVIF_PARAM
;
1433 // check wether we need to allocate our internal data
1434 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1435 if (needInternalData
)
1437 m_AnyInternalData
= TRUE
;
1438 item
.mask
|= LVIF_PARAM
;
1440 // internal stucture that manages data
1441 wxListItemInternalData
*data
= new wxListItemInternalData();
1442 item
.lParam
= (LPARAM
) data
;
1444 if (info
.m_mask
& wxLIST_MASK_DATA
)
1445 data
->lParam
= info
.m_data
;
1447 // check whether it has any custom attributes
1448 if ( info
.HasAttributes() )
1450 // take copy of attributes
1451 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1455 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1457 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1458 wxT("m_count should match ListView_GetItemCount"));
1463 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1466 info
.m_text
= label
;
1467 info
.m_mask
= wxLIST_MASK_TEXT
;
1468 info
.m_itemId
= index
;
1469 return InsertItem(info
);
1472 // Inserts an image item
1473 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1476 info
.m_image
= imageIndex
;
1477 info
.m_mask
= wxLIST_MASK_IMAGE
;
1478 info
.m_itemId
= index
;
1479 return InsertItem(info
);
1482 // Inserts an image/string item
1483 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1486 info
.m_image
= imageIndex
;
1487 info
.m_text
= label
;
1488 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1489 info
.m_itemId
= index
;
1490 return InsertItem(info
);
1493 // For list view mode (only), inserts a column.
1494 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1497 wxConvertToMSWListCol(col
, item
, lvCol
);
1499 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1501 // always give some width to the new column: this one is compatible
1502 // with the generic version
1503 lvCol
.mask
|= LVCF_WIDTH
;
1507 // when we insert a column which can contain an image, we must specify this
1508 // flag right now as doing it later in SetColumn() has no effect
1510 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1511 // way to dynamically set/clear the bitmap as the column without a bitmap
1512 // on the left looks ugly (there is a hole)
1514 // unfortunately with my version of comctl32.dll (5.80), the left column
1515 // image is always on the left and it seems that it's a "feature" - I
1516 // didn't find any way to work around it in any case
1517 if ( lvCol
.mask
& LVCF_IMAGE
)
1519 lvCol
.mask
|= LVCF_FMT
;
1520 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
;
1523 bool success
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
) != -1;
1530 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1537 long wxListCtrl::InsertColumn(long col
,
1538 const wxString
& heading
,
1543 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1544 item
.m_text
= heading
;
1547 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1548 item
.m_width
= width
;
1550 item
.m_format
= format
;
1552 return InsertColumn(col
, item
);
1555 // scroll the control by the given number of pixels (exception: in list view,
1556 // dx is interpreted as number of columns)
1557 bool wxListCtrl::ScrollList(int dx
, int dy
)
1559 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1561 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1571 // fn is a function which takes 3 long arguments: item1, item2, data.
1572 // item1 is the long data associated with a first item (NOT the index).
1573 // item2 is the long data associated with a second item (NOT the index).
1574 // data is the same value as passed to SortItems.
1575 // The return value is a negative number if the first item should precede the second
1576 // item, a positive number of the second item should precede the first,
1577 // or zero if the two items are equivalent.
1579 // data is arbitrary data to be passed to the sort function.
1581 // Internal structures for proxying the user compare function
1582 // so that we can pass it the *real* user data
1584 // translate lParam data and call user func
1585 struct wxInternalDataSort
1587 wxListCtrlCompare user_fn
;
1591 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1593 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1595 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1596 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1598 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1599 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1601 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1605 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1607 struct wxInternalDataSort internalData
;
1608 internalData
.user_fn
= fn
;
1609 internalData
.data
= data
;
1611 // WPARAM cast is needed for mingw/cygwin
1612 if ( !ListView_SortItems(GetHwnd(),
1613 wxInternalDataCompareFunc
,
1614 (WPARAM
) &internalData
) )
1616 wxLogDebug(_T("ListView_SortItems() failed"));
1626 // ----------------------------------------------------------------------------
1627 // message processing
1628 // ----------------------------------------------------------------------------
1630 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1632 if (cmd
== EN_UPDATE
)
1634 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1635 event
.SetEventObject( this );
1636 ProcessCommand(event
);
1639 else if (cmd
== EN_KILLFOCUS
)
1641 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1642 event
.SetEventObject( this );
1643 ProcessCommand(event
);
1650 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1653 // prepare the event
1654 // -----------------
1656 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1657 event
.SetEventObject(this);
1659 wxEventType eventType
= wxEVT_NULL
;
1661 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1663 // if your compiler is as broken as this, you should really change it: this
1664 // code is needed for normal operation! #ifdef below is only useful for
1665 // automatic rebuilds which are done with a very old compiler version
1666 #ifdef HDN_BEGINTRACKA
1668 // check for messages from the header (in report view)
1669 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1671 // is it a message from the header?
1672 if ( nmhdr
->hwndFrom
== hwndHdr
)
1674 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1676 event
.m_itemIndex
= -1;
1678 switch ( nmhdr
->code
)
1680 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1681 // TRACK messages even to ANSI programs: on my system I get
1682 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1684 // work around is to simply catch both versions and hope that it
1685 // works (why should this message exist in ANSI and Unicode is
1686 // beyond me as it doesn't deal with strings at all...)
1688 // note that fr HDN_TRACK another possibility could be to use
1689 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1690 // something other than the item width changes so we'd have to
1691 // filter out the unwanted events then
1692 case HDN_BEGINTRACKA
:
1693 case HDN_BEGINTRACKW
:
1694 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1699 if ( eventType
== wxEVT_NULL
)
1700 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1705 if ( eventType
== wxEVT_NULL
)
1706 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1708 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1709 event
.m_col
= nmHDR
->iItem
;
1714 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1717 // find the column clicked: we have to search for it
1718 // ourselves as the notification message doesn't provide
1721 // where did the click occur?
1723 if ( !::GetCursorPos(&ptClick
) )
1725 wxLogLastError(_T("GetCursorPos"));
1728 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1730 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1733 event
.m_pointDrag
.x
= ptClick
.x
;
1734 event
.m_pointDrag
.y
= ptClick
.y
;
1736 int colCount
= Header_GetItemCount(hwndHdr
);
1739 for ( int col
= 0; col
< colCount
; col
++ )
1741 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1743 if ( ::PtInRect(&rect
, ptClick
) )
1753 case HDN_GETDISPINFOW
:
1755 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1756 // This is a fix for a strange bug under XP.
1757 // Normally, info->iItem is a valid index, but
1758 // sometimes this is a silly (large) number
1759 // and when we return FALSE via wxControl::MSWOnNotify
1760 // to indicate that it hasn't yet been processed,
1761 // there's a GPF in Windows.
1762 // By returning TRUE here, we avoid further processing
1763 // of this strange message.
1764 if ( info
->iItem
>= GetColumnCount() )
1770 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1774 #endif // defined(HDN_BEGINTRACKA)
1775 if ( nmhdr
->hwndFrom
== GetHwnd() )
1777 // almost all messages use NM_LISTVIEW
1778 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1780 const int iItem
= nmLV
->iItem
;
1783 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1784 // ignored for efficiency. It is done here because the internal data is in the
1785 // process of being deleted so we don't want to try and access it below.
1786 if ( m_ignoreChangeMessages
&&
1787 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1793 // If we have a valid item then check if there is a data value
1794 // associated with it and put it in the event.
1795 if ( iItem
>= 0 && iItem
< GetItemCount() )
1797 wxListItemInternalData
*internaldata
=
1798 wxGetInternalData(GetHwnd(), iItem
);
1801 event
.m_item
.m_data
= internaldata
->lParam
;
1805 switch ( nmhdr
->code
)
1807 case LVN_BEGINRDRAG
:
1808 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1812 if ( eventType
== wxEVT_NULL
)
1814 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1817 event
.m_itemIndex
= iItem
;
1818 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1819 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1822 // NB: we have to handle both *A and *W versions here because some
1823 // versions of comctl32.dll send ANSI message to an Unicode app
1824 case LVN_BEGINLABELEDITA
:
1826 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1827 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1828 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1829 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1832 case LVN_BEGINLABELEDITW
:
1834 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1835 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1836 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1837 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1841 case LVN_ENDLABELEDITA
:
1843 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1844 wxLV_ITEM
item(((LV_DISPINFOA
*)lParam
)->item
);
1845 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1846 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1847 ((LV_ITEM
)item
).iItem
== -1 )
1849 // don't keep a stale wxTextCtrl around
1852 // EDIT control will be deleted by the list control itself so
1853 // prevent us from deleting it as well
1854 m_textCtrl
->UnsubclassWin();
1855 m_textCtrl
->SetHWND(0);
1862 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1865 case LVN_ENDLABELEDITW
:
1867 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1868 wxLV_ITEM
item(((LV_DISPINFOW
*)lParam
)->item
);
1869 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1870 if ( ((LV_ITEM
)item
).pszText
== NULL
||
1871 ((LV_ITEM
)item
).iItem
== -1 )
1873 // don't keep a stale wxTextCtrl around
1876 // EDIT control will be deleted by the list control itself so
1877 // prevent us from deleting it as well
1878 m_textCtrl
->UnsubclassWin();
1879 m_textCtrl
->SetHWND(0);
1886 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1890 case LVN_COLUMNCLICK
:
1891 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1892 event
.m_itemIndex
= -1;
1893 event
.m_col
= nmLV
->iSubItem
;
1896 case LVN_DELETEALLITEMS
:
1898 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1899 event
.m_itemIndex
= -1;
1902 case LVN_DELETEITEM
:
1904 // this should be prevented by the post-processing code below,
1905 // but "just in case"
1908 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1909 event
.m_itemIndex
= iItem
;
1910 // delete the assoicated internal data
1911 wxDeleteInternalData(this, iItem
);
1914 case LVN_SETDISPINFO
:
1916 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1917 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1918 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1922 case LVN_INSERTITEM
:
1923 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1924 event
.m_itemIndex
= iItem
;
1927 case LVN_ITEMCHANGED
:
1928 // we translate this catch all message into more interesting
1929 // (and more easy to process) wxWindows events
1931 // first of all, we deal with the state change events only and
1932 // only for valid items (item == -1 for the virtual list
1934 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1936 // temp vars for readability
1937 const UINT stOld
= nmLV
->uOldState
;
1938 const UINT stNew
= nmLV
->uNewState
;
1940 event
.m_item
.SetId(iItem
);
1941 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1944 GetItem(event
.m_item
);
1946 // has the focus changed?
1947 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1949 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1950 event
.m_itemIndex
= iItem
;
1953 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1955 if ( eventType
!= wxEVT_NULL
)
1957 // focus and selection have both changed: send the
1958 // focus event from here and the selection one
1960 event
.SetEventType(eventType
);
1961 (void)GetEventHandler()->ProcessEvent(event
);
1963 else // no focus event to send
1965 // then need to set m_itemIndex as it wasn't done
1967 event
.m_itemIndex
= iItem
;
1970 eventType
= stNew
& LVIS_SELECTED
1971 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1972 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1976 if ( eventType
== wxEVT_NULL
)
1978 // not an interesting event for us
1986 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1987 WORD wVKey
= info
->wVKey
;
1989 // get the current selection
1990 long lItem
= GetNextItem(-1,
1992 wxLIST_STATE_SELECTED
);
1994 // <Enter> or <Space> activate the selected item if any (but
1995 // not with Shift and/or Ctrl as then they have a predefined
1996 // meaning for the list view)
1998 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1999 !(wxIsShiftDown() || wxIsCtrlDown()) )
2001 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2005 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2007 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2008 // value which should be used as is
2009 int code
= wxCharCodeMSWToWX(wVKey
);
2010 event
.m_code
= code
? code
: wVKey
;
2014 event
.m_item
.m_itemId
= lItem
;
2018 // fill the other fields too
2019 event
.m_item
.m_text
= GetItemText(lItem
);
2020 event
.m_item
.m_data
= GetItemData(lItem
);
2026 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2028 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2033 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2034 // if it happened on an item (and not on empty place)
2041 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2042 event
.m_itemIndex
= iItem
;
2043 event
.m_item
.m_text
= GetItemText(iItem
);
2044 event
.m_item
.m_data
= GetItemData(iItem
);
2048 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2049 // don't do anything else
2050 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2055 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2056 LV_HITTESTINFO lvhti
;
2057 wxZeroMemory(lvhti
);
2059 ::GetCursorPos(&(lvhti
.pt
));
2060 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2061 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2063 if ( lvhti
.flags
& LVHT_ONITEM
)
2065 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2066 event
.m_itemIndex
= lvhti
.iItem
;
2067 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2068 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2073 #ifdef NM_CUSTOMDRAW
2074 *result
= OnCustomDraw(lParam
);
2077 #endif // _WIN32_IE >= 0x300
2079 case LVN_ODCACHEHINT
:
2081 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2083 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2085 // we get some really stupid cache hints like ones for
2086 // items in range 0..0 for an empty control or, after
2087 // deleting an item, for items in invalid range -- filter
2089 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2092 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2094 const long iMax
= GetItemCount();
2095 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2100 case LVN_GETDISPINFO
:
2103 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2105 LV_ITEM
& lvi
= info
->item
;
2106 long item
= lvi
.iItem
;
2108 if ( lvi
.mask
& LVIF_TEXT
)
2110 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2111 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2114 // see comment at the end of wxListCtrl::GetColumn()
2115 #ifdef NM_CUSTOMDRAW
2116 if ( lvi
.mask
& LVIF_IMAGE
)
2118 lvi
.iImage
= OnGetItemImage(item
);
2120 #endif // NM_CUSTOMDRAW
2122 // a little dose of healthy paranoia: as we never use
2123 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2124 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2125 _T("we don't support state callbacks yet!") );
2132 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2137 // where did this one come from?
2141 // process the event
2142 // -----------------
2144 event
.SetEventType(eventType
);
2146 bool processed
= GetEventHandler()->ProcessEvent(event
);
2150 switch ( nmhdr
->code
)
2152 case LVN_DELETEALLITEMS
:
2153 // always return TRUE to suppress all additional LVN_DELETEITEM
2154 // notifications - this makes deleting all items from a list ctrl
2159 case LVN_ENDLABELEDITA
:
2160 case LVN_ENDLABELEDITW
:
2161 // logic here is inversed compared to all the other messages
2162 *result
= event
.IsAllowed();
2164 // don't keep a stale wxTextCtrl around
2167 // EDIT control will be deleted by the list control itself so
2168 // prevent us from deleting it as well
2169 m_textCtrl
->UnsubclassWin();
2170 m_textCtrl
->SetHWND(0);
2179 *result
= !event
.IsAllowed();
2184 // see comment at the end of wxListCtrl::GetColumn()
2185 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2187 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2189 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2190 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2191 switch ( nmcd
.dwDrawStage
)
2194 // if we've got any items with non standard attributes,
2195 // notify us before painting each item
2197 // for virtual controls, always suppose that we have attributes as
2198 // there is no way to check for this
2199 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2202 case CDDS_ITEMPREPAINT
:
2204 size_t item
= (size_t)nmcd
.dwItemSpec
;
2205 if ( item
>= (size_t)GetItemCount() )
2207 // we get this message with item == 0 for an empty control,
2208 // we must ignore it as calling OnGetItemAttr() would be
2210 return CDRF_DODEFAULT
;
2213 wxListItemAttr
*attr
=
2214 IsVirtual() ? OnGetItemAttr(item
)
2215 : wxGetInternalDataAttr(this, item
);
2219 // nothing to do for this item
2220 return CDRF_DODEFAULT
;
2224 wxColour colText
, colBack
;
2225 if ( attr
->HasFont() )
2227 wxFont font
= attr
->GetFont();
2228 hFont
= (HFONT
)font
.GetResourceHandle();
2235 if ( attr
->HasTextColour() )
2237 colText
= attr
->GetTextColour();
2241 colText
= GetTextColour();
2244 if ( attr
->HasBackgroundColour() )
2246 colBack
= attr
->GetBackgroundColour();
2250 colBack
= GetBackgroundColour();
2253 lplvcd
->clrText
= wxColourToRGB(colText
);
2254 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2256 // note that if we wanted to set colours for
2257 // individual columns (subitems), we would have
2258 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2261 ::SelectObject(nmcd
.hdc
, hFont
);
2263 return CDRF_NEWFONT
;
2266 // fall through to return CDRF_DODEFAULT
2269 return CDRF_DODEFAULT
;
2273 #endif // NM_CUSTOMDRAW supported
2275 // Necessary for drawing hrules and vrules, if specified
2276 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2280 wxControl::OnPaint(event
);
2282 // Reset the device origin since it may have been set
2283 dc
.SetDeviceOrigin(0, 0);
2285 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2286 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2288 if (!drawHRules
&& !drawVRules
)
2290 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2293 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2295 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2297 wxSize clientSize
= GetClientSize();
2301 int itemCount
= GetItemCount();
2305 long top
= GetTopItem();
2306 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2308 if (GetItemRect(i
, itemRect
))
2310 cy
= itemRect
.GetTop();
2311 if (i
!= 0) // Don't draw the first one
2313 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2316 if (i
== itemCount
- 1)
2318 cy
= itemRect
.GetBottom();
2319 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2325 if (drawVRules
&& (i
> -1))
2327 wxRect firstItemRect
;
2328 GetItemRect(0, firstItemRect
);
2330 if (GetItemRect(i
, itemRect
))
2333 int x
= itemRect
.GetX();
2334 for (col
= 0; col
< GetColumnCount(); col
++)
2336 int colWidth
= GetColumnWidth(col
);
2338 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2344 // ----------------------------------------------------------------------------
2345 // virtual list controls
2346 // ----------------------------------------------------------------------------
2348 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2350 // this is a pure virtual function, in fact - which is not really pure
2351 // because the controls which are not virtual don't need to implement it
2352 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2354 return wxEmptyString
;
2357 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2360 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2365 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2367 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2368 _T("invalid item index in OnGetItemAttr()") );
2370 // no attributes by default
2374 void wxListCtrl::SetItemCount(long count
)
2376 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2378 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
, LVSICF_NOSCROLL
) )
2380 wxLogLastError(_T("ListView_SetItemCount"));
2383 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2384 wxT("m_count should match ListView_GetItemCount"));
2387 void wxListCtrl::RefreshItem(long item
)
2389 // strangely enough, ListView_Update() results in much more flicker here
2390 // than a dumb Refresh() -- why?
2392 if ( !ListView_Update(GetHwnd(), item
) )
2394 wxLogLastError(_T("ListView_Update"));
2398 GetItemRect(item
, rect
);
2403 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2405 wxRect rect1
, rect2
;
2406 GetItemRect(itemFrom
, rect1
);
2407 GetItemRect(itemTo
, rect2
);
2409 wxRect rect
= rect1
;
2410 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2415 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2418 it
.mask
= LVIF_PARAM
;
2421 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2423 return (wxListItemInternalData
*) it
.lParam
;
2428 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2430 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2433 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2435 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2442 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2444 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2448 memset(&item
, 0, sizeof(item
));
2449 item
.iItem
= itemId
;
2450 item
.mask
= LVIF_PARAM
;
2451 item
.lParam
= (LPARAM
) 0;
2452 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2457 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2461 wxListItemInternalData
*internaldata
=
2462 (wxListItemInternalData
*) lvItem
.lParam
;
2465 info
.m_data
= internaldata
->lParam
;
2469 info
.m_stateMask
= 0;
2470 info
.m_itemId
= lvItem
.iItem
;
2472 long oldMask
= lvItem
.mask
;
2474 bool needText
= FALSE
;
2475 if (hwndListCtrl
!= 0)
2477 if ( lvItem
.mask
& LVIF_TEXT
)
2484 lvItem
.pszText
= new wxChar
[513];
2485 lvItem
.cchTextMax
= 512;
2487 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2488 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2491 if ( lvItem
.mask
& LVIF_STATE
)
2493 info
.m_mask
|= wxLIST_MASK_STATE
;
2495 if ( lvItem
.stateMask
& LVIS_CUT
)
2497 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2498 if ( lvItem
.state
& LVIS_CUT
)
2499 info
.m_state
|= wxLIST_STATE_CUT
;
2501 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2503 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2504 if ( lvItem
.state
& LVIS_DROPHILITED
)
2505 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2507 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2509 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2510 if ( lvItem
.state
& LVIS_FOCUSED
)
2511 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2513 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2515 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2516 if ( lvItem
.state
& LVIS_SELECTED
)
2517 info
.m_state
|= wxLIST_STATE_SELECTED
;
2521 if ( lvItem
.mask
& LVIF_TEXT
)
2523 info
.m_mask
|= wxLIST_MASK_TEXT
;
2524 info
.m_text
= lvItem
.pszText
;
2526 if ( lvItem
.mask
& LVIF_IMAGE
)
2528 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2529 info
.m_image
= lvItem
.iImage
;
2531 if ( lvItem
.mask
& LVIF_PARAM
)
2532 info
.m_mask
|= wxLIST_MASK_DATA
;
2533 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2534 info
.m_mask
|= wxLIST_SET_ITEM
;
2535 info
.m_col
= lvItem
.iSubItem
;
2540 delete[] lvItem
.pszText
;
2542 lvItem
.mask
= oldMask
;
2545 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2547 if (stateMask
& wxLIST_STATE_CUT
)
2549 lvItem
.stateMask
|= LVIS_CUT
;
2550 if (state
& wxLIST_STATE_CUT
)
2551 lvItem
.state
|= LVIS_CUT
;
2553 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2555 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2556 if (state
& wxLIST_STATE_DROPHILITED
)
2557 lvItem
.state
|= LVIS_DROPHILITED
;
2559 if (stateMask
& wxLIST_STATE_FOCUSED
)
2561 lvItem
.stateMask
|= LVIS_FOCUSED
;
2562 if (state
& wxLIST_STATE_FOCUSED
)
2563 lvItem
.state
|= LVIS_FOCUSED
;
2565 if (stateMask
& wxLIST_STATE_SELECTED
)
2567 lvItem
.stateMask
|= LVIS_SELECTED
;
2568 if (state
& wxLIST_STATE_SELECTED
)
2569 lvItem
.state
|= LVIS_SELECTED
;
2573 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2574 const wxListItem
& info
,
2577 lvItem
.iItem
= (int) info
.m_itemId
;
2579 lvItem
.iImage
= info
.m_image
;
2580 lvItem
.stateMask
= 0;
2583 lvItem
.iSubItem
= info
.m_col
;
2585 if (info
.m_mask
& wxLIST_MASK_STATE
)
2587 lvItem
.mask
|= LVIF_STATE
;
2589 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2592 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2594 lvItem
.mask
|= LVIF_TEXT
;
2595 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2597 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2601 // pszText is not const, hence the cast
2602 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2603 if ( lvItem
.pszText
)
2604 lvItem
.cchTextMax
= info
.m_text
.Length();
2606 lvItem
.cchTextMax
= 0;
2609 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2610 lvItem
.mask
|= LVIF_IMAGE
;
2613 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2616 wxZeroMemory(lvCol
);
2618 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2620 lvCol
.mask
|= LVCF_TEXT
;
2621 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2624 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2626 lvCol
.mask
|= LVCF_FMT
;
2628 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2629 lvCol
.fmt
= LVCFMT_LEFT
;
2630 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2631 lvCol
.fmt
= LVCFMT_RIGHT
;
2632 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2633 lvCol
.fmt
= LVCFMT_CENTER
;
2636 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2638 lvCol
.mask
|= LVCF_WIDTH
;
2639 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2640 lvCol
.cx
= LVSCW_AUTOSIZE
;
2641 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2642 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2644 lvCol
.cx
= item
.m_width
;
2647 // see comment at the end of wxListCtrl::GetColumn()
2648 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2649 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2651 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2653 lvCol
.mask
|= LVCF_IMAGE
;
2654 lvCol
.iImage
= item
.m_image
;
2656 //else: it doesn't support item images anyhow
2658 #endif // _WIN32_IE >= 0x0300
2661 #endif // wxUSE_LISTCTRL