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 #if WXWIN_COMPATIBILITY_2_4
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
237 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
238 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
239 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
240 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
241 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
243 #if wxUSE_EXTENDED_RTTI
244 WX_DEFINE_FLAGS( wxListCtrlStyle
)
246 wxBEGIN_FLAGS( wxListCtrlStyle
)
247 // new style border flags, we put them first to
248 // use them for streaming out
249 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
250 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
251 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
252 wxFLAGS_MEMBER(wxBORDER_RAISED
)
253 wxFLAGS_MEMBER(wxBORDER_STATIC
)
254 wxFLAGS_MEMBER(wxBORDER_NONE
)
256 // old style border flags
257 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
258 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
259 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
260 wxFLAGS_MEMBER(wxRAISED_BORDER
)
261 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
262 wxFLAGS_MEMBER(wxBORDER
)
264 // standard window styles
265 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
266 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
267 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
268 wxFLAGS_MEMBER(wxWANTS_CHARS
)
269 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
270 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
271 wxFLAGS_MEMBER(wxVSCROLL
)
272 wxFLAGS_MEMBER(wxHSCROLL
)
274 wxFLAGS_MEMBER(wxLC_LIST
)
275 wxFLAGS_MEMBER(wxLC_REPORT
)
276 wxFLAGS_MEMBER(wxLC_ICON
)
277 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
278 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
279 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
280 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
281 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
282 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
283 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
284 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
285 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
286 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
287 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
289 wxEND_FLAGS( wxListCtrlStyle
)
291 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
293 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
294 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
296 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
297 wxEND_PROPERTIES_TABLE()
299 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
300 wxEND_HANDLERS_TABLE()
302 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
305 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
308 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
311 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
312 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
314 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
316 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
317 EVT_PAINT(wxListCtrl::OnPaint
)
320 // ============================================================================
322 // ============================================================================
324 // ----------------------------------------------------------------------------
325 // wxListCtrl construction
326 // ----------------------------------------------------------------------------
328 void wxListCtrl::Init()
330 m_imageListNormal
= NULL
;
331 m_imageListSmall
= NULL
;
332 m_imageListState
= NULL
;
333 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= false;
336 m_ignoreChangeMessages
= false;
338 m_AnyInternalData
= false;
339 m_hasAnyAttr
= false;
342 bool wxListCtrl::Create(wxWindow
*parent
,
347 const wxValidator
& validator
,
348 const wxString
& name
)
350 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
353 if ( !MSWCreateControl(WC_LISTVIEW
, _T(""), pos
, size
) )
356 // explicitly say that we want to use Unicode because otherwise we get ANSI
357 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
358 wxSetCCUnicodeFormat(GetHwnd());
360 // for comctl32.dll v 4.70+ we want to have this attribute because it's
361 // prettier (and also because wxGTK does it like this)
362 if ( InReportView() && wxTheApp
->GetComCtl32Version() >= 470 )
364 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
365 0, LVS_EX_FULLROWSELECT
);
371 WXDWORD
wxListCtrl::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
373 WXDWORD wstyle
= wxControl::MSWGetStyle(style
, exstyle
);
375 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
380 #define MAP_MODE_STYLE(wx, ms) \
381 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
382 #else // !__WXDEBUG__
383 #define MAP_MODE_STYLE(wx, ms) \
384 if ( style & (wx) ) wstyle |= (ms);
385 #endif // __WXDEBUG__
387 MAP_MODE_STYLE(wxLC_ICON
, LVS_ICON
)
388 MAP_MODE_STYLE(wxLC_SMALL_ICON
, LVS_SMALLICON
)
389 MAP_MODE_STYLE(wxLC_LIST
, LVS_LIST
)
390 MAP_MODE_STYLE(wxLC_REPORT
, LVS_REPORT
)
392 wxASSERT_MSG( nModes
== 1,
393 _T("wxListCtrl style should have exactly one mode bit set") );
395 #undef MAP_MODE_STYLE
397 if ( style
& wxLC_ALIGN_LEFT
)
398 wstyle
|= LVS_ALIGNLEFT
;
400 if ( style
& wxLC_ALIGN_TOP
)
401 wstyle
|= LVS_ALIGNTOP
;
403 if ( style
& wxLC_AUTOARRANGE
)
404 wstyle
|= LVS_AUTOARRANGE
;
406 if ( style
& wxLC_NO_SORT_HEADER
)
407 wstyle
|= LVS_NOSORTHEADER
;
409 if ( style
& wxLC_NO_HEADER
)
410 wstyle
|= LVS_NOCOLUMNHEADER
;
412 if ( style
& wxLC_EDIT_LABELS
)
413 wstyle
|= LVS_EDITLABELS
;
415 if ( style
& wxLC_SINGLE_SEL
)
416 wstyle
|= LVS_SINGLESEL
;
418 if ( style
& wxLC_SORT_ASCENDING
)
420 wstyle
|= LVS_SORTASCENDING
;
422 wxASSERT_MSG( !(style
& wxLC_SORT_DESCENDING
),
423 _T("can't sort in ascending and descending orders at once") );
425 else if ( style
& wxLC_SORT_DESCENDING
)
426 wstyle
|= LVS_SORTDESCENDING
;
428 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
429 if ( style
& wxLC_VIRTUAL
)
431 int ver
= wxTheApp
->GetComCtl32Version();
434 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."),
435 ver
/ 100, ver
% 100);
438 wstyle
|= LVS_OWNERDATA
;
440 #endif // ancient cygwin
445 void wxListCtrl::UpdateStyle()
449 // The new window view style
450 DWORD dwStyleNew
= MSWGetStyle(m_windowStyle
, NULL
);
452 // some styles are not returned by MSWGetStyle()
454 dwStyleNew
|= WS_VISIBLE
;
456 // Get the current window style.
457 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
459 // we don't have wxVSCROLL style, but the list control may have it,
460 // don't change it then
461 dwStyleNew
|= dwStyleOld
& (WS_HSCROLL
| WS_VSCROLL
);
463 // Only set the window style if the view bits have changed.
464 if ( dwStyleOld
!= dwStyleNew
)
466 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
471 void wxListCtrl::FreeAllInternalData()
473 if (m_AnyInternalData
)
475 int n
= GetItemCount();
477 m_ignoreChangeMessages
= true;
478 for (int i
= 0; i
< n
; i
++)
479 wxDeleteInternalData(this, i
);
480 m_ignoreChangeMessages
= false;
482 m_AnyInternalData
= false;
486 wxListCtrl::~wxListCtrl()
488 FreeAllInternalData();
492 m_textCtrl
->UnsubclassWin();
493 m_textCtrl
->SetHWND(0);
498 if (m_ownsImageListNormal
) delete m_imageListNormal
;
499 if (m_ownsImageListSmall
) delete m_imageListSmall
;
500 if (m_ownsImageListState
) delete m_imageListState
;
503 // ----------------------------------------------------------------------------
504 // set/get/change style
505 // ----------------------------------------------------------------------------
507 // Add or remove a single window style
508 void wxListCtrl::SetSingleStyle(long style
, bool add
)
510 long flag
= GetWindowStyleFlag();
512 // Get rid of conflicting styles
515 if ( style
& wxLC_MASK_TYPE
)
516 flag
= flag
& ~wxLC_MASK_TYPE
;
517 if ( style
& wxLC_MASK_ALIGN
)
518 flag
= flag
& ~wxLC_MASK_ALIGN
;
519 if ( style
& wxLC_MASK_SORT
)
520 flag
= flag
& ~wxLC_MASK_SORT
;
528 SetWindowStyleFlag(flag
);
531 // Set the whole window style
532 void wxListCtrl::SetWindowStyleFlag(long flag
)
534 if ( flag
!= m_windowStyle
)
536 m_windowStyle
= flag
;
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 /* static */ wxVisualAttributes
549 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
551 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
553 // common controls have their own default font
554 attrs
.font
= wxGetCCDefaultFont();
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 wxUIntPtr
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 wxRect
wxListCtrl::GetViewRect() const
969 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
970 _T("wxListCtrl::GetViewRect() only works in icon mode") );
973 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
975 wxLogDebug(_T("ListView_GetViewRect() failed."));
981 wxCopyRECTToRect(rc
, rect
);
986 // Gets the item rectangle
987 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
992 if ( code
== wxLIST_RECT_BOUNDS
)
993 codeWin
= LVIR_BOUNDS
;
994 else if ( code
== wxLIST_RECT_ICON
)
996 else if ( code
== wxLIST_RECT_LABEL
)
997 codeWin
= LVIR_LABEL
;
1000 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1002 codeWin
= LVIR_BOUNDS
;
1005 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1007 rect
.x
= rectWin
.left
;
1008 rect
.y
= rectWin
.top
;
1009 rect
.width
= rectWin
.right
- rectWin
.left
;
1010 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1015 // Gets the item position
1016 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1020 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1022 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1026 // Sets the item position.
1027 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1029 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1032 // Gets the number of items in the list control
1033 int wxListCtrl::GetItemCount() const
1038 wxSize
wxListCtrl::GetItemSpacing() const
1040 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1042 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1045 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1047 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1050 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1053 info
.m_itemId
= item
;
1054 info
.SetTextColour( col
);
1058 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1061 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1062 if ( data
&& data
->attr
)
1063 col
= data
->attr
->GetTextColour();
1068 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1071 info
.m_itemId
= item
;
1072 info
.SetBackgroundColour( col
);
1076 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1079 wxListItemInternalData
*data
= wxGetInternalData(this, item
);
1080 if ( data
&& data
->attr
)
1081 col
= data
->attr
->GetBackgroundColour();
1086 // Gets the number of selected items in the list control
1087 int wxListCtrl::GetSelectedItemCount() const
1089 return ListView_GetSelectedCount(GetHwnd());
1092 // Gets the text colour of the listview
1093 wxColour
wxListCtrl::GetTextColour() const
1095 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1096 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1100 // Sets the text colour of the listview
1101 void wxListCtrl::SetTextColour(const wxColour
& col
)
1103 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1106 // Gets the index of the topmost visible item when in
1107 // list or report view
1108 long wxListCtrl::GetTopItem() const
1110 return (long) ListView_GetTopIndex(GetHwnd());
1113 // Searches for an item, starting from 'item'.
1114 // 'geometry' is one of
1115 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1116 // 'state' is a state bit flag, one or more of
1117 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1118 // item can be -1 to find the first item that matches the
1120 // Returns the item or -1 if unsuccessful.
1121 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1125 if ( geom
== wxLIST_NEXT_ABOVE
)
1126 flags
|= LVNI_ABOVE
;
1127 if ( geom
== wxLIST_NEXT_ALL
)
1129 if ( geom
== wxLIST_NEXT_BELOW
)
1130 flags
|= LVNI_BELOW
;
1131 if ( geom
== wxLIST_NEXT_LEFT
)
1132 flags
|= LVNI_TOLEFT
;
1133 if ( geom
== wxLIST_NEXT_RIGHT
)
1134 flags
|= LVNI_TORIGHT
;
1136 if ( state
& wxLIST_STATE_CUT
)
1138 if ( state
& wxLIST_STATE_DROPHILITED
)
1139 flags
|= LVNI_DROPHILITED
;
1140 if ( state
& wxLIST_STATE_FOCUSED
)
1141 flags
|= LVNI_FOCUSED
;
1142 if ( state
& wxLIST_STATE_SELECTED
)
1143 flags
|= LVNI_SELECTED
;
1145 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1149 wxImageList
*wxListCtrl::GetImageList(int which
) const
1151 if ( which
== wxIMAGE_LIST_NORMAL
)
1153 return m_imageListNormal
;
1155 else if ( which
== wxIMAGE_LIST_SMALL
)
1157 return m_imageListSmall
;
1159 else if ( which
== wxIMAGE_LIST_STATE
)
1161 return m_imageListState
;
1166 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1169 if ( which
== wxIMAGE_LIST_NORMAL
)
1171 flags
= LVSIL_NORMAL
;
1172 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1173 m_imageListNormal
= imageList
;
1174 m_ownsImageListNormal
= false;
1176 else if ( which
== wxIMAGE_LIST_SMALL
)
1178 flags
= LVSIL_SMALL
;
1179 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1180 m_imageListSmall
= imageList
;
1181 m_ownsImageListSmall
= false;
1183 else if ( which
== wxIMAGE_LIST_STATE
)
1185 flags
= LVSIL_STATE
;
1186 if (m_ownsImageListState
) delete m_imageListState
;
1187 m_imageListState
= imageList
;
1188 m_ownsImageListState
= false;
1190 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1193 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1195 SetImageList(imageList
, which
);
1196 if ( which
== wxIMAGE_LIST_NORMAL
)
1197 m_ownsImageListNormal
= true;
1198 else if ( which
== wxIMAGE_LIST_SMALL
)
1199 m_ownsImageListSmall
= true;
1200 else if ( which
== wxIMAGE_LIST_STATE
)
1201 m_ownsImageListState
= true;
1204 // ----------------------------------------------------------------------------
1206 // ----------------------------------------------------------------------------
1208 // Arranges the items
1209 bool wxListCtrl::Arrange(int flag
)
1212 if ( flag
== wxLIST_ALIGN_LEFT
)
1213 code
= LVA_ALIGNLEFT
;
1214 else if ( flag
== wxLIST_ALIGN_TOP
)
1215 code
= LVA_ALIGNTOP
;
1216 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1218 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1219 code
= LVA_SNAPTOGRID
;
1221 return (ListView_Arrange(GetHwnd(), code
) != 0);
1225 bool wxListCtrl::DeleteItem(long item
)
1227 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1229 wxLogLastError(_T("ListView_DeleteItem"));
1234 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1235 wxT("m_count should match ListView_GetItemCount"));
1237 // the virtual list control doesn't refresh itself correctly, help it
1240 // we need to refresh all the lines below the one which was deleted
1242 if ( item
> 0 && GetItemCount() )
1244 GetItemRect(item
- 1, rectItem
);
1249 rectItem
.height
= 0;
1252 wxRect rectWin
= GetRect();
1253 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1254 rectWin
.y
= rectItem
.GetBottom();
1256 RefreshRect(rectWin
);
1262 // Deletes all items
1263 bool wxListCtrl::DeleteAllItems()
1265 return ListView_DeleteAllItems(GetHwnd()) != 0;
1268 // Deletes all items
1269 bool wxListCtrl::DeleteAllColumns()
1271 while ( m_colCount
> 0 )
1273 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1275 wxLogLastError(wxT("ListView_DeleteColumn"));
1283 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1289 bool wxListCtrl::DeleteColumn(int col
)
1291 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1293 if ( success
&& (m_colCount
> 0) )
1298 // Clears items, and columns if there are any.
1299 void wxListCtrl::ClearAll()
1302 if ( m_colCount
> 0 )
1306 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1308 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1310 // ListView_EditLabel requires that the list has focus.
1313 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1316 // failed to start editing
1320 // [re]create the text control wrapping the HWND we got
1323 m_textCtrl
->UnsubclassWin();
1324 m_textCtrl
->SetHWND(0);
1328 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1329 m_textCtrl
->SetHWND(hWnd
);
1330 m_textCtrl
->SubclassWin(hWnd
);
1331 m_textCtrl
->SetParent(this);
1333 // we must disallow TABbing away from the control while the edit contol is
1334 // shown because this leaves it in some strange state (just try removing
1335 // this line and then pressing TAB while editing an item in listctrl
1337 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1342 // End label editing, optionally cancelling the edit
1343 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1345 wxFAIL_MSG( _T("not implemented") );
1350 // Ensures this item is visible
1351 bool wxListCtrl::EnsureVisible(long item
)
1353 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != FALSE
;
1356 // Find an item whose label matches this string, starting from the item after 'start'
1357 // or the beginning if 'start' is -1.
1358 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1360 LV_FINDINFO findInfo
;
1362 findInfo
.flags
= LVFI_STRING
;
1364 findInfo
.flags
|= LVFI_PARTIAL
;
1367 // ListView_FindItem() excludes the first item from search and to look
1368 // through all the items you need to start from -1 which is unnatural and
1369 // inconsistent with the generic version - so we adjust the index
1372 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1375 // Find an item whose data matches this data, starting from the item after 'start'
1376 // or the beginning if 'start' is -1.
1377 // NOTE : Lindsay Mathieson - 14-July-2002
1378 // No longer use ListView_FindItem as the data attribute is now stored
1379 // in a wxListItemInternalData structure refernced by the actual lParam
1380 long wxListCtrl::FindItem(long start
, wxUIntPtr data
)
1382 long idx
= start
+ 1;
1383 long count
= GetItemCount();
1387 if (GetItemData(idx
) == data
)
1395 // Find an item nearest this position in the specified direction, starting from
1396 // the item after 'start' or the beginning if 'start' is -1.
1397 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1399 LV_FINDINFO findInfo
;
1401 findInfo
.flags
= LVFI_NEARESTXY
;
1402 findInfo
.pt
.x
= pt
.x
;
1403 findInfo
.pt
.y
= pt
.y
;
1404 findInfo
.vkDirection
= VK_RIGHT
;
1406 if ( direction
== wxLIST_FIND_UP
)
1407 findInfo
.vkDirection
= VK_UP
;
1408 else if ( direction
== wxLIST_FIND_DOWN
)
1409 findInfo
.vkDirection
= VK_DOWN
;
1410 else if ( direction
== wxLIST_FIND_LEFT
)
1411 findInfo
.vkDirection
= VK_LEFT
;
1412 else if ( direction
== wxLIST_FIND_RIGHT
)
1413 findInfo
.vkDirection
= VK_RIGHT
;
1415 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1418 // Determines which item (if any) is at the specified point,
1419 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1420 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1422 LV_HITTESTINFO hitTestInfo
;
1423 hitTestInfo
.pt
.x
= (int) point
.x
;
1424 hitTestInfo
.pt
.y
= (int) point
.y
;
1426 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1430 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1431 flags
|= wxLIST_HITTEST_ABOVE
;
1432 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1433 flags
|= wxLIST_HITTEST_BELOW
;
1434 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1435 flags
|= wxLIST_HITTEST_TOLEFT
;
1436 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1437 flags
|= wxLIST_HITTEST_TORIGHT
;
1439 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1440 flags
|= wxLIST_HITTEST_NOWHERE
;
1442 // note a bug or at least a very strange feature of comtl32.dll (tested
1443 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1444 // the right of the item label, ListView_HitTest() returns a combination of
1445 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1446 // the bits which don't make sense
1447 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1449 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1451 // do not translate LVHT_ONITEMICON here, as per above
1455 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1456 flags
|= wxLIST_HITTEST_ONITEMICON
;
1457 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1458 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1461 return (long) hitTestInfo
.iItem
;
1464 // Inserts an item, returning the index of the new item if successful,
1466 long wxListCtrl::InsertItem(wxListItem
& info
)
1468 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1471 wxConvertToMSWListItem(this, info
, item
);
1472 item
.mask
&= ~LVIF_PARAM
;
1474 // check wether we need to allocate our internal data
1475 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1476 if (needInternalData
)
1478 m_AnyInternalData
= true;
1479 item
.mask
|= LVIF_PARAM
;
1481 // internal stucture that manages data
1482 wxListItemInternalData
*data
= new wxListItemInternalData();
1483 item
.lParam
= (LPARAM
) data
;
1485 if (info
.m_mask
& wxLIST_MASK_DATA
)
1486 data
->lParam
= info
.m_data
;
1488 // check whether it has any custom attributes
1489 if ( info
.HasAttributes() )
1491 // take copy of attributes
1492 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1494 // and remember that we have some now...
1495 m_hasAnyAttr
= true;
1499 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1502 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1503 wxT("m_count should match ListView_GetItemCount"));
1508 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1511 info
.m_text
= label
;
1512 info
.m_mask
= wxLIST_MASK_TEXT
;
1513 info
.m_itemId
= index
;
1514 return InsertItem(info
);
1517 // Inserts an image item
1518 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1521 info
.m_image
= imageIndex
;
1522 info
.m_mask
= wxLIST_MASK_IMAGE
;
1523 info
.m_itemId
= index
;
1524 return InsertItem(info
);
1527 // Inserts an image/string item
1528 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1531 info
.m_image
= imageIndex
;
1532 info
.m_text
= label
;
1533 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1534 info
.m_itemId
= index
;
1535 return InsertItem(info
);
1538 // For list view mode (only), inserts a column.
1539 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1542 wxConvertToMSWListCol(col
, item
, lvCol
);
1544 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1546 // always give some width to the new column: this one is compatible
1547 // with the generic version
1548 lvCol
.mask
|= LVCF_WIDTH
;
1552 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1557 else // failed to insert?
1559 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1566 long wxListCtrl::InsertColumn(long col
,
1567 const wxString
& heading
,
1572 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1573 item
.m_text
= heading
;
1576 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1577 item
.m_width
= width
;
1579 item
.m_format
= format
;
1581 return InsertColumn(col
, item
);
1584 // scroll the control by the given number of pixels (exception: in list view,
1585 // dx is interpreted as number of columns)
1586 bool wxListCtrl::ScrollList(int dx
, int dy
)
1588 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1590 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1600 // fn is a function which takes 3 long arguments: item1, item2, data.
1601 // item1 is the long data associated with a first item (NOT the index).
1602 // item2 is the long data associated with a second item (NOT the index).
1603 // data is the same value as passed to SortItems.
1604 // The return value is a negative number if the first item should precede the second
1605 // item, a positive number of the second item should precede the first,
1606 // or zero if the two items are equivalent.
1608 // data is arbitrary data to be passed to the sort function.
1610 // Internal structures for proxying the user compare function
1611 // so that we can pass it the *real* user data
1613 // translate lParam data and call user func
1614 struct wxInternalDataSort
1616 wxListCtrlCompare user_fn
;
1620 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1622 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1624 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1625 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1627 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1628 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1630 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1634 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1636 struct wxInternalDataSort internalData
;
1637 internalData
.user_fn
= fn
;
1638 internalData
.data
= data
;
1640 // WPARAM cast is needed for mingw/cygwin
1641 if ( !ListView_SortItems(GetHwnd(),
1642 wxInternalDataCompareFunc
,
1643 (WPARAM
) &internalData
) )
1645 wxLogDebug(_T("ListView_SortItems() failed"));
1655 // ----------------------------------------------------------------------------
1656 // message processing
1657 // ----------------------------------------------------------------------------
1659 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1661 if (cmd
== EN_UPDATE
)
1663 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1664 event
.SetEventObject( this );
1665 ProcessCommand(event
);
1668 else if (cmd
== EN_KILLFOCUS
)
1670 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1671 event
.SetEventObject( this );
1672 ProcessCommand(event
);
1679 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1682 // prepare the event
1683 // -----------------
1685 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1686 event
.SetEventObject(this);
1688 wxEventType eventType
= wxEVT_NULL
;
1690 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1692 // if your compiler is as broken as this, you should really change it: this
1693 // code is needed for normal operation! #ifdef below is only useful for
1694 // automatic rebuilds which are done with a very old compiler version
1695 #ifdef HDN_BEGINTRACKA
1697 // check for messages from the header (in report view)
1698 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1700 // is it a message from the header?
1701 if ( nmhdr
->hwndFrom
== hwndHdr
)
1703 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1705 event
.m_itemIndex
= -1;
1707 switch ( nmhdr
->code
)
1709 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1710 // TRACK messages even to ANSI programs: on my system I get
1711 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1713 // work around is to simply catch both versions and hope that it
1714 // works (why should this message exist in ANSI and Unicode is
1715 // beyond me as it doesn't deal with strings at all...)
1717 // note that fr HDN_TRACK another possibility could be to use
1718 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1719 // something other than the item width changes so we'd have to
1720 // filter out the unwanted events then
1721 case HDN_BEGINTRACKA
:
1722 case HDN_BEGINTRACKW
:
1723 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1728 if ( eventType
== wxEVT_NULL
)
1729 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1734 if ( eventType
== wxEVT_NULL
)
1735 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1737 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1738 event
.m_col
= nmHDR
->iItem
;
1741 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1742 case GN_CONTEXTMENU
:
1743 #endif //__WXWINCE__
1746 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1749 // find the column clicked: we have to search for it
1750 // ourselves as the notification message doesn't provide
1753 // where did the click occur?
1755 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1756 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1757 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1759 #endif //__WXWINCE__
1760 if ( !::GetCursorPos(&ptClick
) )
1762 wxLogLastError(_T("GetCursorPos"));
1765 if ( !::ScreenToClient(GetHwnd(), &ptClick
) )
1767 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1770 event
.m_pointDrag
.x
= ptClick
.x
;
1771 event
.m_pointDrag
.y
= ptClick
.y
;
1773 int colCount
= Header_GetItemCount(hwndHdr
);
1776 for ( int col
= 0; col
< colCount
; col
++ )
1778 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1780 if ( ::PtInRect(&rect
, ptClick
) )
1790 case HDN_GETDISPINFOW
:
1791 // letting Windows XP handle this message results in mysterious
1792 // crashes in comctl32.dll seemingly because of bad message
1795 // I have no idea what is the real cause of the bug (which is,
1796 // just to make things interesting, is impossible to reproduce
1797 // reliably) but ignoring all these messages does fix it and
1798 // doesn't seem to have any negative consequences
1802 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1806 #endif // defined(HDN_BEGINTRACKA)
1807 if ( nmhdr
->hwndFrom
== GetHwnd() )
1809 // almost all messages use NM_LISTVIEW
1810 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1812 const int iItem
= nmLV
->iItem
;
1815 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1816 // ignored for efficiency. It is done here because the internal data is in the
1817 // process of being deleted so we don't want to try and access it below.
1818 if ( m_ignoreChangeMessages
&&
1819 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1825 // If we have a valid item then check if there is a data value
1826 // associated with it and put it in the event.
1827 if ( iItem
>= 0 && iItem
< GetItemCount() )
1829 wxListItemInternalData
*internaldata
=
1830 wxGetInternalData(GetHwnd(), iItem
);
1833 event
.m_item
.m_data
= internaldata
->lParam
;
1837 switch ( nmhdr
->code
)
1839 case LVN_BEGINRDRAG
:
1840 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1844 if ( eventType
== wxEVT_NULL
)
1846 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1849 event
.m_itemIndex
= iItem
;
1850 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1851 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1854 // NB: we have to handle both *A and *W versions here because some
1855 // versions of comctl32.dll send ANSI messages even to the
1857 case LVN_BEGINLABELEDITA
:
1858 case LVN_BEGINLABELEDITW
:
1861 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1863 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1865 else // LVN_BEGINLABELEDITW
1867 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1870 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1871 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1872 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1876 case LVN_ENDLABELEDITA
:
1877 case LVN_ENDLABELEDITW
:
1880 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1882 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1884 else // LVN_ENDLABELEDITW
1886 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1889 // was editing cancelled?
1890 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1891 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1893 // don't keep a stale wxTextCtrl around
1896 // EDIT control will be deleted by the list control itself so
1897 // prevent us from deleting it as well
1898 m_textCtrl
->UnsubclassWin();
1899 m_textCtrl
->SetHWND(0);
1904 event
.SetEditCanceled(true);
1907 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1908 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1909 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1913 case LVN_COLUMNCLICK
:
1914 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1915 event
.m_itemIndex
= -1;
1916 event
.m_col
= nmLV
->iSubItem
;
1919 case LVN_DELETEALLITEMS
:
1921 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1922 event
.m_itemIndex
= -1;
1925 case LVN_DELETEITEM
:
1927 // this should be prevented by the post-processing code below,
1928 // but "just in case"
1931 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1932 event
.m_itemIndex
= iItem
;
1933 // delete the assoicated internal data
1934 wxDeleteInternalData(this, iItem
);
1937 #if WXWIN_COMPATIBILITY_2_4
1938 case LVN_SETDISPINFO
:
1940 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1941 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1942 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
1947 case LVN_INSERTITEM
:
1948 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1949 event
.m_itemIndex
= iItem
;
1952 case LVN_ITEMCHANGED
:
1953 // we translate this catch all message into more interesting
1954 // (and more easy to process) wxWidgets events
1956 // first of all, we deal with the state change events only and
1957 // only for valid items (item == -1 for the virtual list
1959 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
1961 // temp vars for readability
1962 const UINT stOld
= nmLV
->uOldState
;
1963 const UINT stNew
= nmLV
->uNewState
;
1965 event
.m_item
.SetId(iItem
);
1966 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
1969 GetItem(event
.m_item
);
1971 // has the focus changed?
1972 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
1974 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
1975 event
.m_itemIndex
= iItem
;
1978 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
1980 if ( eventType
!= wxEVT_NULL
)
1982 // focus and selection have both changed: send the
1983 // focus event from here and the selection one
1985 event
.SetEventType(eventType
);
1986 (void)GetEventHandler()->ProcessEvent(event
);
1988 else // no focus event to send
1990 // then need to set m_itemIndex as it wasn't done
1992 event
.m_itemIndex
= iItem
;
1995 eventType
= stNew
& LVIS_SELECTED
1996 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1997 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2001 if ( eventType
== wxEVT_NULL
)
2003 // not an interesting event for us
2011 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2012 WORD wVKey
= info
->wVKey
;
2014 // get the current selection
2015 long lItem
= GetNextItem(-1,
2017 wxLIST_STATE_SELECTED
);
2019 // <Enter> or <Space> activate the selected item if any (but
2020 // not with Shift and/or Ctrl as then they have a predefined
2021 // meaning for the list view)
2023 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2024 !(wxIsShiftDown() || wxIsCtrlDown()) )
2026 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2030 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2032 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2033 // value which should be used as is
2034 int code
= wxCharCodeMSWToWX(wVKey
);
2035 event
.m_code
= code
? code
: wVKey
;
2039 event
.m_item
.m_itemId
= lItem
;
2043 // fill the other fields too
2044 event
.m_item
.m_text
= GetItemText(lItem
);
2045 event
.m_item
.m_data
= GetItemData(lItem
);
2051 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2053 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2058 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2059 // if it happened on an item (and not on empty place)
2066 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2067 event
.m_itemIndex
= iItem
;
2068 event
.m_item
.m_text
= GetItemText(iItem
);
2069 event
.m_item
.m_data
= GetItemData(iItem
);
2072 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2073 case GN_CONTEXTMENU
:
2074 #endif //__WXWINCE__
2076 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2077 // don't do anything else
2078 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2083 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2084 LV_HITTESTINFO lvhti
;
2085 wxZeroMemory(lvhti
);
2087 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2088 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2089 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2091 #endif //__WXWINCE__
2092 ::GetCursorPos(&(lvhti
.pt
));
2093 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2094 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2096 if ( lvhti
.flags
& LVHT_ONITEM
)
2098 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2099 event
.m_itemIndex
= lvhti
.iItem
;
2100 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2101 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2106 #ifdef NM_CUSTOMDRAW
2108 *result
= OnCustomDraw(lParam
);
2111 #endif // _WIN32_IE >= 0x300
2113 case LVN_ODCACHEHINT
:
2115 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2117 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2119 // we get some really stupid cache hints like ones for
2120 // items in range 0..0 for an empty control or, after
2121 // deleting an item, for items in invalid range -- filter
2123 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2126 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2128 const long iMax
= GetItemCount();
2129 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2134 case LVN_GETDISPINFO
:
2137 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2139 LV_ITEM
& lvi
= info
->item
;
2140 long item
= lvi
.iItem
;
2142 if ( lvi
.mask
& LVIF_TEXT
)
2144 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2145 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2148 // see comment at the end of wxListCtrl::GetColumn()
2149 #ifdef NM_CUSTOMDRAW
2150 if ( lvi
.mask
& LVIF_IMAGE
)
2152 lvi
.iImage
= OnGetItemImage(item
);
2154 #endif // NM_CUSTOMDRAW
2156 // a little dose of healthy paranoia: as we never use
2157 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2158 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2159 _T("we don't support state callbacks yet!") );
2166 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2171 // where did this one come from?
2175 // process the event
2176 // -----------------
2178 event
.SetEventType(eventType
);
2180 bool processed
= GetEventHandler()->ProcessEvent(event
);
2184 switch ( nmhdr
->code
)
2186 case LVN_DELETEALLITEMS
:
2187 // always return true to suppress all additional LVN_DELETEITEM
2188 // notifications - this makes deleting all items from a list ctrl
2192 // also, we may free all user data now (couldn't do it before as
2193 // the user should have access to it in OnDeleteAllItems() handler)
2194 FreeAllInternalData();
2197 case LVN_ENDLABELEDITA
:
2198 case LVN_ENDLABELEDITW
:
2199 // logic here is inversed compared to all the other messages
2200 *result
= event
.IsAllowed();
2202 // don't keep a stale wxTextCtrl around
2205 // EDIT control will be deleted by the list control itself so
2206 // prevent us from deleting it as well
2207 m_textCtrl
->UnsubclassWin();
2208 m_textCtrl
->SetHWND(0);
2217 *result
= !event
.IsAllowed();
2222 // see comment at the end of wxListCtrl::GetColumn()
2223 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2225 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2227 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2228 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2229 switch ( nmcd
.dwDrawStage
)
2232 // if we've got any items with non standard attributes,
2233 // notify us before painting each item
2235 // for virtual controls, always suppose that we have attributes as
2236 // there is no way to check for this
2237 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2240 case CDDS_ITEMPREPAINT
:
2242 size_t item
= (size_t)nmcd
.dwItemSpec
;
2243 if ( item
>= (size_t)GetItemCount() )
2245 // we get this message with item == 0 for an empty control,
2246 // we must ignore it as calling OnGetItemAttr() would be
2248 return CDRF_DODEFAULT
;
2251 wxListItemAttr
*attr
=
2252 IsVirtual() ? OnGetItemAttr(item
)
2253 : wxGetInternalDataAttr(this, item
);
2257 // nothing to do for this item
2258 return CDRF_DODEFAULT
;
2262 wxColour colText
, colBack
;
2263 if ( attr
->HasFont() )
2265 wxFont font
= attr
->GetFont();
2266 hFont
= (HFONT
)font
.GetResourceHandle();
2273 if ( attr
->HasTextColour() )
2275 colText
= attr
->GetTextColour();
2279 colText
= GetTextColour();
2282 if ( attr
->HasBackgroundColour() )
2284 colBack
= attr
->GetBackgroundColour();
2288 colBack
= GetBackgroundColour();
2291 lplvcd
->clrText
= wxColourToRGB(colText
);
2292 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2294 // note that if we wanted to set colours for
2295 // individual columns (subitems), we would have
2296 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2299 ::SelectObject(nmcd
.hdc
, hFont
);
2301 return CDRF_NEWFONT
;
2304 // fall through to return CDRF_DODEFAULT
2307 return CDRF_DODEFAULT
;
2311 #endif // NM_CUSTOMDRAW supported
2313 // Necessary for drawing hrules and vrules, if specified
2314 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2318 wxControl::OnPaint(event
);
2320 // Reset the device origin since it may have been set
2321 dc
.SetDeviceOrigin(0, 0);
2323 bool drawHRules
= HasFlag(wxLC_HRULES
);
2324 bool drawVRules
= HasFlag(wxLC_VRULES
);
2326 if (!InReportView() || !drawHRules
&& !drawVRules
)
2329 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2331 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2333 wxSize clientSize
= GetClientSize();
2336 int itemCount
= GetItemCount();
2340 long top
= GetTopItem();
2341 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2343 if (GetItemRect(i
, itemRect
))
2345 int cy
= itemRect
.GetTop();
2346 if (i
!= 0) // Don't draw the first one
2348 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2351 if (i
== itemCount
- 1)
2353 cy
= itemRect
.GetBottom();
2354 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2360 if (drawVRules
&& (i
> -1))
2362 wxRect firstItemRect
;
2363 GetItemRect(0, firstItemRect
);
2365 if (GetItemRect(i
, itemRect
))
2368 int x
= itemRect
.GetX();
2369 for (col
= 0; col
< GetColumnCount(); col
++)
2371 int colWidth
= GetColumnWidth(col
);
2373 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2379 // ----------------------------------------------------------------------------
2380 // virtual list controls
2381 // ----------------------------------------------------------------------------
2383 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2385 // this is a pure virtual function, in fact - which is not really pure
2386 // because the controls which are not virtual don't need to implement it
2387 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2389 return wxEmptyString
;
2392 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2394 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
2396 wxT("List control has an image list, OnGetItemImage should be overridden."));
2400 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2402 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2403 _T("invalid item index in OnGetItemAttr()") );
2405 // no attributes by default
2409 void wxListCtrl::SetItemCount(long count
)
2411 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2413 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2414 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2416 wxLogLastError(_T("ListView_SetItemCount"));
2419 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2420 wxT("m_count should match ListView_GetItemCount"));
2423 void wxListCtrl::RefreshItem(long item
)
2425 // strangely enough, ListView_Update() results in much more flicker here
2426 // than a dumb Refresh() -- why?
2428 if ( !ListView_Update(GetHwnd(), item
) )
2430 wxLogLastError(_T("ListView_Update"));
2434 GetItemRect(item
, rect
);
2439 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2441 wxRect rect1
, rect2
;
2442 GetItemRect(itemFrom
, rect1
);
2443 GetItemRect(itemTo
, rect2
);
2445 wxRect rect
= rect1
;
2446 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2451 // ----------------------------------------------------------------------------
2452 // internal data stuff
2453 // ----------------------------------------------------------------------------
2455 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2458 it
.mask
= LVIF_PARAM
;
2461 if ( !ListView_GetItem(hwnd
, &it
) )
2464 return (wxListItemInternalData
*) it
.lParam
;
2468 wxListItemInternalData
*wxGetInternalData(const wxListCtrl
*ctl
, long itemId
)
2470 return wxGetInternalData(GetHwndOf(ctl
), itemId
);
2473 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2475 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2477 return data
? data
->attr
: NULL
;
2480 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2482 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2486 memset(&item
, 0, sizeof(item
));
2487 item
.iItem
= itemId
;
2488 item
.mask
= LVIF_PARAM
;
2489 item
.lParam
= (LPARAM
) 0;
2490 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2495 // ----------------------------------------------------------------------------
2496 // wxWin <-> MSW items conversions
2497 // ----------------------------------------------------------------------------
2499 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2503 wxListItemInternalData
*internaldata
=
2504 (wxListItemInternalData
*) lvItem
.lParam
;
2507 info
.m_data
= internaldata
->lParam
;
2511 info
.m_stateMask
= 0;
2512 info
.m_itemId
= lvItem
.iItem
;
2514 long oldMask
= lvItem
.mask
;
2516 bool needText
= false;
2517 if (hwndListCtrl
!= 0)
2519 if ( lvItem
.mask
& LVIF_TEXT
)
2526 lvItem
.pszText
= new wxChar
[513];
2527 lvItem
.cchTextMax
= 512;
2529 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2530 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2533 if ( lvItem
.mask
& LVIF_STATE
)
2535 info
.m_mask
|= wxLIST_MASK_STATE
;
2537 if ( lvItem
.stateMask
& LVIS_CUT
)
2539 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2540 if ( lvItem
.state
& LVIS_CUT
)
2541 info
.m_state
|= wxLIST_STATE_CUT
;
2543 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2545 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2546 if ( lvItem
.state
& LVIS_DROPHILITED
)
2547 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2549 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2551 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2552 if ( lvItem
.state
& LVIS_FOCUSED
)
2553 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2555 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2557 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2558 if ( lvItem
.state
& LVIS_SELECTED
)
2559 info
.m_state
|= wxLIST_STATE_SELECTED
;
2563 if ( lvItem
.mask
& LVIF_TEXT
)
2565 info
.m_mask
|= wxLIST_MASK_TEXT
;
2566 info
.m_text
= lvItem
.pszText
;
2568 if ( lvItem
.mask
& LVIF_IMAGE
)
2570 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2571 info
.m_image
= lvItem
.iImage
;
2573 if ( lvItem
.mask
& LVIF_PARAM
)
2574 info
.m_mask
|= wxLIST_MASK_DATA
;
2575 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2576 info
.m_mask
|= wxLIST_SET_ITEM
;
2577 info
.m_col
= lvItem
.iSubItem
;
2582 delete[] lvItem
.pszText
;
2584 lvItem
.mask
= oldMask
;
2587 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2589 if (stateMask
& wxLIST_STATE_CUT
)
2591 lvItem
.stateMask
|= LVIS_CUT
;
2592 if (state
& wxLIST_STATE_CUT
)
2593 lvItem
.state
|= LVIS_CUT
;
2595 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2597 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2598 if (state
& wxLIST_STATE_DROPHILITED
)
2599 lvItem
.state
|= LVIS_DROPHILITED
;
2601 if (stateMask
& wxLIST_STATE_FOCUSED
)
2603 lvItem
.stateMask
|= LVIS_FOCUSED
;
2604 if (state
& wxLIST_STATE_FOCUSED
)
2605 lvItem
.state
|= LVIS_FOCUSED
;
2607 if (stateMask
& wxLIST_STATE_SELECTED
)
2609 lvItem
.stateMask
|= LVIS_SELECTED
;
2610 if (state
& wxLIST_STATE_SELECTED
)
2611 lvItem
.state
|= LVIS_SELECTED
;
2615 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2616 const wxListItem
& info
,
2619 lvItem
.iItem
= (int) info
.m_itemId
;
2621 lvItem
.iImage
= info
.m_image
;
2622 lvItem
.stateMask
= 0;
2625 lvItem
.iSubItem
= info
.m_col
;
2627 if (info
.m_mask
& wxLIST_MASK_STATE
)
2629 lvItem
.mask
|= LVIF_STATE
;
2631 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2634 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2636 lvItem
.mask
|= LVIF_TEXT
;
2637 if ( ctrl
->HasFlag(wxLC_USER_TEXT
) )
2639 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2643 // pszText is not const, hence the cast
2644 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2645 if ( lvItem
.pszText
)
2646 lvItem
.cchTextMax
= info
.m_text
.Length();
2648 lvItem
.cchTextMax
= 0;
2651 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2652 lvItem
.mask
|= LVIF_IMAGE
;
2655 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2658 wxZeroMemory(lvCol
);
2660 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2662 lvCol
.mask
|= LVCF_TEXT
;
2663 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2666 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2668 lvCol
.mask
|= LVCF_FMT
;
2670 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2671 lvCol
.fmt
= LVCFMT_LEFT
;
2672 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2673 lvCol
.fmt
= LVCFMT_RIGHT
;
2674 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2675 lvCol
.fmt
= LVCFMT_CENTER
;
2678 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2680 lvCol
.mask
|= LVCF_WIDTH
;
2681 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2682 lvCol
.cx
= LVSCW_AUTOSIZE
;
2683 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2684 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2686 lvCol
.cx
= item
.m_width
;
2689 // see comment at the end of wxListCtrl::GetColumn()
2690 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2691 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2693 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2695 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2697 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2698 // seem to be generally nicer than on the left and the generic
2699 // version only draws them on the right (we don't have a flag to
2700 // specify the image location anyhow)
2702 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2703 // make any difference in my tests -- but maybe we should?
2704 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2706 lvCol
.iImage
= item
.m_image
;
2708 //else: it doesn't support item images anyhow
2710 #endif // _WIN32_IE >= 0x0300
2713 #endif // wxUSE_LISTCTRL