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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(__WXWINCE__) && !defined(__HANDHELDPC__)
56 // include <commctrl.h> "properly"
57 #include "wx/msw/wrapcctl.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // convert our state and mask flags to LV_ITEM constants
64 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
66 // convert wxListItem to LV_ITEM
67 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
68 const wxListItem
& info
, LV_ITEM
& lvItem
);
70 // convert LV_ITEM to wxListItem
71 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
73 /* const */ LV_ITEM
& lvItem
);
75 // convert our wxListItem to LV_COLUMN
76 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
79 // ----------------------------------------------------------------------------
80 // private helper classes
81 // ----------------------------------------------------------------------------
83 // We have to handle both fooW and fooA notifications in several cases
84 // because of broken comctl32.dll and/or unicows.dll. This class is used to
85 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
86 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
87 // by wxConvertToMSWListItem().
89 #define LV_ITEM_NATIVE LV_ITEMW
90 #define LV_ITEM_OTHER LV_ITEMA
92 #define LV_CONV_TO_WX cMB2WX
93 #define LV_CONV_BUF wxMB2WXbuf
95 #define LV_ITEM_NATIVE LV_ITEMA
96 #define LV_ITEM_OTHER LV_ITEMW
98 #define LV_CONV_TO_WX cWC2WX
99 #define LV_CONV_BUF wxWC2WXbuf
100 #endif // Unicode/ANSI
105 // default ctor, use Init() later
106 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
108 // init without conversion
109 void Init(LV_ITEM_NATIVE
& item
)
111 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
116 // init with conversion
117 void Init(LV_ITEM_OTHER
& item
)
119 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
120 // point to our own m_item
122 // memcpy() can't work if the struct sizes are different
123 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
124 CodeCantWorkIfDiffSizes
);
126 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
128 // convert text from ANSI to Unicod if necessary
129 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
131 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
132 m_item
.pszText
= (wxChar
*)m_buf
->data();
136 // ctor without conversion
137 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
139 // ctor with conversion
140 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
145 ~wxLV_ITEM() { delete m_buf
; }
147 // conversion to the real LV_ITEM
148 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
153 LV_ITEM_NATIVE
*m_pItem
;
154 LV_ITEM_NATIVE m_item
;
156 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
159 ///////////////////////////////////////////////////////
161 // The MSW version had problems with SetTextColour() et
162 // al as the wxListItemAttr's were stored keyed on the
163 // item index. If a item was inserted anywhere but the end
164 // of the list the the text attributes (colour etc) for
165 // the following items were out of sync.
168 // Under MSW the only way to associate data with a List
169 // item independant of its position in the list is to
170 // store a pointer to it in its lParam attribute. However
171 // user programs are already using this (via the
172 // SetItemData() GetItemData() calls).
174 // However what we can do is store a pointer to a
175 // structure which contains the attributes we want *and*
176 // a lParam for the users data, e.g.
178 // class wxListItemInternalData
181 // wxListItemAttr *attr;
182 // long lParam; // user data
185 // To conserve memory, a wxListItemInternalData is
186 // only allocated for a LV_ITEM if text attributes or
187 // user data(lparam) are being set.
190 // class wxListItemInternalData
191 class wxListItemInternalData
194 wxListItemAttr
*attr
;
195 LPARAM lParam
; // user data
197 wxListItemInternalData() : attr(NULL
), lParam(0) {}
198 ~wxListItemInternalData()
204 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
207 // Get the internal data structure
208 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
209 static wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
);
210 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
211 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
237 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
238 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
239 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
241 #if wxUSE_EXTENDED_RTTI
242 WX_DEFINE_FLAGS( wxListCtrlStyle
)
244 wxBEGIN_FLAGS( wxListCtrlStyle
)
245 // new style border flags, we put them first to
246 // use them for streaming out
247 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
248 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
249 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
250 wxFLAGS_MEMBER(wxBORDER_RAISED
)
251 wxFLAGS_MEMBER(wxBORDER_STATIC
)
252 wxFLAGS_MEMBER(wxBORDER_NONE
)
254 // old style border flags
255 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
256 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
257 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
258 wxFLAGS_MEMBER(wxRAISED_BORDER
)
259 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
260 wxFLAGS_MEMBER(wxBORDER
)
262 // standard window styles
263 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
264 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
265 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
266 wxFLAGS_MEMBER(wxWANTS_CHARS
)
267 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
268 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
269 wxFLAGS_MEMBER(wxVSCROLL
)
270 wxFLAGS_MEMBER(wxHSCROLL
)
272 wxFLAGS_MEMBER(wxLC_LIST
)
273 wxFLAGS_MEMBER(wxLC_REPORT
)
274 wxFLAGS_MEMBER(wxLC_ICON
)
275 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
276 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
277 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
278 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
279 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
280 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
281 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
282 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
283 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
284 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
285 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
287 wxEND_FLAGS( wxListCtrlStyle
)
289 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
291 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
292 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
294 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
295 wxEND_PROPERTIES_TABLE()
297 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
298 wxEND_HANDLERS_TABLE()
300 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
303 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
306 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
309 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
310 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
312 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
314 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
315 EVT_PAINT(wxListCtrl::OnPaint
)
318 // ============================================================================
320 // ============================================================================
322 // ----------------------------------------------------------------------------
323 // wxListCtrl construction
324 // ----------------------------------------------------------------------------
326 void wxListCtrl::Init()
328 m_imageListNormal
= NULL
;
329 m_imageListSmall
= NULL
;
330 m_imageListState
= NULL
;
331 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= false;
334 m_ignoreChangeMessages
= false;
336 m_AnyInternalData
= false;
337 m_hasAnyAttr
= false;
340 bool wxListCtrl::Create(wxWindow
*parent
,
345 const wxValidator
& validator
,
346 const wxString
& name
)
348 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
351 if ( !MSWCreateControl(WC_LISTVIEW
, _T(""), pos
, size
) )
354 // explicitly say that we want to use Unicode because otherwise we get ANSI
355 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
356 wxSetCCUnicodeFormat(GetHwnd());
358 // for comctl32.dll v 4.70+ we want to have this attribute because it's
359 // prettier (and also because wxGTK does it like this)
360 if ( InReportView() && wxTheApp
->GetComCtl32Version() >= 470 )
362 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
363 0, LVS_EX_FULLROWSELECT
);
369 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
371 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
373 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
378 #define MAP_MODE_STYLE(wx, ms) \
379 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
380 #else // !__WXDEBUG__
381 #define MAP_MODE_STYLE(wx, ms) \
382 if ( style & (wx) ) wstyle |= (ms);
383 #endif // __WXDEBUG__
385 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
386 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
387 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
388 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
390 wxASSERT_MSG( nModes
== 1,
391 _T("wxListCtrl style should have exactly one mode bit set") );
393 #undef MAP_MODE_STYLE
395 if ( style
& wxLC_ALIGN_LEFT
)
396 wstyle
|= LVS_ALIGNLEFT
;
398 if ( style
& wxLC_ALIGN_TOP
)
399 wstyle
|= LVS_ALIGNTOP
;
401 if ( style
& wxLC_AUTOARRANGE
)
402 wstyle
|= LVS_AUTOARRANGE
;
404 if ( style
& wxLC_NO_SORT_HEADER
)
405 wstyle
|= LVS_NOSORTHEADER
;
407 if ( style
& wxLC_NO_HEADER
)
408 wstyle
|= LVS_NOCOLUMNHEADER
;
410 if ( style
& wxLC_EDIT_LABELS
)
411 wstyle
|= LVS_EDITLABELS
;
413 if ( style
& wxLC_SINGLE_SEL
)
414 wstyle
|= LVS_SINGLESEL
;
416 if ( style
& wxLC_SORT_ASCENDING
)
418 wstyle
|= LVS_SORTASCENDING
;
420 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
421 _T("can't sort in ascending and descending orders at once") );
423 else if ( style
& wxLC_SORT_DESCENDING
)
424 wstyle
|= LVS_SORTDESCENDING
;
426 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
427 if ( style
& wxLC_VIRTUAL
)
429 int ver
= wxTheApp
->GetComCtl32Version();
432 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."),
433 ver
/ 100, ver
% 100);
436 wstyle
|= LVS_OWNERDATA
;
438 #endif // ancient cygwin
443 void wxListCtrl::UpdateStyle()
447 // The new window view style
448 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
450 // Get the current window style.
451 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
453 // Only set the window style if the view bits have changed.
454 if ( dwStyleOld
!= dwStyleNew
)
456 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
461 void wxListCtrl::FreeAllInternalData()
463 if (m_AnyInternalData
)
465 int n
= GetItemCount();
467 m_ignoreChangeMessages
= true;
468 for (int i
= 0; i
< n
; i
++)
469 wxDeleteInternalData(this, i
);
470 m_ignoreChangeMessages
= false;
472 m_AnyInternalData
= false;
476 wxListCtrl::~wxListCtrl()
478 FreeAllInternalData();
482 m_textCtrl
->UnsubclassWin();
483 m_textCtrl
->SetHWND(0);
488 if (m_ownsImageListNormal
) delete m_imageListNormal
;
489 if (m_ownsImageListSmall
) delete m_imageListSmall
;
490 if (m_ownsImageListState
) delete m_imageListState
;
493 // ----------------------------------------------------------------------------
494 // set/get/change style
495 // ----------------------------------------------------------------------------
497 // Add or remove a single window style
498 void wxListCtrl::SetSingleStyle(long style
, bool add
)
500 long flag
= GetWindowStyleFlag();
502 // Get rid of conflicting styles
505 if ( style
& wxLC_MASK_TYPE
)
506 flag
= flag
& ~wxLC_MASK_TYPE
;
507 if ( style
& wxLC_MASK_ALIGN
)
508 flag
= flag
& ~wxLC_MASK_ALIGN
;
509 if ( style
& wxLC_MASK_SORT
)
510 flag
= flag
& ~wxLC_MASK_SORT
;
526 m_windowStyle
= flag
;
531 // Set the whole window style
532 void wxListCtrl::SetWindowStyleFlag(long flag
)
534 m_windowStyle
= flag
;
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 /* static */ wxVisualAttributes
544 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
546 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
548 // common controls have their own default font
549 attrs
.font
= wxGetCCDefaultFont();
554 // Sets the foreground, i.e. text, colour
555 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
557 if ( !wxWindow::SetForegroundColour(col
) )
560 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
565 // Sets the background colour
566 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
568 if ( !wxWindow::SetBackgroundColour(col
) )
571 // we set the same colour for both the "empty" background and the items
573 COLORREF color
= wxColourToRGB(col
);
574 ListView_SetBkColor(GetHwnd(), color
);
575 ListView_SetTextBkColor(GetHwnd(), color
);
580 // Gets information about this column
581 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
586 lvCol
.mask
= LVCF_WIDTH
;
588 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
590 lvCol
.mask
|= LVCF_TEXT
;
591 lvCol
.pszText
= new wxChar
[513];
592 lvCol
.cchTextMax
= 512;
595 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
597 lvCol
.mask
|= LVCF_FMT
;
600 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
602 lvCol
.mask
|= LVCF_IMAGE
;
605 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
607 // item.m_subItem = lvCol.iSubItem;
608 item
.m_width
= lvCol
.cx
;
610 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
612 item
.m_text
= lvCol
.pszText
;
613 delete[] lvCol
.pszText
;
616 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
618 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
620 item
.m_format
= wxLIST_FORMAT_LEFT
;
623 item
.m_format
= wxLIST_FORMAT_RIGHT
;
626 item
.m_format
= wxLIST_FORMAT_CENTRE
;
629 item
.m_format
= -1; // Unknown?
634 // the column images were not supported in older versions but how to check
635 // for this? we can't use _WIN32_IE because we always define it to a very
636 // high value, so see if another symbol which is only defined starting from
637 // comctl32.dll 4.70 is available
638 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
639 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
641 item
.m_image
= lvCol
.iImage
;
643 #endif // LVCOLUMN::iImage exists
648 // Sets information about this column
649 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
652 wxConvertToMSWListCol(col
, item
, lvCol
);
654 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
657 // Gets the column width
658 int wxListCtrl::GetColumnWidth(int col
) const
660 return ListView_GetColumnWidth(GetHwnd(), col
);
663 // Sets the column width
664 bool wxListCtrl::SetColumnWidth(int col
, int width
)
667 if ( m_windowStyle
& wxLC_LIST
)
671 if ( width2
== wxLIST_AUTOSIZE
)
672 width2
= LVSCW_AUTOSIZE
;
673 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
674 width2
= LVSCW_AUTOSIZE_USEHEADER
;
676 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
679 // Gets the number of items that can fit vertically in the
680 // visible area of the list control (list or report view)
681 // or the total number of items in the list control (icon
682 // or small icon view)
683 int wxListCtrl::GetCountPerPage() const
685 return ListView_GetCountPerPage(GetHwnd());
688 // Gets the edit control for editing labels.
689 wxTextCtrl
* wxListCtrl::GetEditControl() const
694 // Gets information about the item
695 bool wxListCtrl::GetItem(wxListItem
& info
) const
698 wxZeroMemory(lvItem
);
700 lvItem
.iItem
= info
.m_itemId
;
701 lvItem
.iSubItem
= info
.m_col
;
703 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
705 lvItem
.mask
|= LVIF_TEXT
;
706 lvItem
.pszText
= new wxChar
[513];
707 lvItem
.cchTextMax
= 512;
711 lvItem
.pszText
= NULL
;
714 if (info
.m_mask
& wxLIST_MASK_DATA
)
715 lvItem
.mask
|= LVIF_PARAM
;
717 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
718 lvItem
.mask
|= LVIF_IMAGE
;
720 if ( info
.m_mask
& wxLIST_MASK_STATE
)
722 lvItem
.mask
|= LVIF_STATE
;
723 // the other bits are hardly interesting anyhow
724 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
727 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
730 wxLogError(_("Couldn't retrieve information about list control item %d."),
735 // give NULL as hwnd as we already have everything we need
736 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
740 delete[] lvItem
.pszText
;
745 // Sets information about the item
746 bool wxListCtrl::SetItem(wxListItem
& info
)
749 wxConvertToMSWListItem(this, info
, item
);
751 // we never update the lParam if it contains our pointer
752 // to the wxListItemInternalData structure
753 item
.mask
&= ~LVIF_PARAM
;
755 // check if setting attributes or lParam
756 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
758 // get internal item data
759 // perhaps a cache here ?
760 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
765 m_AnyInternalData
= true;
766 data
= new wxListItemInternalData();
767 item
.lParam
= (LPARAM
) data
;
768 item
.mask
|= LVIF_PARAM
;
773 if (info
.m_mask
& wxLIST_MASK_DATA
)
774 data
->lParam
= info
.m_data
;
777 if (info
.HasAttributes())
780 *data
->attr
= *info
.GetAttributes();
782 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
787 // we could be changing only the attribute in which case we don't need to
788 // call ListView_SetItem() at all
792 if ( !ListView_SetItem(GetHwnd(), &item
) )
794 wxLogDebug(_T("ListView_SetItem() failed"));
800 // we need to update the item immediately to show the new image
801 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
803 // check whether it has any custom attributes
804 if ( info
.HasAttributes() )
808 // if the colour has changed, we must redraw the item
814 // we need this to make the change visible right now
815 RefreshItem(item
.iItem
);
821 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
825 info
.m_mask
= wxLIST_MASK_TEXT
;
826 info
.m_itemId
= index
;
830 info
.m_image
= imageId
;
831 info
.m_mask
|= wxLIST_MASK_IMAGE
;
833 return SetItem(info
);
837 // Gets the item state
838 int wxListCtrl::GetItemState(long item
, long stateMask
) const
842 info
.m_mask
= wxLIST_MASK_STATE
;
843 info
.m_stateMask
= stateMask
;
844 info
.m_itemId
= item
;
852 // Sets the item state
853 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
855 // NB: don't use SetItem() here as it doesn't work with the virtual list
858 wxZeroMemory(lvItem
);
860 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
862 // for the virtual list controls we need to refresh the previously focused
863 // item manually when changing focus without changing selection
864 // programmatically because otherwise it keeps its focus rectangle until
865 // next repaint (yet another comctl32 bug)
868 (stateMask
& wxLIST_STATE_FOCUSED
) &&
869 (state
& wxLIST_STATE_FOCUSED
) )
871 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
878 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
879 (WPARAM
)item
, (LPARAM
)&lvItem
) )
881 wxLogLastError(_T("ListView_SetItemState"));
886 if ( focusOld
!= -1 )
888 // no need to refresh the item if it was previously selected, it would
889 // only result in annoying flicker
890 if ( !(GetItemState(focusOld
,
891 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
893 RefreshItem(focusOld
);
900 // Sets the item image
901 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
905 info
.m_mask
= wxLIST_MASK_IMAGE
;
906 info
.m_image
= image
;
907 info
.m_itemId
= item
;
909 return SetItem(info
);
912 // Gets the item text
913 wxString
wxListCtrl::GetItemText(long item
) const
917 info
.m_mask
= wxLIST_MASK_TEXT
;
918 info
.m_itemId
= item
;
921 return wxEmptyString
;
925 // Sets the item text
926 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
930 info
.m_mask
= wxLIST_MASK_TEXT
;
931 info
.m_itemId
= item
;
937 // Gets the item data
938 long wxListCtrl::GetItemData(long item
) const
942 info
.m_mask
= wxLIST_MASK_DATA
;
943 info
.m_itemId
= item
;
950 // Sets the item data
951 bool wxListCtrl::SetItemData(long item
, long data
)
955 info
.m_mask
= wxLIST_MASK_DATA
;
956 info
.m_itemId
= item
;
959 return SetItem(info
);
962 wxRect
wxListCtrl::GetViewRect() const
964 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
965 _T("wxListCtrl::GetViewRect() only works in icon mode") );
968 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
970 wxLogDebug(_T("ListView_GetViewRect() failed."));
976 wxCopyRECTToRect(rc
, rect
);
981 // Gets the item rectangle
982 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
987 if ( code
== wxLIST_RECT_BOUNDS
)
988 codeWin
= LVIR_BOUNDS
;
989 else if ( code
== wxLIST_RECT_ICON
)
991 else if ( code
== wxLIST_RECT_LABEL
)
992 codeWin
= LVIR_LABEL
;
995 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
997 codeWin
= LVIR_BOUNDS
;
1000 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1002 rect
.x
= rectWin
.left
;
1003 rect
.y
= rectWin
.top
;
1004 rect
.width
= rectWin
.right
- rectWin
.left
;
1005 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1010 // Gets the item position
1011 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1015 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1017 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1021 // Sets the item position.
1022 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1024 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1027 // Gets the number of items in the list control
1028 int wxListCtrl::GetItemCount() const
1033 wxSize
wxListCtrl::GetItemSpacing() const
1035 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1037 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1040 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1042 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1045 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1048 info
.m_itemId
= item
;
1049 info
.SetTextColour( col
);
1053 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1056 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1057 if ( data
&& data
->attr
)
1058 col
= data
->attr
->GetTextColour();
1063 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1066 info
.m_itemId
= item
;
1067 info
.SetBackgroundColour( col
);
1071 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1074 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1075 if ( data
&& data
->attr
)
1076 col
= data
->attr
->GetBackgroundColour();
1081 // Gets the number of selected items in the list control
1082 int wxListCtrl::GetSelectedItemCount() const
1084 return ListView_GetSelectedCount(GetHwnd());
1087 // Gets the text colour of the listview
1088 wxColour
wxListCtrl::GetTextColour() const
1090 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1091 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1095 // Sets the text colour of the listview
1096 void wxListCtrl::SetTextColour(const wxColour
& col
)
1098 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1101 // Gets the index of the topmost visible item when in
1102 // list or report view
1103 long wxListCtrl::GetTopItem() const
1105 return (long) ListView_GetTopIndex(GetHwnd());
1108 // Searches for an item, starting from 'item'.
1109 // 'geometry' is one of
1110 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1111 // 'state' is a state bit flag, one or more of
1112 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1113 // item can be -1 to find the first item that matches the
1115 // Returns the item or -1 if unsuccessful.
1116 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1120 if ( geom
== wxLIST_NEXT_ABOVE
)
1121 flags
|= LVNI_ABOVE
;
1122 if ( geom
== wxLIST_NEXT_ALL
)
1124 if ( geom
== wxLIST_NEXT_BELOW
)
1125 flags
|= LVNI_BELOW
;
1126 if ( geom
== wxLIST_NEXT_LEFT
)
1127 flags
|= LVNI_TOLEFT
;
1128 if ( geom
== wxLIST_NEXT_RIGHT
)
1129 flags
|= LVNI_TORIGHT
;
1131 if ( state
& wxLIST_STATE_CUT
)
1133 if ( state
& wxLIST_STATE_DROPHILITED
)
1134 flags
|= LVNI_DROPHILITED
;
1135 if ( state
& wxLIST_STATE_FOCUSED
)
1136 flags
|= LVNI_FOCUSED
;
1137 if ( state
& wxLIST_STATE_SELECTED
)
1138 flags
|= LVNI_SELECTED
;
1140 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1144 wxImageList
*wxListCtrl::GetImageList(int which
) const
1146 if ( which
== wxIMAGE_LIST_NORMAL
)
1148 return m_imageListNormal
;
1150 else if ( which
== wxIMAGE_LIST_SMALL
)
1152 return m_imageListSmall
;
1154 else if ( which
== wxIMAGE_LIST_STATE
)
1156 return m_imageListState
;
1161 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1164 if ( which
== wxIMAGE_LIST_NORMAL
)
1166 flags
= LVSIL_NORMAL
;
1167 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1168 m_imageListNormal
= imageList
;
1169 m_ownsImageListNormal
= false;
1171 else if ( which
== wxIMAGE_LIST_SMALL
)
1173 flags
= LVSIL_SMALL
;
1174 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1175 m_imageListSmall
= imageList
;
1176 m_ownsImageListSmall
= false;
1178 else if ( which
== wxIMAGE_LIST_STATE
)
1180 flags
= LVSIL_STATE
;
1181 if (m_ownsImageListState
) delete m_imageListState
;
1182 m_imageListState
= imageList
;
1183 m_ownsImageListState
= false;
1185 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1188 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1190 SetImageList(imageList
, which
);
1191 if ( which
== wxIMAGE_LIST_NORMAL
)
1192 m_ownsImageListNormal
= true;
1193 else if ( which
== wxIMAGE_LIST_SMALL
)
1194 m_ownsImageListSmall
= true;
1195 else if ( which
== wxIMAGE_LIST_STATE
)
1196 m_ownsImageListState
= true;
1199 // ----------------------------------------------------------------------------
1201 // ----------------------------------------------------------------------------
1203 // Arranges the items
1204 bool wxListCtrl::Arrange(int flag
)
1207 if ( flag
== wxLIST_ALIGN_LEFT
)
1208 code
= LVA_ALIGNLEFT
;
1209 else if ( flag
== wxLIST_ALIGN_TOP
)
1210 code
= LVA_ALIGNTOP
;
1211 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1213 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1214 code
= LVA_SNAPTOGRID
;
1216 return (ListView_Arrange(GetHwnd(), code
) != 0);
1220 bool wxListCtrl::DeleteItem(long item
)
1222 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1224 wxLogLastError(_T("ListView_DeleteItem"));
1229 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1230 wxT("m_count should match ListView_GetItemCount"));
1232 // the virtual list control doesn't refresh itself correctly, help it
1235 // we need to refresh all the lines below the one which was deleted
1237 if ( item
> 0 && GetItemCount() )
1239 GetItemRect(item
- 1, rectItem
);
1244 rectItem
.height
= 0;
1247 wxRect rectWin
= GetRect();
1248 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1249 rectWin
.y
= rectItem
.GetBottom();
1251 RefreshRect(rectWin
);
1257 // Deletes all items
1258 bool wxListCtrl::DeleteAllItems()
1260 FreeAllInternalData();
1261 return ListView_DeleteAllItems(GetHwnd()) != 0;
1264 // Deletes all items
1265 bool wxListCtrl::DeleteAllColumns()
1267 while ( m_colCount
> 0 )
1269 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1271 wxLogLastError(wxT("ListView_DeleteColumn"));
1279 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1285 bool wxListCtrl::DeleteColumn(int col
)
1287 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1289 if ( success
&& (m_colCount
> 0) )
1294 // Clears items, and columns if there are any.
1295 void wxListCtrl::ClearAll()
1298 if ( m_colCount
> 0 )
1302 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1304 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1306 // ListView_EditLabel requires that the list has focus.
1309 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1312 // failed to start editing
1316 // [re]create the text control wrapping the HWND we got
1319 m_textCtrl
->UnsubclassWin();
1320 m_textCtrl
->SetHWND(0);
1324 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1325 m_textCtrl
->SetHWND(hWnd
);
1326 m_textCtrl
->SubclassWin(hWnd
);
1327 m_textCtrl
->SetParent(this);
1329 // we must disallow TABbing away from the control while the edit contol is
1330 // shown because this leaves it in some strange state (just try removing
1331 // this line and then pressing TAB while editing an item in listctrl
1333 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1338 // End label editing, optionally cancelling the edit
1339 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1341 wxFAIL_MSG( _T("not implemented") );
1346 // Ensures this item is visible
1347 bool wxListCtrl::EnsureVisible(long item
)
1349 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1352 // Find an item whose label matches this string, starting from the item after 'start'
1353 // or the beginning if 'start' is -1.
1354 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1356 LV_FINDINFO findInfo
;
1358 findInfo
.flags
= LVFI_STRING
;
1360 findInfo
.flags
|= LVFI_PARTIAL
;
1363 // ListView_FindItem() excludes the first item from search and to look
1364 // through all the items you need to start from -1 which is unnatural and
1365 // inconsistent with the generic version - so we adjust the index
1368 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1371 // Find an item whose data matches this data, starting from the item after 'start'
1372 // or the beginning if 'start' is -1.
1373 // NOTE : Lindsay Mathieson - 14-July-2002
1374 // No longer use ListView_FindItem as the data attribute is now stored
1375 // in a wxListItemInternalData structure refernced by the actual lParam
1376 long wxListCtrl::FindItem(long start
, long data
)
1378 long idx
= start
+ 1;
1379 long count
= GetItemCount();
1383 if (GetItemData(idx
) == data
)
1391 // Find an item nearest this position in the specified direction, starting from
1392 // the item after 'start' or the beginning if 'start' is -1.
1393 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1395 LV_FINDINFO findInfo
;
1397 findInfo
.flags
= LVFI_NEARESTXY
;
1398 findInfo
.pt
.x
= pt
.x
;
1399 findInfo
.pt
.y
= pt
.y
;
1400 findInfo
.vkDirection
= VK_RIGHT
;
1402 if ( direction
== wxLIST_FIND_UP
)
1403 findInfo
.vkDirection
= VK_UP
;
1404 else if ( direction
== wxLIST_FIND_DOWN
)
1405 findInfo
.vkDirection
= VK_DOWN
;
1406 else if ( direction
== wxLIST_FIND_LEFT
)
1407 findInfo
.vkDirection
= VK_LEFT
;
1408 else if ( direction
== wxLIST_FIND_RIGHT
)
1409 findInfo
.vkDirection
= VK_RIGHT
;
1411 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1414 // Determines which item (if any) is at the specified point,
1415 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1416 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1418 LV_HITTESTINFO hitTestInfo
;
1419 hitTestInfo
.pt
.x
= (int) point
.x
;
1420 hitTestInfo
.pt
.y
= (int) point
.y
;
1422 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1425 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1426 flags
|= wxLIST_HITTEST_ABOVE
;
1427 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1428 flags
|= wxLIST_HITTEST_BELOW
;
1429 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1430 flags
|= wxLIST_HITTEST_NOWHERE
;
1431 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1432 flags
|= wxLIST_HITTEST_ONITEMICON
;
1433 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1434 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1435 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1436 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1437 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1438 flags
|= wxLIST_HITTEST_TOLEFT
;
1439 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1440 flags
|= wxLIST_HITTEST_TORIGHT
;
1442 return (long) hitTestInfo
.iItem
;
1445 // Inserts an item, returning the index of the new item if successful,
1447 long wxListCtrl::InsertItem(wxListItem
& info
)
1449 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1452 wxConvertToMSWListItem(this, info
, item
);
1453 item
.mask
&= ~LVIF_PARAM
;
1455 // check wether we need to allocate our internal data
1456 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1457 if (needInternalData
)
1459 m_AnyInternalData
= true;
1460 item
.mask
|= LVIF_PARAM
;
1462 // internal stucture that manages data
1463 wxListItemInternalData
*data
= new wxListItemInternalData();
1464 item
.lParam
= (LPARAM
) data
;
1466 if (info
.m_mask
& wxLIST_MASK_DATA
)
1467 data
->lParam
= info
.m_data
;
1469 // check whether it has any custom attributes
1470 if ( info
.HasAttributes() )
1472 // take copy of attributes
1473 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1475 // and remember that we have some now...
1476 m_hasAnyAttr
= true;
1480 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1483 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1484 wxT("m_count should match ListView_GetItemCount"));
1489 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1492 info
.m_text
= label
;
1493 info
.m_mask
= wxLIST_MASK_TEXT
;
1494 info
.m_itemId
= index
;
1495 return InsertItem(info
);
1498 // Inserts an image item
1499 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1502 info
.m_image
= imageIndex
;
1503 info
.m_mask
= wxLIST_MASK_IMAGE
;
1504 info
.m_itemId
= index
;
1505 return InsertItem(info
);
1508 // Inserts an image/string item
1509 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1512 info
.m_image
= imageIndex
;
1513 info
.m_text
= label
;
1514 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1515 info
.m_itemId
= index
;
1516 return InsertItem(info
);
1519 // For list view mode (only), inserts a column.
1520 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1523 wxConvertToMSWListCol(col
, item
, lvCol
);
1525 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1527 // always give some width to the new column: this one is compatible
1528 // with the generic version
1529 lvCol
.mask
|= LVCF_WIDTH
;
1533 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1538 else // failed to insert?
1540 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1547 long wxListCtrl::InsertColumn(long col
,
1548 const wxString
& heading
,
1553 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1554 item
.m_text
= heading
;
1557 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1558 item
.m_width
= width
;
1560 item
.m_format
= format
;
1562 return InsertColumn(col
, item
);
1565 // scroll the control by the given number of pixels (exception: in list view,
1566 // dx is interpreted as number of columns)
1567 bool wxListCtrl::ScrollList(int dx
, int dy
)
1569 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1571 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1581 // fn is a function which takes 3 long arguments: item1, item2, data.
1582 // item1 is the long data associated with a first item (NOT the index).
1583 // item2 is the long data associated with a second item (NOT the index).
1584 // data is the same value as passed to SortItems.
1585 // The return value is a negative number if the first item should precede the second
1586 // item, a positive number of the second item should precede the first,
1587 // or zero if the two items are equivalent.
1589 // data is arbitrary data to be passed to the sort function.
1591 // Internal structures for proxying the user compare function
1592 // so that we can pass it the *real* user data
1594 // translate lParam data and call user func
1595 struct wxInternalDataSort
1597 wxListCtrlCompare user_fn
;
1601 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1603 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1605 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1606 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1608 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1609 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1611 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1615 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1617 struct wxInternalDataSort internalData
;
1618 internalData
.user_fn
= fn
;
1619 internalData
.data
= data
;
1621 // WPARAM cast is needed for mingw/cygwin
1622 if ( !ListView_SortItems(GetHwnd(),
1623 wxInternalDataCompareFunc
,
1624 (WPARAM
) &internalData
) )
1626 wxLogDebug(_T("ListView_SortItems() failed"));
1636 // ----------------------------------------------------------------------------
1637 // message processing
1638 // ----------------------------------------------------------------------------
1640 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1642 if (cmd
== EN_UPDATE
)
1644 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1645 event
.SetEventObject( this );
1646 ProcessCommand(event
);
1649 else if (cmd
== EN_KILLFOCUS
)
1651 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1652 event
.SetEventObject( this );
1653 ProcessCommand(event
);
1660 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1663 // prepare the event
1664 // -----------------
1666 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1667 event
.SetEventObject(this);
1669 wxEventType eventType
= wxEVT_NULL
;
1671 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1673 // if your compiler is as broken as this, you should really change it: this
1674 // code is needed for normal operation! #ifdef below is only useful for
1675 // automatic rebuilds which are done with a very old compiler version
1676 #ifdef HDN_BEGINTRACKA
1678 // check for messages from the header (in report view)
1679 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1681 // is it a message from the header?
1682 if ( nmhdr
->hwndFrom
== hwndHdr
)
1684 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1686 event
.m_itemIndex
= -1;
1688 switch ( nmhdr
->code
)
1690 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1691 // TRACK messages even to ANSI programs: on my system I get
1692 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1694 // work around is to simply catch both versions and hope that it
1695 // works (why should this message exist in ANSI and Unicode is
1696 // beyond me as it doesn't deal with strings at all...)
1698 // note that fr HDN_TRACK another possibility could be to use
1699 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1700 // something other than the item width changes so we'd have to
1701 // filter out the unwanted events then
1702 case HDN_BEGINTRACKA
:
1703 case HDN_BEGINTRACKW
:
1704 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1709 if ( eventType
== wxEVT_NULL
)
1710 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1715 if ( eventType
== wxEVT_NULL
)
1716 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1718 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1719 event
.m_col
= nmHDR
->iItem
;
1722 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1723 case GN_CONTEXTMENU
:
1724 #endif //__WXWINCE__
1727 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1730 // find the column clicked: we have to search for it
1731 // ourselves as the notification message doesn't provide
1734 // where did the click occur?
1736 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1737 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1738 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1740 #endif //__WXWINCE__
1741 if ( !::GetCursorPos(&ptClick
) )
1743 wxLogLastError(_T("GetCursorPos"));
1746 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1748 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1751 event
.m_pointDrag
.x
= ptClick
.x
;
1752 event
.m_pointDrag
.y
= ptClick
.y
;
1754 int colCount
= Header_GetItemCount(hwndHdr
);
1757 for ( int col
= 0; col
< colCount
; col
++ )
1759 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1761 if ( ::PtInRect(&rect
, ptClick
) )
1771 case HDN_GETDISPINFOW
:
1773 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1774 // This is a fix for a strange bug under XP.
1775 // Normally, info->iItem is a valid index, but
1776 // sometimes this is a silly (large) number
1777 // and when we return false via wxControl::MSWOnNotify
1778 // to indicate that it hasn't yet been processed,
1779 // there's a GPF in Windows.
1780 // By returning true here, we avoid further processing
1781 // of this strange message.
1782 if ( (unsigned)info
->iItem
>= (unsigned)GetColumnCount() )
1788 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1792 #endif // defined(HDN_BEGINTRACKA)
1793 if ( nmhdr
->hwndFrom
== GetHwnd() )
1795 // almost all messages use NM_LISTVIEW
1796 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1798 const int iItem
= nmLV
->iItem
;
1801 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1802 // ignored for efficiency. It is done here because the internal data is in the
1803 // process of being deleted so we don't want to try and access it below.
1804 if ( m_ignoreChangeMessages
&&
1805 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1811 // If we have a valid item then check if there is a data value
1812 // associated with it and put it in the event.
1813 if ( iItem
>= 0 && iItem
< GetItemCount() )
1815 wxListItemInternalData
*internaldata
=
1816 wxGetInternalData(GetHwnd(), iItem
);
1819 event
.m_item
.m_data
= internaldata
->lParam
;
1823 switch ( nmhdr
->code
)
1825 case LVN_BEGINRDRAG
:
1826 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1830 if ( eventType
== wxEVT_NULL
)
1832 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1835 event
.m_itemIndex
= iItem
;
1836 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1837 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1840 // NB: we have to handle both *A and *W versions here because some
1841 // versions of comctl32.dll send ANSI messages even to the
1843 case LVN_BEGINLABELEDITA
:
1844 case LVN_BEGINLABELEDITW
:
1847 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1849 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1851 else // LVN_BEGINLABELEDITW
1853 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1856 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1857 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1858 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1862 case LVN_ENDLABELEDITA
:
1863 case LVN_ENDLABELEDITW
:
1866 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1868 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1870 else // LVN_ENDLABELEDITW
1872 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1875 // was editing cancelled?
1876 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1877 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1879 // don't keep a stale wxTextCtrl around
1882 // EDIT control will be deleted by the list control itself so
1883 // prevent us from deleting it as well
1884 m_textCtrl
->UnsubclassWin();
1885 m_textCtrl
->SetHWND(0);
1890 event
.SetEditCanceled(true);
1893 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1894 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1895 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1899 case LVN_COLUMNCLICK
:
1900 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1901 event
.m_itemIndex
= -1;
1902 event
.m_col
= nmLV
->iSubItem
;
1905 case LVN_DELETEALLITEMS
:
1907 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1908 event
.m_itemIndex
= -1;
1911 case LVN_DELETEITEM
:
1913 // this should be prevented by the post-processing code below,
1914 // but "just in case"
1917 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1918 event
.m_itemIndex
= iItem
;
1919 // delete the assoicated internal data
1920 wxDeleteInternalData(this, iItem
);
1923 case LVN_SETDISPINFO
:
1925 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1926 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1927 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1931 case LVN_INSERTITEM
:
1932 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1933 event
.m_itemIndex
= iItem
;
1936 case LVN_ITEMCHANGED
:
1937 // we translate this catch all message into more interesting
1938 // (and more easy to process) wxWidgets events
1940 // first of all, we deal with the state change events only and
1941 // only for valid items (item == -1 for the virtual list
1943 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1945 // temp vars for readability
1946 const UINT stOld
= nmLV
->uOldState
;
1947 const UINT stNew
= nmLV
->uNewState
;
1949 event
.m_item
.SetId(iItem
);
1950 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1953 GetItem(event
.m_item
);
1955 // has the focus changed?
1956 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1958 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1959 event
.m_itemIndex
= iItem
;
1962 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1964 if ( eventType
!= wxEVT_NULL
)
1966 // focus and selection have both changed: send the
1967 // focus event from here and the selection one
1969 event
.SetEventType(eventType
);
1970 (void)GetEventHandler()->ProcessEvent(event
);
1972 else // no focus event to send
1974 // then need to set m_itemIndex as it wasn't done
1976 event
.m_itemIndex
= iItem
;
1979 eventType
= stNew
& LVIS_SELECTED
1980 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1981 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1985 if ( eventType
== wxEVT_NULL
)
1987 // not an interesting event for us
1995 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1996 WORD wVKey
= info
->wVKey
;
1998 // get the current selection
1999 long lItem
= GetNextItem(-1,
2001 wxLIST_STATE_SELECTED
);
2003 // <Enter> or <Space> activate the selected item if any (but
2004 // not with Shift and/or Ctrl as then they have a predefined
2005 // meaning for the list view)
2007 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2008 !(wxIsShiftDown() || wxIsCtrlDown()) )
2010 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2014 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2016 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2017 // value which should be used as is
2018 int code
= wxCharCodeMSWToWX(wVKey
);
2019 event
.m_code
= code
? code
: wVKey
;
2023 event
.m_item
.m_itemId
= lItem
;
2027 // fill the other fields too
2028 event
.m_item
.m_text
= GetItemText(lItem
);
2029 event
.m_item
.m_data
= GetItemData(lItem
);
2035 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2037 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2042 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2043 // if it happened on an item (and not on empty place)
2050 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2051 event
.m_itemIndex
= iItem
;
2052 event
.m_item
.m_text
= GetItemText(iItem
);
2053 event
.m_item
.m_data
= GetItemData(iItem
);
2056 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2057 case GN_CONTEXTMENU
:
2058 #endif //__WXWINCE__
2060 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2061 // don't do anything else
2062 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2067 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2068 LV_HITTESTINFO lvhti
;
2069 wxZeroMemory(lvhti
);
2071 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2072 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2073 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2075 #endif //__WXWINCE__
2076 ::GetCursorPos(&(lvhti
.pt
));
2077 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2078 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2080 if ( lvhti
.flags
& LVHT_ONITEM
)
2082 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2083 event
.m_itemIndex
= lvhti
.iItem
;
2084 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2085 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2090 #ifdef NM_CUSTOMDRAW
2092 *result
= OnCustomDraw(lParam
);
2095 #endif // _WIN32_IE >= 0x300
2097 case LVN_ODCACHEHINT
:
2099 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2101 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2103 // we get some really stupid cache hints like ones for
2104 // items in range 0..0 for an empty control or, after
2105 // deleting an item, for items in invalid range -- filter
2107 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2110 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2112 const long iMax
= GetItemCount();
2113 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2118 case LVN_GETDISPINFO
:
2121 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2123 LV_ITEM
& lvi
= info
->item
;
2124 long item
= lvi
.iItem
;
2126 if ( lvi
.mask
& LVIF_TEXT
)
2128 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2129 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2132 // see comment at the end of wxListCtrl::GetColumn()
2133 #ifdef NM_CUSTOMDRAW
2134 if ( lvi
.mask
& LVIF_IMAGE
)
2136 lvi
.iImage
= OnGetItemImage(item
);
2138 #endif // NM_CUSTOMDRAW
2140 // a little dose of healthy paranoia: as we never use
2141 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2142 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2143 _T("we don't support state callbacks yet!") );
2150 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2155 // where did this one come from?
2159 // process the event
2160 // -----------------
2162 event
.SetEventType(eventType
);
2164 bool processed
= GetEventHandler()->ProcessEvent(event
);
2168 switch ( nmhdr
->code
)
2170 case LVN_DELETEALLITEMS
:
2171 // always return true to suppress all additional LVN_DELETEITEM
2172 // notifications - this makes deleting all items from a list ctrl
2177 case LVN_ENDLABELEDITA
:
2178 case LVN_ENDLABELEDITW
:
2179 // logic here is inversed compared to all the other messages
2180 *result
= event
.IsAllowed();
2182 // don't keep a stale wxTextCtrl around
2185 // EDIT control will be deleted by the list control itself so
2186 // prevent us from deleting it as well
2187 m_textCtrl
->UnsubclassWin();
2188 m_textCtrl
->SetHWND(0);
2197 *result
= !event
.IsAllowed();
2202 // see comment at the end of wxListCtrl::GetColumn()
2203 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2205 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2207 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2208 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2209 switch ( nmcd
.dwDrawStage
)
2212 // if we've got any items with non standard attributes,
2213 // notify us before painting each item
2215 // for virtual controls, always suppose that we have attributes as
2216 // there is no way to check for this
2217 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2220 case CDDS_ITEMPREPAINT
:
2222 size_t item
= (size_t)nmcd
.dwItemSpec
;
2223 if ( item
>= (size_t)GetItemCount() )
2225 // we get this message with item == 0 for an empty control,
2226 // we must ignore it as calling OnGetItemAttr() would be
2228 return CDRF_DODEFAULT
;
2231 wxListItemAttr
*attr
=
2232 IsVirtual() ? OnGetItemAttr(item
)
2233 : wxGetInternalDataAttr(this, item
);
2237 // nothing to do for this item
2238 return CDRF_DODEFAULT
;
2242 wxColour colText
, colBack
;
2243 if ( attr
->HasFont() )
2245 wxFont font
= attr
->GetFont();
2246 hFont
= (HFONT
)font
.GetResourceHandle();
2253 if ( attr
->HasTextColour() )
2255 colText
= attr
->GetTextColour();
2259 colText
= GetTextColour();
2262 if ( attr
->HasBackgroundColour() )
2264 colBack
= attr
->GetBackgroundColour();
2268 colBack
= GetBackgroundColour();
2271 lplvcd
->clrText
= wxColourToRGB(colText
);
2272 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2274 // note that if we wanted to set colours for
2275 // individual columns (subitems), we would have
2276 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2279 ::SelectObject(nmcd
.hdc
, hFont
);
2281 return CDRF_NEWFONT
;
2284 // fall through to return CDRF_DODEFAULT
2287 return CDRF_DODEFAULT
;
2291 #endif // NM_CUSTOMDRAW supported
2293 // Necessary for drawing hrules and vrules, if specified
2294 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2298 wxControl::OnPaint(event
);
2300 // Reset the device origin since it may have been set
2301 dc
.SetDeviceOrigin(0, 0);
2303 bool drawHRules
= HasFlag(wxLC_HRULES
);
2304 bool drawVRules
= HasFlag(wxLC_VRULES
);
2306 if (!InReportView() || !drawHRules
&& !drawVRules
)
2309 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2311 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2313 wxSize clientSize
= GetClientSize();
2316 int itemCount
= GetItemCount();
2320 long top
= GetTopItem();
2321 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2323 if (GetItemRect(i
, itemRect
))
2325 int cy
= itemRect
.GetTop();
2326 if (i
!= 0) // Don't draw the first one
2328 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2331 if (i
== itemCount
- 1)
2333 cy
= itemRect
.GetBottom();
2334 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2340 if (drawVRules
&& (i
> -1))
2342 wxRect firstItemRect
;
2343 GetItemRect(0, firstItemRect
);
2345 if (GetItemRect(i
, itemRect
))
2348 int x
= itemRect
.GetX();
2349 for (col
= 0; col
< GetColumnCount(); col
++)
2351 int colWidth
= GetColumnWidth(col
);
2353 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2359 // ----------------------------------------------------------------------------
2360 // virtual list controls
2361 // ----------------------------------------------------------------------------
2363 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2365 // this is a pure virtual function, in fact - which is not really pure
2366 // because the controls which are not virtual don't need to implement it
2367 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2369 return wxEmptyString
;
2372 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2375 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2380 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2382 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2383 _T("invalid item index in OnGetItemAttr()") );
2385 // no attributes by default
2389 void wxListCtrl::SetItemCount(long count
)
2391 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2393 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2394 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2396 wxLogLastError(_T("ListView_SetItemCount"));
2399 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2400 wxT("m_count should match ListView_GetItemCount"));
2403 void wxListCtrl::RefreshItem(long item
)
2405 // strangely enough, ListView_Update() results in much more flicker here
2406 // than a dumb Refresh() -- why?
2408 if ( !ListView_Update(GetHwnd(), item
) )
2410 wxLogLastError(_T("ListView_Update"));
2414 GetItemRect(item
, rect
);
2419 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2421 wxRect rect1
, rect2
;
2422 GetItemRect(itemFrom
, rect1
);
2423 GetItemRect(itemTo
, rect2
);
2425 wxRect rect
= rect1
;
2426 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2431 // ----------------------------------------------------------------------------
2432 // internal data stuff
2433 // ----------------------------------------------------------------------------
2435 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2438 it
.mask
= LVIF_PARAM
;
2441 if ( !ListView_GetItem(hwnd
, &it
) )
2444 return (wxListItemInternalData
*) it
.lParam
;
2448 wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
)
2450 return wxGetInternalData(GetHwndOf(ctl
), itemId
);
2453 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2455 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2457 return data
? data
->attr
: NULL
;
2460 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2462 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2466 memset(&item
, 0, sizeof(item
));
2467 item
.iItem
= itemId
;
2468 item
.mask
= LVIF_PARAM
;
2469 item
.lParam
= (LPARAM
) 0;
2470 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2475 // ----------------------------------------------------------------------------
2476 // wxWin <-> MSW items conversions
2477 // ----------------------------------------------------------------------------
2479 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2483 wxListItemInternalData
*internaldata
=
2484 (wxListItemInternalData
*) lvItem
.lParam
;
2487 info
.m_data
= internaldata
->lParam
;
2491 info
.m_stateMask
= 0;
2492 info
.m_itemId
= lvItem
.iItem
;
2494 long oldMask
= lvItem
.mask
;
2496 bool needText
= false;
2497 if (hwndListCtrl
!= 0)
2499 if ( lvItem
.mask
& LVIF_TEXT
)
2506 lvItem
.pszText
= new wxChar
[513];
2507 lvItem
.cchTextMax
= 512;
2509 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2510 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2513 if ( lvItem
.mask
& LVIF_STATE
)
2515 info
.m_mask
|= wxLIST_MASK_STATE
;
2517 if ( lvItem
.stateMask
& LVIS_CUT
)
2519 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2520 if ( lvItem
.state
& LVIS_CUT
)
2521 info
.m_state
|= wxLIST_STATE_CUT
;
2523 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2525 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2526 if ( lvItem
.state
& LVIS_DROPHILITED
)
2527 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2529 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2531 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2532 if ( lvItem
.state
& LVIS_FOCUSED
)
2533 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2535 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2537 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2538 if ( lvItem
.state
& LVIS_SELECTED
)
2539 info
.m_state
|= wxLIST_STATE_SELECTED
;
2543 if ( lvItem
.mask
& LVIF_TEXT
)
2545 info
.m_mask
|= wxLIST_MASK_TEXT
;
2546 info
.m_text
= lvItem
.pszText
;
2548 if ( lvItem
.mask
& LVIF_IMAGE
)
2550 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2551 info
.m_image
= lvItem
.iImage
;
2553 if ( lvItem
.mask
& LVIF_PARAM
)
2554 info
.m_mask
|= wxLIST_MASK_DATA
;
2555 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2556 info
.m_mask
|= wxLIST_SET_ITEM
;
2557 info
.m_col
= lvItem
.iSubItem
;
2562 delete[] lvItem
.pszText
;
2564 lvItem
.mask
= oldMask
;
2567 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2569 if (stateMask
& wxLIST_STATE_CUT
)
2571 lvItem
.stateMask
|= LVIS_CUT
;
2572 if (state
& wxLIST_STATE_CUT
)
2573 lvItem
.state
|= LVIS_CUT
;
2575 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2577 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2578 if (state
& wxLIST_STATE_DROPHILITED
)
2579 lvItem
.state
|= LVIS_DROPHILITED
;
2581 if (stateMask
& wxLIST_STATE_FOCUSED
)
2583 lvItem
.stateMask
|= LVIS_FOCUSED
;
2584 if (state
& wxLIST_STATE_FOCUSED
)
2585 lvItem
.state
|= LVIS_FOCUSED
;
2587 if (stateMask
& wxLIST_STATE_SELECTED
)
2589 lvItem
.stateMask
|= LVIS_SELECTED
;
2590 if (state
& wxLIST_STATE_SELECTED
)
2591 lvItem
.state
|= LVIS_SELECTED
;
2595 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2596 const wxListItem
& info
,
2599 lvItem
.iItem
= (int) info
.m_itemId
;
2601 lvItem
.iImage
= info
.m_image
;
2602 lvItem
.stateMask
= 0;
2605 lvItem
.iSubItem
= info
.m_col
;
2607 if (info
.m_mask
& wxLIST_MASK_STATE
)
2609 lvItem
.mask
|= LVIF_STATE
;
2611 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2614 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2616 lvItem
.mask
|= LVIF_TEXT
;
2617 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
2619 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2623 // pszText is not const, hence the cast
2624 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2625 if ( lvItem
.pszText
)
2626 lvItem
.cchTextMax
= info
.m_text
.Length();
2628 lvItem
.cchTextMax
= 0;
2631 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2632 lvItem
.mask
|= LVIF_IMAGE
;
2635 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2638 wxZeroMemory(lvCol
);
2640 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2642 lvCol
.mask
|= LVCF_TEXT
;
2643 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2646 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2648 lvCol
.mask
|= LVCF_FMT
;
2650 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2651 lvCol
.fmt
= LVCFMT_LEFT
;
2652 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2653 lvCol
.fmt
= LVCFMT_RIGHT
;
2654 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2655 lvCol
.fmt
= LVCFMT_CENTER
;
2658 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2660 lvCol
.mask
|= LVCF_WIDTH
;
2661 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2662 lvCol
.cx
= LVSCW_AUTOSIZE
;
2663 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2664 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2666 lvCol
.cx
= item
.m_width
;
2669 // see comment at the end of wxListCtrl::GetColumn()
2670 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2671 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2673 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2675 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2677 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2678 // seem to be generally nicer than on the left and the generic
2679 // version only draws them on the right (we don't have a flag to
2680 // specify the image location anyhow)
2682 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2683 // make any difference in my tests -- but maybe we should?
2684 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2686 lvCol
.iImage
= item
.m_image
;
2688 //else: it doesn't support item images anyhow
2690 #endif // _WIN32_IE >= 0x0300
2693 #endif // wxUSE_LISTCTRL