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__)
54 // include <commctrl.h> "properly"
55 #include "wx/msw/wrapcctl.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // convert our state and mask flags to LV_ITEM constants
62 static void wxConvertToMSWFlags(long state
, long mask
, LV_ITEM
& lvItem
);
64 // convert wxListItem to LV_ITEM
65 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
66 const wxListItem
& info
, LV_ITEM
& lvItem
);
68 // convert LV_ITEM to wxListItem
69 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
71 /* const */ LV_ITEM
& lvItem
);
73 // convert our wxListItem to LV_COLUMN
74 static void wxConvertToMSWListCol(int col
, const wxListItem
& item
,
77 // ----------------------------------------------------------------------------
78 // private helper classes
79 // ----------------------------------------------------------------------------
81 // We have to handle both fooW and fooA notifications in several cases
82 // because of broken comctl32.dll and/or unicows.dll. This class is used to
83 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
84 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
85 // by wxConvertToMSWListItem().
87 #define LV_ITEM_NATIVE LV_ITEMW
88 #define LV_ITEM_OTHER LV_ITEMA
90 #define LV_CONV_TO_WX cMB2WX
91 #define LV_CONV_BUF wxMB2WXbuf
93 #define LV_ITEM_NATIVE LV_ITEMA
94 #define LV_ITEM_OTHER LV_ITEMW
96 #define LV_CONV_TO_WX cWC2WX
97 #define LV_CONV_BUF wxWC2WXbuf
98 #endif // Unicode/ANSI
103 // default ctor, use Init() later
104 wxLV_ITEM() { m_buf
= NULL
; m_pItem
= NULL
; }
106 // init without conversion
107 void Init(LV_ITEM_NATIVE
& item
)
109 wxASSERT_MSG( !m_pItem
, _T("Init() called twice?") );
114 // init with conversion
115 void Init(LV_ITEM_OTHER
& item
)
117 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
118 // point to our own m_item
120 // memcpy() can't work if the struct sizes are different
121 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER
) == sizeof(LV_ITEM_NATIVE
),
122 CodeCantWorkIfDiffSizes
);
124 memcpy(&m_item
, &item
, sizeof(LV_ITEM_NATIVE
));
126 // convert text from ANSI to Unicod if necessary
127 if ( (item
.mask
& LVIF_TEXT
) && item
.pszText
)
129 m_buf
= new LV_CONV_BUF(wxConvLocal
.LV_CONV_TO_WX(item
.pszText
));
130 m_item
.pszText
= (wxChar
*)m_buf
->data();
134 // ctor without conversion
135 wxLV_ITEM(LV_ITEM_NATIVE
& item
) : m_buf(NULL
), m_pItem(&item
) { }
137 // ctor with conversion
138 wxLV_ITEM(LV_ITEM_OTHER
& item
) : m_buf(NULL
)
143 ~wxLV_ITEM() { delete m_buf
; }
145 // conversion to the real LV_ITEM
146 operator LV_ITEM_NATIVE
&() const { return *m_pItem
; }
151 LV_ITEM_NATIVE
*m_pItem
;
152 LV_ITEM_NATIVE m_item
;
154 DECLARE_NO_COPY_CLASS(wxLV_ITEM
)
157 ///////////////////////////////////////////////////////
159 // The MSW version had problems with SetTextColour() et
160 // al as the wxListItemAttr's were stored keyed on the
161 // item index. If a item was inserted anywhere but the end
162 // of the list the the text attributes (colour etc) for
163 // the following items were out of sync.
166 // Under MSW the only way to associate data with a List
167 // item independant of its position in the list is to
168 // store a pointer to it in its lParam attribute. However
169 // user programs are already using this (via the
170 // SetItemData() GetItemData() calls).
172 // However what we can do is store a pointer to a
173 // structure which contains the attributes we want *and*
174 // a lParam for the users data, e.g.
176 // class wxListItemInternalData
179 // wxListItemAttr *attr;
180 // long lParam; // user data
183 // To conserve memory, a wxListItemInternalData is
184 // only allocated for a LV_ITEM if text attributes or
185 // user data(lparam) are being set.
188 // class wxListItemInternalData
189 class wxListItemInternalData
192 wxListItemAttr
*attr
;
193 LPARAM lParam
; // user data
195 wxListItemInternalData() : attr(NULL
), lParam(0) {}
196 ~wxListItemInternalData()
202 DECLARE_NO_COPY_CLASS(wxListItemInternalData
)
205 // Get the internal data structure
206 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
);
207 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
);
208 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
);
209 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
);
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
217 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
218 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
219 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
220 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
221 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
222 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
224 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
225 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
226 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
227 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
228 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
229 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
230 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
231 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
232 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
233 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
234 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
235 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
236 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
237 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
239 #if wxUSE_EXTENDED_RTTI
240 WX_DEFINE_FLAGS( wxListCtrlStyle
)
242 wxBEGIN_FLAGS( wxListCtrlStyle
)
243 // new style border flags, we put them first to
244 // use them for streaming out
245 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
246 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
247 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
248 wxFLAGS_MEMBER(wxBORDER_RAISED
)
249 wxFLAGS_MEMBER(wxBORDER_STATIC
)
250 wxFLAGS_MEMBER(wxBORDER_NONE
)
252 // old style border flags
253 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
254 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
255 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
256 wxFLAGS_MEMBER(wxRAISED_BORDER
)
257 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
258 wxFLAGS_MEMBER(wxBORDER
)
260 // standard window styles
261 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
262 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
263 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
264 wxFLAGS_MEMBER(wxWANTS_CHARS
)
265 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
266 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
267 wxFLAGS_MEMBER(wxVSCROLL
)
268 wxFLAGS_MEMBER(wxHSCROLL
)
270 wxFLAGS_MEMBER(wxLC_LIST
)
271 wxFLAGS_MEMBER(wxLC_REPORT
)
272 wxFLAGS_MEMBER(wxLC_ICON
)
273 wxFLAGS_MEMBER(wxLC_SMALL_ICON
)
274 wxFLAGS_MEMBER(wxLC_ALIGN_TOP
)
275 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT
)
276 wxFLAGS_MEMBER(wxLC_AUTOARRANGE
)
277 wxFLAGS_MEMBER(wxLC_USER_TEXT
)
278 wxFLAGS_MEMBER(wxLC_EDIT_LABELS
)
279 wxFLAGS_MEMBER(wxLC_NO_HEADER
)
280 wxFLAGS_MEMBER(wxLC_SINGLE_SEL
)
281 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING
)
282 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING
)
283 wxFLAGS_MEMBER(wxLC_VIRTUAL
)
285 wxEND_FLAGS( wxListCtrlStyle
)
287 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl
, wxControl
,"wx/listctrl.h")
289 wxBEGIN_PROPERTIES_TABLE(wxListCtrl
)
290 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
292 wxPROPERTY_FLAGS( WindowStyle
, wxListCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
293 wxEND_PROPERTIES_TABLE()
295 wxBEGIN_HANDLERS_TABLE(wxListCtrl
)
296 wxEND_HANDLERS_TABLE()
298 wxCONSTRUCTOR_5( wxListCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
301 TODO : Expose more information of a list's layout etc. via appropriate objects (Ã la NotebookPageInfo)
304 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
307 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
308 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
310 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
312 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
313 EVT_PAINT(wxListCtrl::OnPaint
)
316 // ============================================================================
318 // ============================================================================
320 // ----------------------------------------------------------------------------
321 // wxListCtrl construction
322 // ----------------------------------------------------------------------------
324 void wxListCtrl::Init()
326 m_imageListNormal
= NULL
;
327 m_imageListSmall
= NULL
;
328 m_imageListState
= NULL
;
329 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
333 m_ignoreChangeMessages
= FALSE
;
335 m_AnyInternalData
= FALSE
;
336 m_hasAnyAttr
= FALSE
;
339 bool wxListCtrl::Create(wxWindow
*parent
,
344 const wxValidator
& wxVALIDATOR_PARAM(validator
),
345 const wxString
& name
)
348 SetValidator(validator
);
349 #endif // wxUSE_VALIDATORS
358 m_windowStyle
= style
;
371 m_windowId
= (id
== -1) ? NewControlId() : id
;
373 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
374 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
376 m_baseStyle
= wstyle
;
378 if ( !DoCreateControl(x
, y
, width
, height
) )
382 parent
->AddChild(this);
387 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
389 DWORD wstyle
= m_baseStyle
;
392 WXDWORD standardStyle
= MSWGetStyle(GetWindowStyle(), & exStyle
) ;
394 long oldStyle
= 0; // Dummy
395 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
396 wstyle
|= standardStyle
;
398 // Create the ListView control.
399 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
404 GetWinHwnd(GetParent()),
411 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
416 // explicitly say that we want to use Unicode because otherwise we get ANSI
417 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
419 ::SendMessage(GetHwnd(), LVM_SETUNICODEFORMAT
, TRUE
, 0);
422 // for comctl32.dll v 4.70+ we want to have this attribute because it's
423 // prettier (and also because wxGTK does it like this)
424 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
426 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
427 0, LVS_EX_FULLROWSELECT
);
430 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
431 SetForegroundColour(GetParent()->GetForegroundColour());
438 void wxListCtrl::UpdateStyle()
442 // The new window view style
444 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
445 dwStyleNew
|= m_baseStyle
;
447 // Get the current window style.
448 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
450 // Only set the window style if the view bits have changed.
451 if ( dwStyleOld
!= dwStyleNew
)
453 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
458 void wxListCtrl::FreeAllInternalData()
460 if (m_AnyInternalData
)
462 int n
= GetItemCount();
465 m_ignoreChangeMessages
= TRUE
;
466 for (i
= 0; i
< n
; i
++)
467 wxDeleteInternalData(this, i
);
468 m_ignoreChangeMessages
= FALSE
;
470 m_AnyInternalData
= FALSE
;
474 wxListCtrl::~wxListCtrl()
476 FreeAllInternalData();
480 m_textCtrl
->UnsubclassWin();
481 m_textCtrl
->SetHWND(0);
486 if (m_ownsImageListNormal
) delete m_imageListNormal
;
487 if (m_ownsImageListSmall
) delete m_imageListSmall
;
488 if (m_ownsImageListState
) delete m_imageListState
;
491 // ----------------------------------------------------------------------------
492 // set/get/change style
493 // ----------------------------------------------------------------------------
495 // Add or remove a single window style
496 void wxListCtrl::SetSingleStyle(long style
, bool add
)
498 long flag
= GetWindowStyleFlag();
500 // Get rid of conflicting styles
503 if ( style
& wxLC_MASK_TYPE
)
504 flag
= flag
& ~wxLC_MASK_TYPE
;
505 if ( style
& wxLC_MASK_ALIGN
)
506 flag
= flag
& ~wxLC_MASK_ALIGN
;
507 if ( style
& wxLC_MASK_SORT
)
508 flag
= flag
& ~wxLC_MASK_SORT
;
524 m_windowStyle
= flag
;
529 // Set the whole window style
530 void wxListCtrl::SetWindowStyleFlag(long flag
)
532 m_windowStyle
= flag
;
537 // Can be just a single style, or a bitlist
538 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
541 if ( style
& wxLC_ICON
)
543 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
544 oldStyle
-= LVS_SMALLICON
;
545 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
546 oldStyle
-= LVS_REPORT
;
547 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
548 oldStyle
-= LVS_LIST
;
552 if ( style
& wxLC_SMALL_ICON
)
554 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
555 oldStyle
-= LVS_ICON
;
556 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
557 oldStyle
-= LVS_REPORT
;
558 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
559 oldStyle
-= LVS_LIST
;
560 wstyle
|= LVS_SMALLICON
;
563 if ( style
& wxLC_LIST
)
565 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
566 oldStyle
-= LVS_ICON
;
567 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
568 oldStyle
-= LVS_REPORT
;
569 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
570 oldStyle
-= LVS_SMALLICON
;
574 if ( style
& wxLC_REPORT
)
576 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
577 oldStyle
-= LVS_ICON
;
578 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
579 oldStyle
-= LVS_LIST
;
580 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
581 oldStyle
-= LVS_SMALLICON
;
583 wstyle
|= LVS_REPORT
;
586 if ( style
& wxLC_ALIGN_LEFT
)
588 if ( oldStyle
& LVS_ALIGNTOP
)
589 oldStyle
-= LVS_ALIGNTOP
;
590 wstyle
|= LVS_ALIGNLEFT
;
593 if ( style
& wxLC_ALIGN_TOP
)
595 if ( oldStyle
& LVS_ALIGNLEFT
)
596 oldStyle
-= LVS_ALIGNLEFT
;
597 wstyle
|= LVS_ALIGNTOP
;
600 if ( style
& wxLC_AUTOARRANGE
)
601 wstyle
|= LVS_AUTOARRANGE
;
603 if ( style
& wxLC_NO_SORT_HEADER
)
604 wstyle
|= LVS_NOSORTHEADER
;
606 if ( style
& wxLC_NO_HEADER
)
607 wstyle
|= LVS_NOCOLUMNHEADER
;
609 if ( style
& wxLC_EDIT_LABELS
)
610 wstyle
|= LVS_EDITLABELS
;
612 if ( style
& wxLC_SINGLE_SEL
)
613 wstyle
|= LVS_SINGLESEL
;
615 if ( style
& wxLC_SORT_ASCENDING
)
617 if ( oldStyle
& LVS_SORTDESCENDING
)
618 oldStyle
-= LVS_SORTDESCENDING
;
619 wstyle
|= LVS_SORTASCENDING
;
622 if ( style
& wxLC_SORT_DESCENDING
)
624 if ( oldStyle
& LVS_SORTASCENDING
)
625 oldStyle
-= LVS_SORTASCENDING
;
626 wstyle
|= LVS_SORTDESCENDING
;
629 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
630 if ( style
& wxLC_VIRTUAL
)
632 int ver
= wxTheApp
->GetComCtl32Version();
635 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."),
636 ver
/ 100, ver
% 100);
639 wstyle
|= LVS_OWNERDATA
;
646 // ----------------------------------------------------------------------------
648 // ----------------------------------------------------------------------------
650 // Sets the foreground, i.e. text, colour
651 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
653 if ( !wxWindow::SetForegroundColour(col
) )
656 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
661 // Sets the background colour
662 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
664 if ( !wxWindow::SetBackgroundColour(col
) )
667 // we set the same colour for both the "empty" background and the items
669 COLORREF color
= wxColourToRGB(col
);
670 ListView_SetBkColor(GetHwnd(), color
);
671 ListView_SetTextBkColor(GetHwnd(), color
);
676 // Gets information about this column
677 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
682 lvCol
.mask
= LVCF_WIDTH
;
684 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
686 lvCol
.mask
|= LVCF_TEXT
;
687 lvCol
.pszText
= new wxChar
[513];
688 lvCol
.cchTextMax
= 512;
691 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
693 lvCol
.mask
|= LVCF_FMT
;
696 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
698 lvCol
.mask
|= LVCF_IMAGE
;
701 bool success
= ListView_GetColumn(GetHwnd(), col
, &lvCol
) != 0;
703 // item.m_subItem = lvCol.iSubItem;
704 item
.m_width
= lvCol
.cx
;
706 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
708 item
.m_text
= lvCol
.pszText
;
709 delete[] lvCol
.pszText
;
712 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
714 switch (lvCol
.fmt
& LVCFMT_JUSTIFYMASK
) {
716 item
.m_format
= wxLIST_FORMAT_LEFT
;
719 item
.m_format
= wxLIST_FORMAT_RIGHT
;
722 item
.m_format
= wxLIST_FORMAT_CENTRE
;
725 item
.m_format
= -1; // Unknown?
730 // the column images were not supported in older versions but how to check
731 // for this? we can't use _WIN32_IE because we always define it to a very
732 // high value, so see if another symbol which is only defined starting from
733 // comctl32.dll 4.70 is available
734 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
735 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
737 item
.m_image
= lvCol
.iImage
;
739 #endif // LVCOLUMN::iImage exists
744 // Sets information about this column
745 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
748 wxConvertToMSWListCol(col
, item
, lvCol
);
750 return ListView_SetColumn(GetHwnd(), col
, &lvCol
) != 0;
753 // Gets the column width
754 int wxListCtrl::GetColumnWidth(int col
) const
756 return ListView_GetColumnWidth(GetHwnd(), col
);
759 // Sets the column width
760 bool wxListCtrl::SetColumnWidth(int col
, int width
)
763 if ( m_windowStyle
& wxLC_LIST
)
767 if ( width2
== wxLIST_AUTOSIZE
)
768 width2
= LVSCW_AUTOSIZE
;
769 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
770 width2
= LVSCW_AUTOSIZE_USEHEADER
;
772 return ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0;
775 // Gets the number of items that can fit vertically in the
776 // visible area of the list control (list or report view)
777 // or the total number of items in the list control (icon
778 // or small icon view)
779 int wxListCtrl::GetCountPerPage() const
781 return ListView_GetCountPerPage(GetHwnd());
784 // Gets the edit control for editing labels.
785 wxTextCtrl
* wxListCtrl::GetEditControl() const
790 // Gets information about the item
791 bool wxListCtrl::GetItem(wxListItem
& info
) const
794 wxZeroMemory(lvItem
);
796 lvItem
.iItem
= info
.m_itemId
;
797 lvItem
.iSubItem
= info
.m_col
;
799 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
801 lvItem
.mask
|= LVIF_TEXT
;
802 lvItem
.pszText
= new wxChar
[513];
803 lvItem
.cchTextMax
= 512;
807 lvItem
.pszText
= NULL
;
810 if (info
.m_mask
& wxLIST_MASK_DATA
)
811 lvItem
.mask
|= LVIF_PARAM
;
813 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
814 lvItem
.mask
|= LVIF_IMAGE
;
816 if ( info
.m_mask
& wxLIST_MASK_STATE
)
818 lvItem
.mask
|= LVIF_STATE
;
819 // the other bits are hardly interesting anyhow
820 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
823 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
826 wxLogError(_("Couldn't retrieve information about list control item %d."),
831 // give NULL as hwnd as we already have everything we need
832 wxConvertFromMSWListItem(NULL
, info
, lvItem
);
836 delete[] lvItem
.pszText
;
841 // Sets information about the item
842 bool wxListCtrl::SetItem(wxListItem
& info
)
845 wxConvertToMSWListItem(this, info
, item
);
847 // we never update the lParam if it contains our pointer
848 // to the wxListItemInternalData structure
849 item
.mask
&= ~LVIF_PARAM
;
851 // check if setting attributes or lParam
852 if (info
.HasAttributes() || (info
.m_mask
& wxLIST_MASK_DATA
))
854 // get internal item data
855 // perhaps a cache here ?
856 wxListItemInternalData
*data
= wxGetInternalData(this, info
.m_itemId
);
861 m_AnyInternalData
= TRUE
;
862 data
= new wxListItemInternalData();
863 item
.lParam
= (LPARAM
) data
;
864 item
.mask
|= LVIF_PARAM
;
869 if (info
.m_mask
& wxLIST_MASK_DATA
)
870 data
->lParam
= info
.m_data
;
873 if (info
.HasAttributes())
876 *data
->attr
= *info
.GetAttributes();
878 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
883 // we could be changing only the attribute in which case we don't need to
884 // call ListView_SetItem() at all
888 if ( !ListView_SetItem(GetHwnd(), &item
) )
890 wxLogDebug(_T("ListView_SetItem() failed"));
896 // we need to update the item immediately to show the new image
897 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
899 // check whether it has any custom attributes
900 if ( info
.HasAttributes() )
904 // if the colour has changed, we must redraw the item
910 // we need this to make the change visible right now
911 RefreshItem(item
.iItem
);
917 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
921 info
.m_mask
= wxLIST_MASK_TEXT
;
922 info
.m_itemId
= index
;
926 info
.m_image
= imageId
;
927 info
.m_mask
|= wxLIST_MASK_IMAGE
;
929 return SetItem(info
);
933 // Gets the item state
934 int wxListCtrl::GetItemState(long item
, long stateMask
) const
938 info
.m_mask
= wxLIST_MASK_STATE
;
939 info
.m_stateMask
= stateMask
;
940 info
.m_itemId
= item
;
948 // Sets the item state
949 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
951 // NB: don't use SetItem() here as it doesn't work with the virtual list
954 wxZeroMemory(lvItem
);
956 wxConvertToMSWFlags(state
, stateMask
, lvItem
);
958 // for the virtual list controls we need to refresh the previously focused
959 // item manually when changing focus without changing selection
960 // programmatically because otherwise it keeps its focus rectangle until
961 // next repaint (yet another comctl32 bug)
964 (stateMask
& wxLIST_STATE_FOCUSED
) &&
965 (state
& wxLIST_STATE_FOCUSED
) )
967 focusOld
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
974 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE
,
975 (WPARAM
)item
, (LPARAM
)&lvItem
) )
977 wxLogLastError(_T("ListView_SetItemState"));
982 if ( focusOld
!= -1 )
984 // no need to refresh the item if it was previously selected, it would
985 // only result in annoying flicker
986 if ( !(GetItemState(focusOld
,
987 wxLIST_STATE_SELECTED
) & wxLIST_STATE_SELECTED
) )
989 RefreshItem(focusOld
);
996 // Sets the item image
997 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
1001 info
.m_mask
= wxLIST_MASK_IMAGE
;
1002 info
.m_image
= image
;
1003 info
.m_itemId
= item
;
1005 return SetItem(info
);
1008 // Gets the item text
1009 wxString
wxListCtrl::GetItemText(long item
) const
1013 info
.m_mask
= wxLIST_MASK_TEXT
;
1014 info
.m_itemId
= item
;
1017 return wxEmptyString
;
1021 // Sets the item text
1022 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
1026 info
.m_mask
= wxLIST_MASK_TEXT
;
1027 info
.m_itemId
= item
;
1033 // Gets the item data
1034 long wxListCtrl::GetItemData(long item
) const
1038 info
.m_mask
= wxLIST_MASK_DATA
;
1039 info
.m_itemId
= item
;
1046 // Sets the item data
1047 bool wxListCtrl::SetItemData(long item
, long data
)
1051 info
.m_mask
= wxLIST_MASK_DATA
;
1052 info
.m_itemId
= item
;
1055 return SetItem(info
);
1058 wxRect
wxListCtrl::GetViewRect() const
1060 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
1061 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1064 if ( !ListView_GetViewRect(GetHwnd(), &rc
) )
1066 wxLogDebug(_T("ListView_GetViewRect() failed."));
1072 wxCopyRECTToRect(rc
, rect
);
1077 // Gets the item rectangle
1078 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
1083 if ( code
== wxLIST_RECT_BOUNDS
)
1084 codeWin
= LVIR_BOUNDS
;
1085 else if ( code
== wxLIST_RECT_ICON
)
1086 codeWin
= LVIR_ICON
;
1087 else if ( code
== wxLIST_RECT_LABEL
)
1088 codeWin
= LVIR_LABEL
;
1091 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1093 codeWin
= LVIR_BOUNDS
;
1096 bool success
= ListView_GetItemRect(GetHwnd(), (int) item
, &rectWin
, codeWin
) != 0;
1098 rect
.x
= rectWin
.left
;
1099 rect
.y
= rectWin
.top
;
1100 rect
.width
= rectWin
.right
- rectWin
.left
;
1101 rect
.height
= rectWin
.bottom
- rectWin
.top
;
1106 // Gets the item position
1107 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
1111 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
1113 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
1117 // Sets the item position.
1118 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
1120 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
1123 // Gets the number of items in the list control
1124 int wxListCtrl::GetItemCount() const
1129 wxSize
wxListCtrl::GetItemSpacing() const
1131 const int spacing
= ListView_GetItemSpacing(GetHwnd(), (BOOL
)HasFlag(wxLC_SMALL_ICON
));
1133 return wxSize(LOWORD(spacing
), HIWORD(spacing
));
1136 int wxListCtrl::GetItemSpacing(bool isSmall
) const
1138 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
1141 void wxListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
1144 info
.m_itemId
= item
;
1145 info
.SetTextColour( col
);
1149 wxColour
wxListCtrl::GetItemTextColour( long item
) const
1152 info
.m_itemId
= item
;
1154 return info
.GetTextColour();
1157 void wxListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
1160 info
.m_itemId
= item
;
1161 info
.SetBackgroundColour( col
);
1165 wxColour
wxListCtrl::GetItemBackgroundColour( long item
) const
1168 info
.m_itemId
= item
;
1170 return info
.GetBackgroundColour();
1173 // Gets the number of selected items in the list control
1174 int wxListCtrl::GetSelectedItemCount() const
1176 return ListView_GetSelectedCount(GetHwnd());
1179 // Gets the text colour of the listview
1180 wxColour
wxListCtrl::GetTextColour() const
1182 COLORREF ref
= ListView_GetTextColor(GetHwnd());
1183 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
1187 // Sets the text colour of the listview
1188 void wxListCtrl::SetTextColour(const wxColour
& col
)
1190 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
1193 // Gets the index of the topmost visible item when in
1194 // list or report view
1195 long wxListCtrl::GetTopItem() const
1197 return (long) ListView_GetTopIndex(GetHwnd());
1200 // Searches for an item, starting from 'item'.
1201 // 'geometry' is one of
1202 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1203 // 'state' is a state bit flag, one or more of
1204 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1205 // item can be -1 to find the first item that matches the
1207 // Returns the item or -1 if unsuccessful.
1208 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
1212 if ( geom
== wxLIST_NEXT_ABOVE
)
1213 flags
|= LVNI_ABOVE
;
1214 if ( geom
== wxLIST_NEXT_ALL
)
1216 if ( geom
== wxLIST_NEXT_BELOW
)
1217 flags
|= LVNI_BELOW
;
1218 if ( geom
== wxLIST_NEXT_LEFT
)
1219 flags
|= LVNI_TOLEFT
;
1220 if ( geom
== wxLIST_NEXT_RIGHT
)
1221 flags
|= LVNI_TORIGHT
;
1223 if ( state
& wxLIST_STATE_CUT
)
1225 if ( state
& wxLIST_STATE_DROPHILITED
)
1226 flags
|= LVNI_DROPHILITED
;
1227 if ( state
& wxLIST_STATE_FOCUSED
)
1228 flags
|= LVNI_FOCUSED
;
1229 if ( state
& wxLIST_STATE_SELECTED
)
1230 flags
|= LVNI_SELECTED
;
1232 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
1236 wxImageList
*wxListCtrl::GetImageList(int which
) const
1238 if ( which
== wxIMAGE_LIST_NORMAL
)
1240 return m_imageListNormal
;
1242 else if ( which
== wxIMAGE_LIST_SMALL
)
1244 return m_imageListSmall
;
1246 else if ( which
== wxIMAGE_LIST_STATE
)
1248 return m_imageListState
;
1253 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
1256 if ( which
== wxIMAGE_LIST_NORMAL
)
1258 flags
= LVSIL_NORMAL
;
1259 if (m_ownsImageListNormal
) delete m_imageListNormal
;
1260 m_imageListNormal
= imageList
;
1261 m_ownsImageListNormal
= FALSE
;
1263 else if ( which
== wxIMAGE_LIST_SMALL
)
1265 flags
= LVSIL_SMALL
;
1266 if (m_ownsImageListSmall
) delete m_imageListSmall
;
1267 m_imageListSmall
= imageList
;
1268 m_ownsImageListSmall
= FALSE
;
1270 else if ( which
== wxIMAGE_LIST_STATE
)
1272 flags
= LVSIL_STATE
;
1273 if (m_ownsImageListState
) delete m_imageListState
;
1274 m_imageListState
= imageList
;
1275 m_ownsImageListState
= FALSE
;
1277 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
1280 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
1282 SetImageList(imageList
, which
);
1283 if ( which
== wxIMAGE_LIST_NORMAL
)
1284 m_ownsImageListNormal
= TRUE
;
1285 else if ( which
== wxIMAGE_LIST_SMALL
)
1286 m_ownsImageListSmall
= TRUE
;
1287 else if ( which
== wxIMAGE_LIST_STATE
)
1288 m_ownsImageListState
= TRUE
;
1291 // ----------------------------------------------------------------------------
1293 // ----------------------------------------------------------------------------
1295 // Arranges the items
1296 bool wxListCtrl::Arrange(int flag
)
1299 if ( flag
== wxLIST_ALIGN_LEFT
)
1300 code
= LVA_ALIGNLEFT
;
1301 else if ( flag
== wxLIST_ALIGN_TOP
)
1302 code
= LVA_ALIGNTOP
;
1303 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
1305 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
1306 code
= LVA_SNAPTOGRID
;
1308 return (ListView_Arrange(GetHwnd(), code
) != 0);
1312 bool wxListCtrl::DeleteItem(long item
)
1314 if ( !ListView_DeleteItem(GetHwnd(), (int) item
) )
1316 wxLogLastError(_T("ListView_DeleteItem"));
1321 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1322 wxT("m_count should match ListView_GetItemCount"));
1324 // the virtual list control doesn't refresh itself correctly, help it
1327 // we need to refresh all the lines below the one which was deleted
1329 if ( item
> 0 && GetItemCount() )
1331 GetItemRect(item
- 1, rectItem
);
1336 rectItem
.height
= 0;
1339 wxRect rectWin
= GetRect();
1340 rectWin
.height
= rectWin
.GetBottom() - rectItem
.GetBottom();
1341 rectWin
.y
= rectItem
.GetBottom();
1343 RefreshRect(rectWin
);
1349 // Deletes all items
1350 bool wxListCtrl::DeleteAllItems()
1352 FreeAllInternalData();
1353 return ListView_DeleteAllItems(GetHwnd()) != 0;
1356 // Deletes all items
1357 bool wxListCtrl::DeleteAllColumns()
1359 while ( m_colCount
> 0 )
1361 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1363 wxLogLastError(wxT("ListView_DeleteColumn"));
1371 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1377 bool wxListCtrl::DeleteColumn(int col
)
1379 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1381 if ( success
&& (m_colCount
> 0) )
1386 // Clears items, and columns if there are any.
1387 void wxListCtrl::ClearAll()
1390 if ( m_colCount
> 0 )
1394 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1396 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1398 // ListView_EditLabel requires that the list has focus.
1401 WXHWND hWnd
= (WXHWND
) ListView_EditLabel(GetHwnd(), item
);
1404 // failed to start editing
1408 // [re]create the text control wrapping the HWND we got
1411 m_textCtrl
->UnsubclassWin();
1412 m_textCtrl
->SetHWND(0);
1416 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1417 m_textCtrl
->SetHWND(hWnd
);
1418 m_textCtrl
->SubclassWin(hWnd
);
1419 m_textCtrl
->SetParent(this);
1421 // we must disallow TABbing away from the control while the edit contol is
1422 // shown because this leaves it in some strange state (just try removing
1423 // this line and then pressing TAB while editing an item in listctrl
1425 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle() | wxTE_PROCESS_TAB
);
1430 // End label editing, optionally cancelling the edit
1431 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1433 wxFAIL_MSG( _T("not implemented") );
1438 // Ensures this item is visible
1439 bool wxListCtrl::EnsureVisible(long item
)
1441 return ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0;
1444 // Find an item whose label matches this string, starting from the item after 'start'
1445 // or the beginning if 'start' is -1.
1446 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1448 LV_FINDINFO findInfo
;
1450 findInfo
.flags
= LVFI_STRING
;
1452 findInfo
.flags
|= LVFI_PARTIAL
;
1455 // ListView_FindItem() excludes the first item from search and to look
1456 // through all the items you need to start from -1 which is unnatural and
1457 // inconsistent with the generic version - so we adjust the index
1460 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1463 // Find an item whose data matches this data, starting from the item after 'start'
1464 // or the beginning if 'start' is -1.
1465 // NOTE : Lindsay Mathieson - 14-July-2002
1466 // No longer use ListView_FindItem as the data attribute is now stored
1467 // in a wxListItemInternalData structure refernced by the actual lParam
1468 long wxListCtrl::FindItem(long start
, long data
)
1470 long idx
= start
+ 1;
1471 long count
= GetItemCount();
1475 if (GetItemData(idx
) == data
)
1483 // Find an item nearest this position in the specified direction, starting from
1484 // the item after 'start' or the beginning if 'start' is -1.
1485 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1487 LV_FINDINFO findInfo
;
1489 findInfo
.flags
= LVFI_NEARESTXY
;
1490 findInfo
.pt
.x
= pt
.x
;
1491 findInfo
.pt
.y
= pt
.y
;
1492 findInfo
.vkDirection
= VK_RIGHT
;
1494 if ( direction
== wxLIST_FIND_UP
)
1495 findInfo
.vkDirection
= VK_UP
;
1496 else if ( direction
== wxLIST_FIND_DOWN
)
1497 findInfo
.vkDirection
= VK_DOWN
;
1498 else if ( direction
== wxLIST_FIND_LEFT
)
1499 findInfo
.vkDirection
= VK_LEFT
;
1500 else if ( direction
== wxLIST_FIND_RIGHT
)
1501 findInfo
.vkDirection
= VK_RIGHT
;
1503 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1506 // Determines which item (if any) is at the specified point,
1507 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1508 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1510 LV_HITTESTINFO hitTestInfo
;
1511 hitTestInfo
.pt
.x
= (int) point
.x
;
1512 hitTestInfo
.pt
.y
= (int) point
.y
;
1514 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1517 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1518 flags
|= wxLIST_HITTEST_ABOVE
;
1519 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1520 flags
|= wxLIST_HITTEST_BELOW
;
1521 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1522 flags
|= wxLIST_HITTEST_NOWHERE
;
1523 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1524 flags
|= wxLIST_HITTEST_ONITEMICON
;
1525 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1526 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1527 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1528 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1529 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1530 flags
|= wxLIST_HITTEST_TOLEFT
;
1531 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1532 flags
|= wxLIST_HITTEST_TORIGHT
;
1534 return (long) hitTestInfo
.iItem
;
1537 // Inserts an item, returning the index of the new item if successful,
1539 long wxListCtrl::InsertItem(wxListItem
& info
)
1541 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1544 wxConvertToMSWListItem(this, info
, item
);
1545 item
.mask
&= ~LVIF_PARAM
;
1547 // check wether we need to allocate our internal data
1548 bool needInternalData
= ((info
.m_mask
& wxLIST_MASK_DATA
) || info
.HasAttributes());
1549 if (needInternalData
)
1551 m_AnyInternalData
= TRUE
;
1552 item
.mask
|= LVIF_PARAM
;
1554 // internal stucture that manages data
1555 wxListItemInternalData
*data
= new wxListItemInternalData();
1556 item
.lParam
= (LPARAM
) data
;
1558 if (info
.m_mask
& wxLIST_MASK_DATA
)
1559 data
->lParam
= info
.m_data
;
1561 // check whether it has any custom attributes
1562 if ( info
.HasAttributes() )
1564 // take copy of attributes
1565 data
->attr
= new wxListItemAttr(*info
.GetAttributes());
1569 long rv
= ListView_InsertItem(GetHwnd(), & item
);
1572 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
1573 wxT("m_count should match ListView_GetItemCount"));
1578 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1581 info
.m_text
= label
;
1582 info
.m_mask
= wxLIST_MASK_TEXT
;
1583 info
.m_itemId
= index
;
1584 return InsertItem(info
);
1587 // Inserts an image item
1588 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1591 info
.m_image
= imageIndex
;
1592 info
.m_mask
= wxLIST_MASK_IMAGE
;
1593 info
.m_itemId
= index
;
1594 return InsertItem(info
);
1597 // Inserts an image/string item
1598 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1601 info
.m_image
= imageIndex
;
1602 info
.m_text
= label
;
1603 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1604 info
.m_itemId
= index
;
1605 return InsertItem(info
);
1608 // For list view mode (only), inserts a column.
1609 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1612 wxConvertToMSWListCol(col
, item
, lvCol
);
1614 if ( !(lvCol
.mask
& LVCF_WIDTH
) )
1616 // always give some width to the new column: this one is compatible
1617 // with the generic version
1618 lvCol
.mask
|= LVCF_WIDTH
;
1622 long n
= ListView_InsertColumn(GetHwnd(), col
, &lvCol
);
1627 else // failed to insert?
1629 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1636 long wxListCtrl::InsertColumn(long col
,
1637 const wxString
& heading
,
1642 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1643 item
.m_text
= heading
;
1646 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1647 item
.m_width
= width
;
1649 item
.m_format
= format
;
1651 return InsertColumn(col
, item
);
1654 // scroll the control by the given number of pixels (exception: in list view,
1655 // dx is interpreted as number of columns)
1656 bool wxListCtrl::ScrollList(int dx
, int dy
)
1658 if ( !ListView_Scroll(GetHwnd(), dx
, dy
) )
1660 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx
, dy
);
1670 // fn is a function which takes 3 long arguments: item1, item2, data.
1671 // item1 is the long data associated with a first item (NOT the index).
1672 // item2 is the long data associated with a second item (NOT the index).
1673 // data is the same value as passed to SortItems.
1674 // The return value is a negative number if the first item should precede the second
1675 // item, a positive number of the second item should precede the first,
1676 // or zero if the two items are equivalent.
1678 // data is arbitrary data to be passed to the sort function.
1680 // Internal structures for proxying the user compare function
1681 // so that we can pass it the *real* user data
1683 // translate lParam data and call user func
1684 struct wxInternalDataSort
1686 wxListCtrlCompare user_fn
;
1690 int CALLBACK
wxInternalDataCompareFunc(LPARAM lParam1
, LPARAM lParam2
, LPARAM lParamSort
)
1692 struct wxInternalDataSort
*internalData
= (struct wxInternalDataSort
*) lParamSort
;
1694 wxListItemInternalData
*data1
= (wxListItemInternalData
*) lParam1
;
1695 wxListItemInternalData
*data2
= (wxListItemInternalData
*) lParam2
;
1697 long d1
= (data1
== NULL
? 0 : data1
->lParam
);
1698 long d2
= (data2
== NULL
? 0 : data2
->lParam
);
1700 return internalData
->user_fn(d1
, d2
, internalData
->data
);
1704 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1706 struct wxInternalDataSort internalData
;
1707 internalData
.user_fn
= fn
;
1708 internalData
.data
= data
;
1710 // WPARAM cast is needed for mingw/cygwin
1711 if ( !ListView_SortItems(GetHwnd(),
1712 wxInternalDataCompareFunc
,
1713 (WPARAM
) &internalData
) )
1715 wxLogDebug(_T("ListView_SortItems() failed"));
1725 // ----------------------------------------------------------------------------
1726 // message processing
1727 // ----------------------------------------------------------------------------
1729 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1731 if (cmd
== EN_UPDATE
)
1733 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1734 event
.SetEventObject( this );
1735 ProcessCommand(event
);
1738 else if (cmd
== EN_KILLFOCUS
)
1740 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1741 event
.SetEventObject( this );
1742 ProcessCommand(event
);
1749 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1752 // prepare the event
1753 // -----------------
1755 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1756 event
.SetEventObject(this);
1758 wxEventType eventType
= wxEVT_NULL
;
1760 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1762 // if your compiler is as broken as this, you should really change it: this
1763 // code is needed for normal operation! #ifdef below is only useful for
1764 // automatic rebuilds which are done with a very old compiler version
1765 #ifdef HDN_BEGINTRACKA
1767 // check for messages from the header (in report view)
1768 HWND hwndHdr
= ListView_GetHeader(GetHwnd());
1770 // is it a message from the header?
1771 if ( nmhdr
->hwndFrom
== hwndHdr
)
1773 HD_NOTIFY
*nmHDR
= (HD_NOTIFY
*)nmhdr
;
1775 event
.m_itemIndex
= -1;
1777 switch ( nmhdr
->code
)
1779 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1780 // TRACK messages even to ANSI programs: on my system I get
1781 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1783 // work around is to simply catch both versions and hope that it
1784 // works (why should this message exist in ANSI and Unicode is
1785 // beyond me as it doesn't deal with strings at all...)
1787 // note that fr HDN_TRACK another possibility could be to use
1788 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1789 // something other than the item width changes so we'd have to
1790 // filter out the unwanted events then
1791 case HDN_BEGINTRACKA
:
1792 case HDN_BEGINTRACKW
:
1793 eventType
= wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
;
1798 if ( eventType
== wxEVT_NULL
)
1799 eventType
= wxEVT_COMMAND_LIST_COL_DRAGGING
;
1804 if ( eventType
== wxEVT_NULL
)
1805 eventType
= wxEVT_COMMAND_LIST_COL_END_DRAG
;
1807 event
.m_item
.m_width
= nmHDR
->pitem
->cxy
;
1808 event
.m_col
= nmHDR
->iItem
;
1812 case GN_CONTEXTMENU
:
1813 #endif //__WXWINCE__
1816 eventType
= wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
;
1819 // find the column clicked: we have to search for it
1820 // ourselves as the notification message doesn't provide
1823 // where did the click occur?
1826 if(nmhdr
->code
== GN_CONTEXTMENU
) {
1827 ptClick
= ((NMRGINFO
*)nmhdr
)->ptAction
;
1829 #endif //__WXWINCE__
1830 if ( !::GetCursorPos(&ptClick
) )
1832 wxLogLastError(_T("GetCursorPos"));
1835 if ( !::ScreenToClient(hwndHdr
, &ptClick
) )
1837 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1840 event
.m_pointDrag
.x
= ptClick
.x
;
1841 event
.m_pointDrag
.y
= ptClick
.y
;
1843 int colCount
= Header_GetItemCount(hwndHdr
);
1846 for ( int col
= 0; col
< colCount
; col
++ )
1848 if ( Header_GetItemRect(hwndHdr
, col
, &rect
) )
1850 if ( ::PtInRect(&rect
, ptClick
) )
1860 case HDN_GETDISPINFOW
:
1862 LPNMHDDISPINFOW info
= (LPNMHDDISPINFOW
) lParam
;
1863 // This is a fix for a strange bug under XP.
1864 // Normally, info->iItem is a valid index, but
1865 // sometimes this is a silly (large) number
1866 // and when we return FALSE via wxControl::MSWOnNotify
1867 // to indicate that it hasn't yet been processed,
1868 // there's a GPF in Windows.
1869 // By returning TRUE here, we avoid further processing
1870 // of this strange message.
1871 if ( info
->iItem
>= GetColumnCount() )
1877 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1881 #endif // defined(HDN_BEGINTRACKA)
1882 if ( nmhdr
->hwndFrom
== GetHwnd() )
1884 // almost all messages use NM_LISTVIEW
1885 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1887 const int iItem
= nmLV
->iItem
;
1890 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1891 // ignored for efficiency. It is done here because the internal data is in the
1892 // process of being deleted so we don't want to try and access it below.
1893 if ( m_ignoreChangeMessages
&&
1894 ( (nmLV
->hdr
.code
== LVN_ITEMCHANGED
) || (nmLV
->hdr
.code
== LVN_ITEMCHANGING
)))
1900 // If we have a valid item then check if there is a data value
1901 // associated with it and put it in the event.
1902 if ( iItem
>= 0 && iItem
< GetItemCount() )
1904 wxListItemInternalData
*internaldata
=
1905 wxGetInternalData(GetHwnd(), iItem
);
1908 event
.m_item
.m_data
= internaldata
->lParam
;
1912 switch ( nmhdr
->code
)
1914 case LVN_BEGINRDRAG
:
1915 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1919 if ( eventType
== wxEVT_NULL
)
1921 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1924 event
.m_itemIndex
= iItem
;
1925 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1926 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1929 // NB: we have to handle both *A and *W versions here because some
1930 // versions of comctl32.dll send ANSI messages even to the
1932 case LVN_BEGINLABELEDITA
:
1933 case LVN_BEGINLABELEDITW
:
1936 if ( nmhdr
->code
== LVN_BEGINLABELEDITA
)
1938 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1940 else // LVN_BEGINLABELEDITW
1942 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1945 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1946 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, item
);
1947 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1951 case LVN_ENDLABELEDITA
:
1952 case LVN_ENDLABELEDITW
:
1955 if ( nmhdr
->code
== LVN_ENDLABELEDITA
)
1957 item
.Init(((LV_DISPINFOA
*)lParam
)->item
);
1959 else // LVN_ENDLABELEDITW
1961 item
.Init(((LV_DISPINFOW
*)lParam
)->item
);
1964 // was editing cancelled?
1965 const LV_ITEM
& lvi
= (LV_ITEM
)item
;
1966 if ( !lvi
.pszText
|| lvi
.iItem
== -1 )
1968 // don't keep a stale wxTextCtrl around
1971 // EDIT control will be deleted by the list control itself so
1972 // prevent us from deleting it as well
1973 m_textCtrl
->UnsubclassWin();
1974 m_textCtrl
->SetHWND(0);
1979 event
.SetEditCanceled(true);
1982 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1983 wxConvertFromMSWListItem(NULL
, event
.m_item
, item
);
1984 event
.m_itemIndex
= event
.m_item
.m_itemId
;
1988 case LVN_COLUMNCLICK
:
1989 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1990 event
.m_itemIndex
= -1;
1991 event
.m_col
= nmLV
->iSubItem
;
1994 case LVN_DELETEALLITEMS
:
1996 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1997 event
.m_itemIndex
= -1;
2000 case LVN_DELETEITEM
:
2002 // this should be prevented by the post-processing code below,
2003 // but "just in case"
2006 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
2007 event
.m_itemIndex
= iItem
;
2008 // delete the assoicated internal data
2009 wxDeleteInternalData(this, iItem
);
2012 case LVN_SETDISPINFO
:
2014 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
2015 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2016 wxConvertFromMSWListItem(GetHwnd(), event
.m_item
, info
->item
);
2020 case LVN_INSERTITEM
:
2021 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
2022 event
.m_itemIndex
= iItem
;
2025 case LVN_ITEMCHANGED
:
2026 // we translate this catch all message into more interesting
2027 // (and more easy to process) wxWindows events
2029 // first of all, we deal with the state change events only and
2030 // only for valid items (item == -1 for the virtual list
2032 if ( nmLV
->uChanged
& LVIF_STATE
&& iItem
!= -1 )
2034 // temp vars for readability
2035 const UINT stOld
= nmLV
->uOldState
;
2036 const UINT stNew
= nmLV
->uNewState
;
2038 event
.m_item
.SetId(iItem
);
2039 event
.m_item
.SetMask(wxLIST_MASK_TEXT
|
2042 GetItem(event
.m_item
);
2044 // has the focus changed?
2045 if ( !(stOld
& LVIS_FOCUSED
) && (stNew
& LVIS_FOCUSED
) )
2047 eventType
= wxEVT_COMMAND_LIST_ITEM_FOCUSED
;
2048 event
.m_itemIndex
= iItem
;
2051 if ( (stNew
& LVIS_SELECTED
) != (stOld
& LVIS_SELECTED
) )
2053 if ( eventType
!= wxEVT_NULL
)
2055 // focus and selection have both changed: send the
2056 // focus event from here and the selection one
2058 event
.SetEventType(eventType
);
2059 (void)GetEventHandler()->ProcessEvent(event
);
2061 else // no focus event to send
2063 // then need to set m_itemIndex as it wasn't done
2065 event
.m_itemIndex
= iItem
;
2068 eventType
= stNew
& LVIS_SELECTED
2069 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2070 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
2074 if ( eventType
== wxEVT_NULL
)
2076 // not an interesting event for us
2084 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
2085 WORD wVKey
= info
->wVKey
;
2087 // get the current selection
2088 long lItem
= GetNextItem(-1,
2090 wxLIST_STATE_SELECTED
);
2092 // <Enter> or <Space> activate the selected item if any (but
2093 // not with Shift and/or Ctrl as then they have a predefined
2094 // meaning for the list view)
2096 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
2097 !(wxIsShiftDown() || wxIsCtrlDown()) )
2099 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2103 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
2105 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2106 // value which should be used as is
2107 int code
= wxCharCodeMSWToWX(wVKey
);
2108 event
.m_code
= code
? code
: wVKey
;
2112 event
.m_item
.m_itemId
= lItem
;
2116 // fill the other fields too
2117 event
.m_item
.m_text
= GetItemText(lItem
);
2118 event
.m_item
.m_data
= GetItemData(lItem
);
2124 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2126 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2131 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2132 // if it happened on an item (and not on empty place)
2139 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
2140 event
.m_itemIndex
= iItem
;
2141 event
.m_item
.m_text
= GetItemText(iItem
);
2142 event
.m_item
.m_data
= GetItemData(iItem
);
2146 case GN_CONTEXTMENU
:
2147 #endif //__WXWINCE__
2149 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2150 // don't do anything else
2151 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
2156 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2157 LV_HITTESTINFO lvhti
;
2158 wxZeroMemory(lvhti
);
2161 if(nmhdr
->code
== GN_CONTEXTMENU
) {
2162 lvhti
.pt
= ((NMRGINFO
*)nmhdr
)->ptAction
;
2164 #endif //__WXWINCE__
2165 ::GetCursorPos(&(lvhti
.pt
));
2166 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
2167 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
2169 if ( lvhti
.flags
& LVHT_ONITEM
)
2171 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
2172 event
.m_itemIndex
= lvhti
.iItem
;
2173 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
2174 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
2179 #ifdef NM_CUSTOMDRAW
2181 *result
= OnCustomDraw(lParam
);
2184 #endif // _WIN32_IE >= 0x300
2186 case LVN_ODCACHEHINT
:
2188 const NM_CACHEHINT
*cacheHint
= (NM_CACHEHINT
*)lParam
;
2190 eventType
= wxEVT_COMMAND_LIST_CACHE_HINT
;
2192 // we get some really stupid cache hints like ones for
2193 // items in range 0..0 for an empty control or, after
2194 // deleting an item, for items in invalid range -- filter
2196 if ( cacheHint
->iFrom
> cacheHint
->iTo
)
2199 event
.m_oldItemIndex
= cacheHint
->iFrom
;
2201 const long iMax
= GetItemCount();
2202 event
.m_itemIndex
= cacheHint
->iTo
< iMax
? cacheHint
->iTo
2207 case LVN_GETDISPINFO
:
2210 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
2212 LV_ITEM
& lvi
= info
->item
;
2213 long item
= lvi
.iItem
;
2215 if ( lvi
.mask
& LVIF_TEXT
)
2217 wxString text
= OnGetItemText(item
, lvi
.iSubItem
);
2218 wxStrncpy(lvi
.pszText
, text
, lvi
.cchTextMax
);
2221 // see comment at the end of wxListCtrl::GetColumn()
2222 #ifdef NM_CUSTOMDRAW
2223 if ( lvi
.mask
& LVIF_IMAGE
)
2225 lvi
.iImage
= OnGetItemImage(item
);
2227 #endif // NM_CUSTOMDRAW
2229 // a little dose of healthy paranoia: as we never use
2230 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2231 wxASSERT_MSG( !(lvi
.mask
& LVIF_STATE
),
2232 _T("we don't support state callbacks yet!") );
2239 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2244 // where did this one come from?
2248 // process the event
2249 // -----------------
2251 event
.SetEventType(eventType
);
2253 bool processed
= GetEventHandler()->ProcessEvent(event
);
2257 switch ( nmhdr
->code
)
2259 case LVN_DELETEALLITEMS
:
2260 // always return TRUE to suppress all additional LVN_DELETEITEM
2261 // notifications - this makes deleting all items from a list ctrl
2266 case LVN_ENDLABELEDITA
:
2267 case LVN_ENDLABELEDITW
:
2268 // logic here is inversed compared to all the other messages
2269 *result
= event
.IsAllowed();
2271 // don't keep a stale wxTextCtrl around
2274 // EDIT control will be deleted by the list control itself so
2275 // prevent us from deleting it as well
2276 m_textCtrl
->UnsubclassWin();
2277 m_textCtrl
->SetHWND(0);
2286 *result
= !event
.IsAllowed();
2291 // see comment at the end of wxListCtrl::GetColumn()
2292 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2294 WXLPARAM
wxListCtrl::OnCustomDraw(WXLPARAM lParam
)
2296 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
2297 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
2298 switch ( nmcd
.dwDrawStage
)
2301 // if we've got any items with non standard attributes,
2302 // notify us before painting each item
2304 // for virtual controls, always suppose that we have attributes as
2305 // there is no way to check for this
2306 return IsVirtual() || m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2309 case CDDS_ITEMPREPAINT
:
2311 size_t item
= (size_t)nmcd
.dwItemSpec
;
2312 if ( item
>= (size_t)GetItemCount() )
2314 // we get this message with item == 0 for an empty control,
2315 // we must ignore it as calling OnGetItemAttr() would be
2317 return CDRF_DODEFAULT
;
2320 wxListItemAttr
*attr
=
2321 IsVirtual() ? OnGetItemAttr(item
)
2322 : wxGetInternalDataAttr(this, item
);
2326 // nothing to do for this item
2327 return CDRF_DODEFAULT
;
2331 wxColour colText
, colBack
;
2332 if ( attr
->HasFont() )
2334 wxFont font
= attr
->GetFont();
2335 hFont
= (HFONT
)font
.GetResourceHandle();
2342 if ( attr
->HasTextColour() )
2344 colText
= attr
->GetTextColour();
2348 colText
= GetTextColour();
2351 if ( attr
->HasBackgroundColour() )
2353 colBack
= attr
->GetBackgroundColour();
2357 colBack
= GetBackgroundColour();
2360 lplvcd
->clrText
= wxColourToRGB(colText
);
2361 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
2363 // note that if we wanted to set colours for
2364 // individual columns (subitems), we would have
2365 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2368 ::SelectObject(nmcd
.hdc
, hFont
);
2370 return CDRF_NEWFONT
;
2373 // fall through to return CDRF_DODEFAULT
2376 return CDRF_DODEFAULT
;
2380 #endif // NM_CUSTOMDRAW supported
2382 // Necessary for drawing hrules and vrules, if specified
2383 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
2387 wxControl::OnPaint(event
);
2389 // Reset the device origin since it may have been set
2390 dc
.SetDeviceOrigin(0, 0);
2392 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
2393 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
2395 if (!drawHRules
&& !drawVRules
)
2397 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
2400 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2402 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2404 wxSize clientSize
= GetClientSize();
2408 int itemCount
= GetItemCount();
2412 long top
= GetTopItem();
2413 for (i
= top
; i
< top
+ GetCountPerPage() + 1; i
++)
2415 if (GetItemRect(i
, itemRect
))
2417 cy
= itemRect
.GetTop();
2418 if (i
!= 0) // Don't draw the first one
2420 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2423 if (i
== itemCount
- 1)
2425 cy
= itemRect
.GetBottom();
2426 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
2432 if (drawVRules
&& (i
> -1))
2434 wxRect firstItemRect
;
2435 GetItemRect(0, firstItemRect
);
2437 if (GetItemRect(i
, itemRect
))
2440 int x
= itemRect
.GetX();
2441 for (col
= 0; col
< GetColumnCount(); col
++)
2443 int colWidth
= GetColumnWidth(col
);
2445 dc
.DrawLine(x
-1, firstItemRect
.GetY() - 2, x
-1, itemRect
.GetBottom());
2451 // ----------------------------------------------------------------------------
2452 // virtual list controls
2453 // ----------------------------------------------------------------------------
2455 wxString
wxListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
2457 // this is a pure virtual function, in fact - which is not really pure
2458 // because the controls which are not virtual don't need to implement it
2459 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2461 return wxEmptyString
;
2464 int wxListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
2467 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2472 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
2474 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
2475 _T("invalid item index in OnGetItemAttr()") );
2477 // no attributes by default
2481 void wxListCtrl::SetItemCount(long count
)
2483 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2485 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT
, (WPARAM
)count
,
2486 LVSICF_NOSCROLL
| LVSICF_NOINVALIDATEALL
) )
2488 wxLogLastError(_T("ListView_SetItemCount"));
2491 wxASSERT_MSG( m_count
== ListView_GetItemCount(GetHwnd()),
2492 wxT("m_count should match ListView_GetItemCount"));
2495 void wxListCtrl::RefreshItem(long item
)
2497 // strangely enough, ListView_Update() results in much more flicker here
2498 // than a dumb Refresh() -- why?
2500 if ( !ListView_Update(GetHwnd(), item
) )
2502 wxLogLastError(_T("ListView_Update"));
2506 GetItemRect(item
, rect
);
2511 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
2513 wxRect rect1
, rect2
;
2514 GetItemRect(itemFrom
, rect1
);
2515 GetItemRect(itemTo
, rect2
);
2517 wxRect rect
= rect1
;
2518 rect
.height
= rect2
.GetBottom() - rect1
.GetTop();
2523 static wxListItemInternalData
*wxGetInternalData(HWND hwnd
, long itemId
)
2526 it
.mask
= LVIF_PARAM
;
2529 bool success
= ListView_GetItem(hwnd
, &it
) != 0;
2531 return (wxListItemInternalData
*) it
.lParam
;
2536 static wxListItemInternalData
*wxGetInternalData(wxListCtrl
*ctl
, long itemId
)
2538 return wxGetInternalData((HWND
) ctl
->GetHWND(), itemId
);
2541 static wxListItemAttr
*wxGetInternalDataAttr(wxListCtrl
*ctl
, long itemId
)
2543 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2550 static void wxDeleteInternalData(wxListCtrl
* ctl
, long itemId
)
2552 wxListItemInternalData
*data
= wxGetInternalData(ctl
, itemId
);
2556 memset(&item
, 0, sizeof(item
));
2557 item
.iItem
= itemId
;
2558 item
.mask
= LVIF_PARAM
;
2559 item
.lParam
= (LPARAM
) 0;
2560 ListView_SetItem((HWND
)ctl
->GetHWND(), &item
);
2565 static void wxConvertFromMSWListItem(HWND hwndListCtrl
,
2569 wxListItemInternalData
*internaldata
=
2570 (wxListItemInternalData
*) lvItem
.lParam
;
2573 info
.m_data
= internaldata
->lParam
;
2577 info
.m_stateMask
= 0;
2578 info
.m_itemId
= lvItem
.iItem
;
2580 long oldMask
= lvItem
.mask
;
2582 bool needText
= FALSE
;
2583 if (hwndListCtrl
!= 0)
2585 if ( lvItem
.mask
& LVIF_TEXT
)
2592 lvItem
.pszText
= new wxChar
[513];
2593 lvItem
.cchTextMax
= 512;
2595 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
2596 ::SendMessage(hwndListCtrl
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
2599 if ( lvItem
.mask
& LVIF_STATE
)
2601 info
.m_mask
|= wxLIST_MASK_STATE
;
2603 if ( lvItem
.stateMask
& LVIS_CUT
)
2605 info
.m_stateMask
|= wxLIST_STATE_CUT
;
2606 if ( lvItem
.state
& LVIS_CUT
)
2607 info
.m_state
|= wxLIST_STATE_CUT
;
2609 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
2611 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
2612 if ( lvItem
.state
& LVIS_DROPHILITED
)
2613 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
2615 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
2617 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
2618 if ( lvItem
.state
& LVIS_FOCUSED
)
2619 info
.m_state
|= wxLIST_STATE_FOCUSED
;
2621 if ( lvItem
.stateMask
& LVIS_SELECTED
)
2623 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
2624 if ( lvItem
.state
& LVIS_SELECTED
)
2625 info
.m_state
|= wxLIST_STATE_SELECTED
;
2629 if ( lvItem
.mask
& LVIF_TEXT
)
2631 info
.m_mask
|= wxLIST_MASK_TEXT
;
2632 info
.m_text
= lvItem
.pszText
;
2634 if ( lvItem
.mask
& LVIF_IMAGE
)
2636 info
.m_mask
|= wxLIST_MASK_IMAGE
;
2637 info
.m_image
= lvItem
.iImage
;
2639 if ( lvItem
.mask
& LVIF_PARAM
)
2640 info
.m_mask
|= wxLIST_MASK_DATA
;
2641 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
2642 info
.m_mask
|= wxLIST_SET_ITEM
;
2643 info
.m_col
= lvItem
.iSubItem
;
2648 delete[] lvItem
.pszText
;
2650 lvItem
.mask
= oldMask
;
2653 static void wxConvertToMSWFlags(long state
, long stateMask
, LV_ITEM
& lvItem
)
2655 if (stateMask
& wxLIST_STATE_CUT
)
2657 lvItem
.stateMask
|= LVIS_CUT
;
2658 if (state
& wxLIST_STATE_CUT
)
2659 lvItem
.state
|= LVIS_CUT
;
2661 if (stateMask
& wxLIST_STATE_DROPHILITED
)
2663 lvItem
.stateMask
|= LVIS_DROPHILITED
;
2664 if (state
& wxLIST_STATE_DROPHILITED
)
2665 lvItem
.state
|= LVIS_DROPHILITED
;
2667 if (stateMask
& wxLIST_STATE_FOCUSED
)
2669 lvItem
.stateMask
|= LVIS_FOCUSED
;
2670 if (state
& wxLIST_STATE_FOCUSED
)
2671 lvItem
.state
|= LVIS_FOCUSED
;
2673 if (stateMask
& wxLIST_STATE_SELECTED
)
2675 lvItem
.stateMask
|= LVIS_SELECTED
;
2676 if (state
& wxLIST_STATE_SELECTED
)
2677 lvItem
.state
|= LVIS_SELECTED
;
2681 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
,
2682 const wxListItem
& info
,
2685 lvItem
.iItem
= (int) info
.m_itemId
;
2687 lvItem
.iImage
= info
.m_image
;
2688 lvItem
.stateMask
= 0;
2691 lvItem
.iSubItem
= info
.m_col
;
2693 if (info
.m_mask
& wxLIST_MASK_STATE
)
2695 lvItem
.mask
|= LVIF_STATE
;
2697 wxConvertToMSWFlags(info
.m_state
, info
.m_stateMask
, lvItem
);
2700 if (info
.m_mask
& wxLIST_MASK_TEXT
)
2702 lvItem
.mask
|= LVIF_TEXT
;
2703 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
2705 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
2709 // pszText is not const, hence the cast
2710 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
2711 if ( lvItem
.pszText
)
2712 lvItem
.cchTextMax
= info
.m_text
.Length();
2714 lvItem
.cchTextMax
= 0;
2717 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2718 lvItem
.mask
|= LVIF_IMAGE
;
2721 static void wxConvertToMSWListCol(int WXUNUSED(col
), const wxListItem
& item
,
2724 wxZeroMemory(lvCol
);
2726 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
2728 lvCol
.mask
|= LVCF_TEXT
;
2729 lvCol
.pszText
= (wxChar
*)item
.m_text
.c_str(); // cast is safe
2732 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
2734 lvCol
.mask
|= LVCF_FMT
;
2736 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
2737 lvCol
.fmt
= LVCFMT_LEFT
;
2738 else if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
2739 lvCol
.fmt
= LVCFMT_RIGHT
;
2740 else if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
2741 lvCol
.fmt
= LVCFMT_CENTER
;
2744 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
2746 lvCol
.mask
|= LVCF_WIDTH
;
2747 if ( item
.m_width
== wxLIST_AUTOSIZE
)
2748 lvCol
.cx
= LVSCW_AUTOSIZE
;
2749 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2750 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
2752 lvCol
.cx
= item
.m_width
;
2755 // see comment at the end of wxListCtrl::GetColumn()
2756 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2757 if ( item
.m_mask
& wxLIST_MASK_IMAGE
)
2759 if ( wxTheApp
->GetComCtl32Version() >= 470 )
2761 lvCol
.mask
|= LVCF_IMAGE
| LVCF_FMT
;
2763 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2764 // seem to be generally nicer than on the left and the generic
2765 // version only draws them on the right (we don't have a flag to
2766 // specify the image location anyhow)
2768 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2769 // make any difference in my tests -- but maybe we should?
2770 lvCol
.fmt
|= LVCFMT_BITMAP_ON_RIGHT
| LVCFMT_IMAGE
;
2772 lvCol
.iImage
= item
.m_image
;
2774 //else: it doesn't support item images anyhow
2776 #endif // _WIN32_IE >= 0x0300
2779 #endif // wxUSE_LISTCTRL