]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listctrl.cpp
Patch wxGTK1 for recent changes in bmpbuttn
[wxWidgets.git] / src / msw / listctrl.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
648bec3e 2// Name: src/msw/listctrl.cpp
2bda0e17
KB
3// Purpose: wxListCtrl
4// Author: Julian Smart
164a7972 5// Modified by: Agron Selimaj
2bda0e17
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
0b5efdc7 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
a71d815b 27#if wxUSE_LISTCTRL
9f303149 28
e409b62a
PC
29#include "wx/listctrl.h"
30
2bda0e17 31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
9f303149 33 #include "wx/app.h"
9f303149
VZ
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/settings.h"
ed4b0fdc 37 #include "wx/dcclient.h"
fec9cc08 38 #include "wx/textctrl.h"
2bda0e17
KB
39#endif
40
de8e98f1 41#include "wx/imaglist.h"
ceef3893 42#include "wx/vector.h"
2bda0e17
KB
43
44#include "wx/msw/private.h"
45
781a24e8 46#if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
80a81a12
JS
47 #include <ole2.h>
48 #include <shellapi.h>
2d36b3d8
JS
49 #if _WIN32_WCE < 400
50 #include <aygshell.h>
51 #endif
80a81a12
JS
52#endif
53
2f73fd38
MW
54// Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
55// it by its old name NM_FINDTIEM.
56//
73f91e1e 57#if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
2f73fd38 58 #define HAVE_NMLVFINDITEM 1
73f91e1e 59#elif defined(__DMC__) || defined(NM_FINDITEM)
206391cb
VZ
60 #define HAVE_NMLVFINDITEM 1
61 #define NMLVFINDITEM NM_FINDITEM
2f73fd38
MW
62#endif
63
edccf428
VZ
64// ----------------------------------------------------------------------------
65// private functions
66// ----------------------------------------------------------------------------
2bda0e17 67
8dff2b5c
VZ
68// convert our state and mask flags to LV_ITEM constants
69static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem);
70
71// convert wxListItem to LV_ITEM
72static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
73 const wxListItem& info, LV_ITEM& lvItem);
74
75// convert LV_ITEM to wxListItem
76static void wxConvertFromMSWListItem(HWND hwndListCtrl,
77 wxListItem& info,
78 /* const */ LV_ITEM& lvItem);
2bda0e17 79
6f806543 80// convert our wxListItem to LV_COLUMN
e64658ed
VZ
81static void wxConvertToMSWListCol(HWND hwndList,
82 int col,
83 const wxListItem& item,
6f806543
VZ
84 LV_COLUMN& lvCol);
85
89cafab0
VZ
86namespace
87{
88
89// replacement for ListView_GetSubItemRect() which provokes warnings like
90// "the address of 'rc' will always evaluate as 'true'" when used with mingw32
91// 4.3+
92//
93// this function does no error checking on item and subitem parameters, notice
67561862
VZ
94// that subitem 0 means the whole item so there is no way to retrieve the
95// rectangle of the first subitem using this function, in particular notice
96// that the index is *not* 1-based, in spite of what MSDN says
89cafab0
VZ
97inline bool
98wxGetListCtrlSubItemRect(HWND hwnd, int item, int subitem, int flags, RECT& rect)
99{
100 rect.top = subitem;
101 rect.left = flags;
102 return ::SendMessage(hwnd, LVM_GETSUBITEMRECT, item, (LPARAM)&rect) != 0;
103}
104
105inline bool
106wxGetListCtrlItemRect(HWND hwnd, int item, int flags, RECT& rect)
107{
108 return wxGetListCtrlSubItemRect(hwnd, item, 0, flags, rect);
109}
110
111} // anonymous namespace
112
8c21905b
VS
113// ----------------------------------------------------------------------------
114// private helper classes
115// ----------------------------------------------------------------------------
116
117// We have to handle both fooW and fooA notifications in several cases
d2ed74b9 118// because of broken comctl32.dll and/or unicows.dll. This class is used to
73d8c5fd 119// convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
8c21905b
VS
120// LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
121// by wxConvertToMSWListItem().
d2ed74b9
VZ
122#if wxUSE_UNICODE
123 #define LV_ITEM_NATIVE LV_ITEMW
124 #define LV_ITEM_OTHER LV_ITEMA
125
126 #define LV_CONV_TO_WX cMB2WX
127 #define LV_CONV_BUF wxMB2WXbuf
128#else // ANSI
129 #define LV_ITEM_NATIVE LV_ITEMA
130 #define LV_ITEM_OTHER LV_ITEMW
131
132 #define LV_CONV_TO_WX cWC2WX
133 #define LV_CONV_BUF wxWC2WXbuf
134#endif // Unicode/ANSI
135
8c21905b
VS
136class wxLV_ITEM
137{
138public:
d2ed74b9
VZ
139 // default ctor, use Init() later
140 wxLV_ITEM() { m_buf = NULL; m_pItem = NULL; }
8c21905b 141
d2ed74b9
VZ
142 // init without conversion
143 void Init(LV_ITEM_NATIVE& item)
8c21905b 144 {
d2ed74b9
VZ
145 wxASSERT_MSG( !m_pItem, _T("Init() called twice?") );
146
147 m_pItem = &item;
73d8c5fd 148 }
8c21905b 149
d2ed74b9 150 // init with conversion
fbfb8bcc 151 void Init(const LV_ITEM_OTHER& item)
8c21905b 152 {
d2ed74b9
VZ
153 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
154 // point to our own m_item
2b5f62a0 155
1c6f2414
WS
156 // memcpy() can't work if the struct sizes are different
157 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER) == sizeof(LV_ITEM_NATIVE),
158 CodeCantWorkIfDiffSizes);
159
d2ed74b9
VZ
160 memcpy(&m_item, &item, sizeof(LV_ITEM_NATIVE));
161
162 // convert text from ANSI to Unicod if necessary
8c21905b
VS
163 if ( (item.mask & LVIF_TEXT) && item.pszText )
164 {
d2ed74b9
VZ
165 m_buf = new LV_CONV_BUF(wxConvLocal.LV_CONV_TO_WX(item.pszText));
166 m_item.pszText = (wxChar *)m_buf->data();
8c21905b 167 }
73d8c5fd 168 }
d2ed74b9
VZ
169
170 // ctor without conversion
171 wxLV_ITEM(LV_ITEM_NATIVE& item) : m_buf(NULL), m_pItem(&item) { }
172
173 // ctor with conversion
174 wxLV_ITEM(LV_ITEM_OTHER& item) : m_buf(NULL)
175 {
176 Init(item);
177 }
178
179 ~wxLV_ITEM() { delete m_buf; }
180
181 // conversion to the real LV_ITEM
182 operator LV_ITEM_NATIVE&() const { return *m_pItem; }
183
8c21905b 184private:
d2ed74b9 185 LV_CONV_BUF *m_buf;
8c21905b 186
d2ed74b9
VZ
187 LV_ITEM_NATIVE *m_pItem;
188 LV_ITEM_NATIVE m_item;
22f3361e 189
c0c133e1 190 wxDECLARE_NO_COPY_CLASS(wxLV_ITEM);
8c21905b
VS
191};
192
7cf46fdb
VZ
193///////////////////////////////////////////////////////
194// Problem:
73d8c5fd
RD
195// The MSW version had problems with SetTextColour() et
196// al as the wxListItemAttr's were stored keyed on the
197// item index. If a item was inserted anywhere but the end
c767a5bb 198// of the list the text attributes (colour etc) for
7cf46fdb 199// the following items were out of sync.
73d8c5fd 200//
7cf46fdb 201// Solution:
73d8c5fd 202// Under MSW the only way to associate data with a List
43e8916f 203// item independent of its position in the list is to
73d8c5fd
RD
204// store a pointer to it in its lParam attribute. However
205// user programs are already using this (via the
7cf46fdb 206// SetItemData() GetItemData() calls).
73d8c5fd
RD
207//
208// However what we can do is store a pointer to a
209// structure which contains the attributes we want *and*
c767a5bb 210// a lParam -- and this is what wxMSWListItemData does.
73d8c5fd 211//
c767a5bb 212// To conserve memory, a wxMSWListItemData is
73d8c5fd 213// only allocated for a LV_ITEM if text attributes or
7cf46fdb 214// user data(lparam) are being set.
c767a5bb 215class wxMSWListItemData
7cf46fdb
VZ
216{
217public:
c767a5bb
VZ
218 wxMSWListItemData() : attr(NULL), lParam(0) {}
219 ~wxMSWListItemData() { delete attr; }
7cf46fdb 220
c767a5bb
VZ
221 wxListItemAttr *attr;
222 LPARAM lParam; // real user data
22f3361e 223
c767a5bb 224 wxDECLARE_NO_COPY_CLASS(wxMSWListItemData);
7cf46fdb
VZ
225};
226
227// Get the internal data structure
c767a5bb
VZ
228static wxMSWListItemData *wxGetInternalData(HWND hwnd, long itemId);
229static wxMSWListItemData *wxGetInternalData(const wxListCtrl *ctl, long itemId);
7cf46fdb
VZ
230
231
786a2425 232#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
233WX_DEFINE_FLAGS( wxListCtrlStyle )
234
321239b6 235wxBEGIN_FLAGS( wxListCtrlStyle )
bc9fb572
JS
236 // new style border flags, we put them first to
237 // use them for streaming out
321239b6
SC
238 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
239 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
240 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
241 wxFLAGS_MEMBER(wxBORDER_RAISED)
242 wxFLAGS_MEMBER(wxBORDER_STATIC)
243 wxFLAGS_MEMBER(wxBORDER_NONE)
598ddd96 244
bc9fb572 245 // old style border flags
321239b6
SC
246 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
247 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
248 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
249 wxFLAGS_MEMBER(wxRAISED_BORDER)
250 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 251 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
252
253 // standard window styles
321239b6
SC
254 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
255 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
256 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
257 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 258 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
321239b6
SC
259 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
260 wxFLAGS_MEMBER(wxVSCROLL)
261 wxFLAGS_MEMBER(wxHSCROLL)
262
263 wxFLAGS_MEMBER(wxLC_LIST)
264 wxFLAGS_MEMBER(wxLC_REPORT)
265 wxFLAGS_MEMBER(wxLC_ICON)
266 wxFLAGS_MEMBER(wxLC_SMALL_ICON)
267 wxFLAGS_MEMBER(wxLC_ALIGN_TOP)
268 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT)
269 wxFLAGS_MEMBER(wxLC_AUTOARRANGE)
270 wxFLAGS_MEMBER(wxLC_USER_TEXT)
271 wxFLAGS_MEMBER(wxLC_EDIT_LABELS)
272 wxFLAGS_MEMBER(wxLC_NO_HEADER)
273 wxFLAGS_MEMBER(wxLC_SINGLE_SEL)
274 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING)
275 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING)
276 wxFLAGS_MEMBER(wxLC_VIRTUAL)
277
278wxEND_FLAGS( wxListCtrlStyle )
bc9fb572 279
786a2425
SC
280IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h")
281
321239b6 282wxBEGIN_PROPERTIES_TABLE(wxListCtrl)
598ddd96 283 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
c5ca409b 284
af498247 285 wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
321239b6 286wxEND_PROPERTIES_TABLE()
786a2425 287
321239b6
SC
288wxBEGIN_HANDLERS_TABLE(wxListCtrl)
289wxEND_HANDLERS_TABLE()
786a2425 290
598ddd96 291wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
786a2425
SC
292
293/*
9d55bfef 294 TODO : Expose more information of a list's layout etc. via appropriate objects (a la NotebookPageInfo)
786a2425
SC
295*/
296#else
8f177c8e 297IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
786a2425
SC
298#endif
299
bf9b6266 300IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
8f177c8e 301IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2bda0e17 302
2d33aec9
VZ
303IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
304
2702ed5a
JS
305BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
306 EVT_PAINT(wxListCtrl::OnPaint)
307END_EVENT_TABLE()
308
edccf428
VZ
309// ============================================================================
310// implementation
311// ============================================================================
312
313// ----------------------------------------------------------------------------
314// wxListCtrl construction
315// ----------------------------------------------------------------------------
316
bdc72a22 317void wxListCtrl::Init()
2bda0e17 318{
c767a5bb
VZ
319 m_imageListNormal =
320 m_imageListSmall =
0b5efdc7 321 m_imageListState = NULL;
c767a5bb
VZ
322 m_ownsImageListNormal =
323 m_ownsImageListSmall =
324 m_ownsImageListState = false;
325
2bda0e17 326 m_colCount = 0;
68c124a1 327 m_count = 0;
bbcdf8bc 328 m_textCtrl = NULL;
c767a5bb 329
598ddd96 330 m_hasAnyAttr = false;
2bda0e17
KB
331}
332
0b5efdc7
VZ
333bool wxListCtrl::Create(wxWindow *parent,
334 wxWindowID id,
335 const wxPoint& pos,
336 const wxSize& size,
337 long style,
97c611f8 338 const wxValidator& validator,
0b5efdc7 339 const wxString& name)
2bda0e17 340{
97c611f8 341 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
598ddd96 342 return false;
11b6a93b 343
f31a4098 344 if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) )
598ddd96 345 return false;
2bda0e17 346
97c611f8
VZ
347 // explicitly say that we want to use Unicode because otherwise we get ANSI
348 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
9a63feff 349 wxSetCCUnicodeFormat(GetHwnd());
2bda0e17 350
6e21a3db
KH
351 // We must set the default text colour to the system/theme color, otherwise
352 // GetTextColour will always return black
353 SetTextColour(GetDefaultAttributes().colFg);
354
04d24675
VZ
355 if ( InReportView() )
356 MSWSetExListStyles();
357
358 return true;
359}
360
361void wxListCtrl::MSWSetExListStyles()
362{
16a0ccd0
VZ
363 // for comctl32.dll v 4.70+ we want to have some non default extended
364 // styles because it's prettier (and also because wxGTK does it like this)
04d24675 365 if ( wxApp::GetComCtl32Version() >= 470 )
97c611f8 366 {
67d3fc49
VZ
367 ::SendMessage
368 (
369 GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
2036bdd3
VZ
370 // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's
371 // not defined in the SDK headers
372#ifdef LVS_EX_LABELTIP
67d3fc49 373 LVS_EX_LABELTIP |
2036bdd3 374#endif
67d3fc49
VZ
375 LVS_EX_FULLROWSELECT |
376 LVS_EX_SUBITEMIMAGES |
377 // normally this should be governed by a style as it's probably not
378 // always appropriate, but we don't have any free styles left and
379 // it seems better to enable it by default than disable
380 LVS_EX_HEADERDRAGDROP
381 );
97c611f8 382 }
97c611f8 383}
2bda0e17 384
97c611f8
VZ
385WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
386{
387 WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle);
2bda0e17 388
97c611f8 389 wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
b0766406 390
4b6a582b 391#if wxDEBUG_LEVEL
97c611f8 392 size_t nModes = 0;
edccf428 393
97c611f8
VZ
394 #define MAP_MODE_STYLE(wx, ms) \
395 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
4b6a582b 396#else // !wxDEBUG_LEVEL
97c611f8
VZ
397 #define MAP_MODE_STYLE(wx, ms) \
398 if ( style & (wx) ) wstyle |= (ms);
4b6a582b 399#endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
edccf428 400
97c611f8
VZ
401 MAP_MODE_STYLE(wxLC_ICON, LVS_ICON)
402 MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON)
403 MAP_MODE_STYLE(wxLC_LIST, LVS_LIST)
404 MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT)
edccf428 405
97c611f8
VZ
406 wxASSERT_MSG( nModes == 1,
407 _T("wxListCtrl style should have exactly one mode bit set") );
edccf428 408
97c611f8 409#undef MAP_MODE_STYLE
2bda0e17 410
97c611f8
VZ
411 if ( style & wxLC_ALIGN_LEFT )
412 wstyle |= LVS_ALIGNLEFT;
2bda0e17 413
97c611f8
VZ
414 if ( style & wxLC_ALIGN_TOP )
415 wstyle |= LVS_ALIGNTOP;
2bda0e17 416
97c611f8
VZ
417 if ( style & wxLC_AUTOARRANGE )
418 wstyle |= LVS_AUTOARRANGE;
f8352807 419
97c611f8
VZ
420 if ( style & wxLC_NO_SORT_HEADER )
421 wstyle |= LVS_NOSORTHEADER;
0b5efdc7 422
97c611f8
VZ
423 if ( style & wxLC_NO_HEADER )
424 wstyle |= LVS_NOCOLUMNHEADER;
0b5efdc7 425
97c611f8
VZ
426 if ( style & wxLC_EDIT_LABELS )
427 wstyle |= LVS_EDITLABELS;
bfc8138f 428
97c611f8
VZ
429 if ( style & wxLC_SINGLE_SEL )
430 wstyle |= LVS_SINGLESEL;
431
432 if ( style & wxLC_SORT_ASCENDING )
0b5efdc7 433 {
97c611f8
VZ
434 wstyle |= LVS_SORTASCENDING;
435
436 wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING),
437 _T("can't sort in ascending and descending orders at once") );
0b5efdc7 438 }
97c611f8
VZ
439 else if ( style & wxLC_SORT_DESCENDING )
440 wstyle |= LVS_SORTDESCENDING;
f8352807 441
97c611f8
VZ
442#if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
443 if ( style & wxLC_VIRTUAL )
444 {
2a1f999f 445 int ver = wxApp::GetComCtl32Version();
97c611f8
VZ
446 if ( ver < 470 )
447 {
448 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."),
449 ver / 100, ver % 100);
450 }
2bda0e17 451
97c611f8
VZ
452 wstyle |= LVS_OWNERDATA;
453 }
454#endif // ancient cygwin
2bda0e17 455
97c611f8 456 return wstyle;
2bda0e17
KB
457}
458
edccf428
VZ
459void wxListCtrl::UpdateStyle()
460{
97c611f8 461 if ( GetHwnd() )
edccf428
VZ
462 {
463 // The new window view style
97c611f8 464 DWORD dwStyleNew = MSWGetStyle(m_windowStyle, NULL);
edccf428 465
6e2fef03
VZ
466 // some styles are not returned by MSWGetStyle()
467 if ( IsShown() )
468 dwStyleNew |= WS_VISIBLE;
469
910484a6 470 // Get the current window style.
edccf428
VZ
471 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
472
6e2fef03
VZ
473 // we don't have wxVSCROLL style, but the list control may have it,
474 // don't change it then
475 dwStyleNew |= dwStyleOld & (WS_HSCROLL | WS_VSCROLL);
476
910484a6 477 // Only set the window style if the view bits have changed.
edccf428
VZ
478 if ( dwStyleOld != dwStyleNew )
479 {
480 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
04d24675
VZ
481
482 // if we switched to the report view, set the extended styles for
483 // it too
484 if ( !(dwStyleOld & LVS_REPORT) && (dwStyleNew & LVS_REPORT) )
485 MSWSetExListStyles();
edccf428
VZ
486 }
487 }
488}
489
7cf46fdb 490void wxListCtrl::FreeAllInternalData()
2bda0e17 491{
c767a5bb
VZ
492 const unsigned count = m_internalData.size();
493 for ( unsigned n = 0; n < count; n++ )
494 delete m_internalData[n];
6149de71 495
c767a5bb 496 m_internalData.clear();
6932a32c 497}
d62228a6 498
5cd4cb75 499void wxListCtrl::DeleteEditControl()
6932a32c 500{
d62228a6 501 if ( m_textCtrl )
bbcdf8bc 502 {
7bf1474a 503 m_textCtrl->UnsubclassWin();
f3076f9f 504 m_textCtrl->SetHWND(0);
0b5efdc7
VZ
505 delete m_textCtrl;
506 m_textCtrl = NULL;
bbcdf8bc 507 }
5cd4cb75
VZ
508}
509
510wxListCtrl::~wxListCtrl()
511{
512 FreeAllInternalData();
513
514 DeleteEditControl();
33ac7e6f 515
07763c99
VZ
516 if (m_ownsImageListNormal)
517 delete m_imageListNormal;
518 if (m_ownsImageListSmall)
519 delete m_imageListSmall;
520 if (m_ownsImageListState)
521 delete m_imageListState;
2bda0e17
KB
522}
523
edccf428
VZ
524// ----------------------------------------------------------------------------
525// set/get/change style
526// ----------------------------------------------------------------------------
527
2bda0e17 528// Add or remove a single window style
debe6624 529void wxListCtrl::SetSingleStyle(long style, bool add)
2bda0e17 530{
0b5efdc7 531 long flag = GetWindowStyleFlag();
2bda0e17 532
0b5efdc7 533 // Get rid of conflicting styles
acb62b84
VZ
534 if ( add )
535 {
0b5efdc7 536 if ( style & wxLC_MASK_TYPE)
edccf428 537 flag = flag & ~wxLC_MASK_TYPE;
0b5efdc7 538 if ( style & wxLC_MASK_ALIGN )
edccf428 539 flag = flag & ~wxLC_MASK_ALIGN;
0b5efdc7 540 if ( style & wxLC_MASK_SORT )
edccf428 541 flag = flag & ~wxLC_MASK_SORT;
acb62b84 542 }
2bda0e17 543
6e2fef03
VZ
544 if ( add )
545 flag |= style;
0b5efdc7 546 else
6e2fef03 547 flag &= ~style;
2bda0e17 548
6e2fef03 549 SetWindowStyleFlag(flag);
2bda0e17
KB
550}
551
552// Set the whole window style
debe6624 553void wxListCtrl::SetWindowStyleFlag(long flag)
2bda0e17 554{
6e2fef03
VZ
555 if ( flag != m_windowStyle )
556 {
9a8d75f1 557 wxControl::SetWindowStyleFlag(flag);
6e2fef03
VZ
558
559 UpdateStyle();
2bda0e17 560
6e2fef03
VZ
561 Refresh();
562 }
2bda0e17
KB
563}
564
edccf428
VZ
565// ----------------------------------------------------------------------------
566// accessors
567// ----------------------------------------------------------------------------
568
39c7a53c
VZ
569/* static */ wxVisualAttributes
570wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
571{
572 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
573
574 // common controls have their own default font
575 attrs.font = wxGetCCDefaultFont();
576
577 return attrs;
578}
579
91b4c08d
VZ
580// Sets the foreground, i.e. text, colour
581bool wxListCtrl::SetForegroundColour(const wxColour& col)
582{
583 if ( !wxWindow::SetForegroundColour(col) )
598ddd96 584 return false;
91b4c08d
VZ
585
586 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
587
598ddd96 588 return true;
91b4c08d
VZ
589}
590
591// Sets the background colour
cc2b7472 592bool wxListCtrl::SetBackgroundColour(const wxColour& col)
2bda0e17 593{
cc2b7472 594 if ( !wxWindow::SetBackgroundColour(col) )
598ddd96 595 return false;
2bda0e17 596
91b4c08d
VZ
597 // we set the same colour for both the "empty" background and the items
598 // background
599 COLORREF color = wxColourToRGB(col);
600 ListView_SetBkColor(GetHwnd(), color);
601 ListView_SetTextBkColor(GetHwnd(), color);
cc2b7472 602
598ddd96 603 return true;
2bda0e17
KB
604}
605
606// Gets information about this column
debe6624 607bool wxListCtrl::GetColumn(int col, wxListItem& item) const
2bda0e17 608{
0b5efdc7 609 LV_COLUMN lvCol;
6f806543 610 wxZeroMemory(lvCol);
0b5efdc7 611
37094564
RD
612 lvCol.mask = LVCF_WIDTH;
613
0b5efdc7
VZ
614 if ( item.m_mask & wxLIST_MASK_TEXT )
615 {
616 lvCol.mask |= LVCF_TEXT;
837e5743 617 lvCol.pszText = new wxChar[513];
0b5efdc7
VZ
618 lvCol.cchTextMax = 512;
619 }
620
37094564
RD
621 if ( item.m_mask & wxLIST_MASK_FORMAT )
622 {
623 lvCol.mask |= LVCF_FMT;
624 }
625
626 if ( item.m_mask & wxLIST_MASK_IMAGE )
627 {
628 lvCol.mask |= LVCF_IMAGE;
629 }
630
0b190ffc 631 bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0;
0b5efdc7
VZ
632
633 // item.m_subItem = lvCol.iSubItem;
634 item.m_width = lvCol.cx;
635
636 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
637 {
638 item.m_text = lvCol.pszText;
639 delete[] lvCol.pszText;
640 }
641
642 if ( item.m_mask & wxLIST_MASK_FORMAT )
643 {
0b190ffc
RD
644 switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) {
645 case LVCFMT_LEFT:
646 item.m_format = wxLIST_FORMAT_LEFT;
647 break;
648 case LVCFMT_RIGHT:
649 item.m_format = wxLIST_FORMAT_RIGHT;
650 break;
651 case LVCFMT_CENTER:
652 item.m_format = wxLIST_FORMAT_CENTRE;
653 break;
654 default:
655 item.m_format = -1; // Unknown?
656 break;
657 }
2bda0e17
KB
658 }
659
5b59df83
VZ
660 // the column images were not supported in older versions but how to check
661 // for this? we can't use _WIN32_IE because we always define it to a very
662 // high value, so see if another symbol which is only defined starting from
663 // comctl32.dll 4.70 is available
664#ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
37094564
RD
665 if ( item.m_mask & wxLIST_MASK_IMAGE )
666 {
667 item.m_image = lvCol.iImage;
668 }
5b59df83 669#endif // LVCOLUMN::iImage exists
37094564 670
0b5efdc7 671 return success;
2bda0e17
KB
672}
673
674// Sets information about this column
fbfb8bcc 675bool wxListCtrl::SetColumn(int col, const wxListItem& item)
2bda0e17 676{
0b5efdc7 677 LV_COLUMN lvCol;
e64658ed 678 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
0b5efdc7 679
6f806543 680 return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
2bda0e17
KB
681}
682
683// Gets the column width
debe6624 684int wxListCtrl::GetColumnWidth(int col) const
2bda0e17 685{
edccf428 686 return ListView_GetColumnWidth(GetHwnd(), col);
2bda0e17
KB
687}
688
689// Sets the column width
debe6624 690bool wxListCtrl::SetColumnWidth(int col, int width)
2bda0e17 691{
0b5efdc7 692 if ( m_windowStyle & wxLC_LIST )
514ee250 693 col = 0;
2bda0e17 694
514ee250
VZ
695 if ( width == wxLIST_AUTOSIZE)
696 width = LVSCW_AUTOSIZE;
697 else if ( width == wxLIST_AUTOSIZE_USEHEADER)
698 width = LVSCW_AUTOSIZE_USEHEADER;
2bda0e17 699
514ee250 700 return ListView_SetColumnWidth(GetHwnd(), col, width) != 0;
2bda0e17
KB
701}
702
67d3fc49
VZ
703// ----------------------------------------------------------------------------
704// columns order
705// ----------------------------------------------------------------------------
706
80cc5fc7 707int wxListCtrl::GetColumnIndexFromOrder(int order) const
67d3fc49
VZ
708{
709 const int numCols = GetColumnCount();
80cc5fc7
VZ
710 wxCHECK_MSG( order >= 0 && order < numCols, -1,
711 _T("Column position out of bounds") );
67d3fc49
VZ
712
713 wxArrayInt indexArray(numCols);
67d3fc49
VZ
714 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) )
715 return -1;
716
80cc5fc7 717 return indexArray[order];
67d3fc49
VZ
718}
719
80cc5fc7 720int wxListCtrl::GetColumnOrder(int col) const
67d3fc49
VZ
721{
722 const int numCols = GetColumnCount();
80cc5fc7 723 wxASSERT_MSG( col >= 0 && col < numCols, _T("Column index out of bounds") );
67d3fc49
VZ
724
725 wxArrayInt indexArray(numCols);
67d3fc49
VZ
726 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) )
727 return -1;
728
80cc5fc7 729 for ( int pos = 0; pos < numCols; pos++ )
67d3fc49 730 {
80cc5fc7
VZ
731 if ( indexArray[pos] == col )
732 return pos;
67d3fc49
VZ
733 }
734
735 wxFAIL_MSG( _T("no column with with given order?") );
736
737 return -1;
738}
739
740// Gets the column order for all columns
741wxArrayInt wxListCtrl::GetColumnsOrder() const
742{
743 const int numCols = GetColumnCount();
744
745 wxArrayInt orders(numCols);
746 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &orders[0]) )
747 orders.clear();
748
749 return orders;
750}
751
752// Sets the column order for all columns
753bool wxListCtrl::SetColumnsOrder(const wxArrayInt& orders)
754{
755 const int numCols = GetColumnCount();
756
757 wxCHECK_MSG( orders.size() == (size_t)numCols, false,
758 _T("wrong number of elements in column orders array") );
759
760 return ListView_SetColumnOrderArray(GetHwnd(), numCols, &orders[0]) != 0;
761}
762
763
2bda0e17
KB
764// Gets the number of items that can fit vertically in the
765// visible area of the list control (list or report view)
766// or the total number of items in the list control (icon
767// or small icon view)
c42404a5 768int wxListCtrl::GetCountPerPage() const
2bda0e17 769{
edccf428 770 return ListView_GetCountPerPage(GetHwnd());
2bda0e17
KB
771}
772
773// Gets the edit control for editing labels.
c42404a5 774wxTextCtrl* wxListCtrl::GetEditControl() const
2bda0e17 775{
508b6523
VZ
776 // first check corresponds to the case when the label editing was started
777 // by user and hence m_textCtrl wasn't created by EditLabel() at all, while
778 // the second case corresponds to us being called from inside EditLabel()
779 // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this
780 // case EditLabel() did create the control but it didn't have an HWND to
781 // initialize it with yet
782 if ( !m_textCtrl || !m_textCtrl->GetHWND() )
783 {
784 HWND hwndEdit = ListView_GetEditControl(GetHwnd());
785 if ( hwndEdit )
786 {
5c33522f 787 wxListCtrl * const self = const_cast<wxListCtrl *>(this);
508b6523
VZ
788
789 if ( !m_textCtrl )
790 self->m_textCtrl = new wxTextCtrl;
791 self->InitEditControl((WXHWND)hwndEdit);
792 }
793 }
794
bbcdf8bc 795 return m_textCtrl;
2bda0e17
KB
796}
797
798// Gets information about the item
799bool wxListCtrl::GetItem(wxListItem& info) const
800{
0b5efdc7 801 LV_ITEM lvItem;
11c7d5b6 802 wxZeroMemory(lvItem);
2bda0e17 803
0b5efdc7 804 lvItem.iItem = info.m_itemId;
fc5a93cd 805 lvItem.iSubItem = info.m_col;
0b5efdc7
VZ
806
807 if ( info.m_mask & wxLIST_MASK_TEXT )
808 {
809 lvItem.mask |= LVIF_TEXT;
837e5743 810 lvItem.pszText = new wxChar[513];
0b5efdc7
VZ
811 lvItem.cchTextMax = 512;
812 }
813 else
814 {
815 lvItem.pszText = NULL;
816 }
817
818 if (info.m_mask & wxLIST_MASK_DATA)
edccf428 819 lvItem.mask |= LVIF_PARAM;
0b5efdc7 820
2189c644
JS
821 if (info.m_mask & wxLIST_MASK_IMAGE)
822 lvItem.mask |= LVIF_IMAGE;
823
0b5efdc7
VZ
824 if ( info.m_mask & wxLIST_MASK_STATE )
825 {
826 lvItem.mask |= LVIF_STATE;
7c392815 827 wxConvertToMSWFlags(0, info.m_stateMask, lvItem);
0b5efdc7
VZ
828 }
829
830 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
831 if ( !success )
832 {
833 wxLogError(_("Couldn't retrieve information about list control item %d."),
834 lvItem.iItem);
835 }
836 else
837 {
dccde0c0
VZ
838 // give NULL as hwnd as we already have everything we need
839 wxConvertFromMSWListItem(NULL, info, lvItem);
0b5efdc7
VZ
840 }
841
842 if (lvItem.pszText)
843 delete[] lvItem.pszText;
844
845 return success;
2bda0e17
KB
846}
847
848// Sets information about the item
849bool wxListCtrl::SetItem(wxListItem& info)
850{
4ce04fd8
VZ
851 const long id = info.GetId();
852 wxCHECK_MSG( id >= 0 && id < GetItemCount(), false,
853 _T("invalid item index in SetItem") );
854
0b5efdc7 855 LV_ITEM item;
2bda0e17 856 wxConvertToMSWListItem(this, info, item);
bdc72a22 857
7cf46fdb 858 // we never update the lParam if it contains our pointer
c767a5bb 859 // to the wxMSWListItemData structure
7cf46fdb
VZ
860 item.mask &= ~LVIF_PARAM;
861
862 // check if setting attributes or lParam
c767a5bb 863 if ( info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA) )
7cf46fdb
VZ
864 {
865 // get internal item data
c767a5bb 866 wxMSWListItemData *data = MSWGetItemData(id);
73d8c5fd 867
c767a5bb 868 if ( !data )
7cf46fdb 869 {
c767a5bb
VZ
870 // need to allocate the internal data object
871 data = new wxMSWListItemData;
872 m_internalData.push_back(data);
7cf46fdb
VZ
873 item.lParam = (LPARAM) data;
874 item.mask |= LVIF_PARAM;
dcc3f1a5 875 }
7cf46fdb
VZ
876
877
878 // user data
c767a5bb 879 if ( info.m_mask & wxLIST_MASK_DATA )
7cf46fdb
VZ
880 data->lParam = info.m_data;
881
882 // attributes
0f3888a5 883 if ( info.HasAttributes() )
7cf46fdb 884 {
0f3888a5
VZ
885 const wxListItemAttr& attrNew = *info.GetAttributes();
886
887 // don't overwrite the already set attributes if we have them
888 if ( data->attr )
889 data->attr->AssignFrom(attrNew);
7cf46fdb 890 else
0f3888a5 891 data->attr = new wxListItemAttr(attrNew);
dcc3f1a5
VZ
892 }
893 }
7cf46fdb
VZ
894
895
2d33aec9
VZ
896 // we could be changing only the attribute in which case we don't need to
897 // call ListView_SetItem() at all
898 if ( item.mask )
bdc72a22 899 {
2d33aec9
VZ
900 if ( !ListView_SetItem(GetHwnd(), &item) )
901 {
902 wxLogDebug(_T("ListView_SetItem() failed"));
2702ed5a 903
598ddd96 904 return false;
2d33aec9 905 }
233d0295 906 }
2702ed5a 907
233d0295
VZ
908 // we need to update the item immediately to show the new image
909 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
2702ed5a 910
233d0295
VZ
911 // check whether it has any custom attributes
912 if ( info.HasAttributes() )
913 {
598ddd96 914 m_hasAnyAttr = true;
233d0295
VZ
915
916 // if the colour has changed, we must redraw the item
598ddd96 917 updateNow = true;
bdc72a22 918 }
bdc72a22 919
233d0295 920 if ( updateNow )
7cc98b3e 921 {
233d0295 922 // we need this to make the change visible right now
2d33aec9 923 RefreshItem(item.iItem);
7cc98b3e
VZ
924 }
925
598ddd96 926 return true;
2bda0e17
KB
927}
928
debe6624 929long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
2bda0e17 930{
0b5efdc7
VZ
931 wxListItem info;
932 info.m_text = label;
933 info.m_mask = wxLIST_MASK_TEXT;
934 info.m_itemId = index;
935 info.m_col = col;
936 if ( imageId > -1 )
937 {
938 info.m_image = imageId;
939 info.m_mask |= wxLIST_MASK_IMAGE;
940 }
941 return SetItem(info);
2bda0e17
KB
942}
943
944
945// Gets the item state
debe6624 946int wxListCtrl::GetItemState(long item, long stateMask) const
2bda0e17 947{
0b5efdc7 948 wxListItem info;
2bda0e17 949
edccf428 950 info.m_mask = wxLIST_MASK_STATE;
0b5efdc7
VZ
951 info.m_stateMask = stateMask;
952 info.m_itemId = item;
2bda0e17 953
0b5efdc7
VZ
954 if (!GetItem(info))
955 return 0;
2bda0e17 956
0b5efdc7 957 return info.m_state;
2bda0e17
KB
958}
959
960// Sets the item state
debe6624 961bool wxListCtrl::SetItemState(long item, long state, long stateMask)
2bda0e17 962{
8dff2b5c
VZ
963 // NB: don't use SetItem() here as it doesn't work with the virtual list
964 // controls
965 LV_ITEM lvItem;
966 wxZeroMemory(lvItem);
2bda0e17 967
8dff2b5c 968 wxConvertToMSWFlags(state, stateMask, lvItem);
2bda0e17 969
b720b86b
VZ
970 const bool changingFocus = (stateMask & wxLIST_STATE_FOCUSED) &&
971 (state & wxLIST_STATE_FOCUSED);
972
2d33aec9 973 // for the virtual list controls we need to refresh the previously focused
ad9f477d
VZ
974 // item manually when changing focus without changing selection
975 // programmatically because otherwise it keeps its focus rectangle until
976 // next repaint (yet another comctl32 bug)
2d33aec9 977 long focusOld;
b720b86b 978 if ( IsVirtual() && changingFocus )
2d33aec9
VZ
979 {
980 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
981 }
982 else
983 {
984 focusOld = -1;
985 }
986
8dff2b5c
VZ
987 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
988 (WPARAM)item, (LPARAM)&lvItem) )
989 {
990 wxLogLastError(_T("ListView_SetItemState"));
991
598ddd96 992 return false;
8dff2b5c
VZ
993 }
994
2d33aec9
VZ
995 if ( focusOld != -1 )
996 {
ad9f477d
VZ
997 // no need to refresh the item if it was previously selected, it would
998 // only result in annoying flicker
999 if ( !(GetItemState(focusOld,
1000 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
1001 {
1002 RefreshItem(focusOld);
1003 }
2d33aec9
VZ
1004 }
1005
b720b86b
VZ
1006 // we expect the selection anchor, i.e. the item from which multiple
1007 // selection (such as performed with e.g. Shift-arrows) starts, to be the
1008 // same as the currently focused item but the native control doesn't update
1009 // it when we change focus and leaves at the last item it set itself focus
1010 // to, so do it explicitly
1011 if ( changingFocus && !HasFlag(wxLC_SINGLE_SEL) )
1012 {
1013 ListView_SetSelectionMark(GetHwnd(), item);
1014 }
1015
598ddd96 1016 return true;
2bda0e17
KB
1017}
1018
1019// Sets the item image
33ac7e6f 1020bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
06db67bc
RD
1021{
1022 return SetItemColumnImage(item, 0, image);
1023}
1024
1025// Sets the item image
1026bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
2bda0e17 1027{
0b5efdc7 1028 wxListItem info;
2bda0e17 1029
edccf428 1030 info.m_mask = wxLIST_MASK_IMAGE;
0b5efdc7
VZ
1031 info.m_image = image;
1032 info.m_itemId = item;
06db67bc 1033 info.m_col = column;
2bda0e17 1034
0b5efdc7 1035 return SetItem(info);
2bda0e17
KB
1036}
1037
1038// Gets the item text
debe6624 1039wxString wxListCtrl::GetItemText(long item) const
2bda0e17 1040{
0b5efdc7 1041 wxListItem info;
2bda0e17 1042
edccf428 1043 info.m_mask = wxLIST_MASK_TEXT;
0b5efdc7 1044 info.m_itemId = item;
2bda0e17 1045
0b5efdc7 1046 if (!GetItem(info))
2b5f62a0 1047 return wxEmptyString;
0b5efdc7 1048 return info.m_text;
2bda0e17
KB
1049}
1050
1051// Sets the item text
debe6624 1052void wxListCtrl::SetItemText(long item, const wxString& str)
2bda0e17 1053{
0b5efdc7 1054 wxListItem info;
2bda0e17 1055
edccf428 1056 info.m_mask = wxLIST_MASK_TEXT;
0b5efdc7
VZ
1057 info.m_itemId = item;
1058 info.m_text = str;
2bda0e17 1059
0b5efdc7 1060 SetItem(info);
2bda0e17
KB
1061}
1062
c767a5bb
VZ
1063// Gets the internal item data
1064wxMSWListItemData *wxListCtrl::MSWGetItemData(long itemId) const
1065{
1066 LV_ITEM it;
1067 it.mask = LVIF_PARAM;
1068 it.iItem = itemId;
1069
1070 if ( !ListView_GetItem(GetHwnd(), &it) )
1071 return NULL;
1072
1073 return (wxMSWListItemData *) it.lParam;
1074}
1075
2bda0e17 1076// Gets the item data
81ce36aa 1077wxUIntPtr wxListCtrl::GetItemData(long item) const
2bda0e17 1078{
0b5efdc7 1079 wxListItem info;
2bda0e17 1080
edccf428 1081 info.m_mask = wxLIST_MASK_DATA;
0b5efdc7 1082 info.m_itemId = item;
2bda0e17 1083
0b5efdc7
VZ
1084 if (!GetItem(info))
1085 return 0;
1086 return info.m_data;
2bda0e17
KB
1087}
1088
1089// Sets the item data
9fcd0bf7 1090bool wxListCtrl::SetItemPtrData(long item, wxUIntPtr data)
2bda0e17 1091{
0b5efdc7 1092 wxListItem info;
2bda0e17 1093
edccf428 1094 info.m_mask = wxLIST_MASK_DATA;
0b5efdc7
VZ
1095 info.m_itemId = item;
1096 info.m_data = data;
2bda0e17 1097
0b5efdc7 1098 return SetItem(info);
2bda0e17
KB
1099}
1100
11ebea16
VZ
1101wxRect wxListCtrl::GetViewRect() const
1102{
929b7901 1103 wxRect rect;
11ebea16 1104
929b7901
VZ
1105 // ListView_GetViewRect() can only be used in icon and small icon views
1106 // (this is documented in MSDN and, indeed, it returns bogus results in
1107 // report view, at least with comctl32.dll v6 under Windows 2003)
1108 if ( HasFlag(wxLC_ICON | wxLC_SMALL_ICON) )
11ebea16 1109 {
929b7901
VZ
1110 RECT rc;
1111 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
1112 {
1113 wxLogDebug(_T("ListView_GetViewRect() failed."));
1114
1115 wxZeroMemory(rc);
1116 }
11ebea16 1117
929b7901 1118 wxCopyRECTToRect(rc, rect);
11ebea16 1119 }
929b7901
VZ
1120 else if ( HasFlag(wxLC_REPORT) )
1121 {
1122 const long count = GetItemCount();
1123 if ( count )
1124 {
1125 GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count - 1), rect);
11ebea16 1126
929b7901
VZ
1127 // extend the rectangle to start at the top (we include the column
1128 // headers, if any, for compatibility with the generic version)
1129 rect.height += rect.y;
1130 rect.y = 0;
1131 }
1132 }
1133 else
1134 {
1135 wxFAIL_MSG( _T("not implemented in this mode") );
1136 }
11ebea16
VZ
1137
1138 return rect;
1139}
1140
2bda0e17 1141// Gets the item rectangle
16e93305 1142bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
164a7972
VZ
1143{
1144 return GetSubItemRect( item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code) ;
1145}
1146
164a7972 1147bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) const
2bda0e17 1148{
e974c5d2
VZ
1149 // ListView_GetSubItemRect() doesn't do subItem error checking and returns
1150 // true even for the out of range values of it (even if the results are
1151 // completely bogus in this case), so we check item validity ourselves
1152 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM ||
1153 (subItem >= 0 && subItem < GetColumnCount()),
1154 false, _T("invalid sub item index") );
2bda0e17 1155
8d1a4101
FM
1156 // use wxCHECK_MSG against "item" too, for coherency with the generic implementation:
1157 wxCHECK_MSG( item >= 0 && item < GetItemCount(), false,
1158 _T("invalid item in GetSubItemRect") );
1159
2d33aec9 1160 int codeWin;
0b5efdc7 1161 if ( code == wxLIST_RECT_BOUNDS )
2d33aec9 1162 codeWin = LVIR_BOUNDS;
0b5efdc7 1163 else if ( code == wxLIST_RECT_ICON )
2d33aec9 1164 codeWin = LVIR_ICON;
0b5efdc7 1165 else if ( code == wxLIST_RECT_LABEL )
2d33aec9
VZ
1166 codeWin = LVIR_LABEL;
1167 else
1168 {
164a7972 1169 wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") );
2d33aec9
VZ
1170 codeWin = LVIR_BOUNDS;
1171 }
2bda0e17 1172
e974c5d2 1173 RECT rectWin;
89cafab0 1174 if ( !wxGetListCtrlSubItemRect
e974c5d2
VZ
1175 (
1176 GetHwnd(),
1177 item,
1178 subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM ? 0 : subItem,
1179 codeWin,
89cafab0 1180 rectWin
e974c5d2 1181 ) )
164a7972 1182 {
e974c5d2 1183 return false;
164a7972 1184 }
2bda0e17 1185
e974c5d2 1186 wxCopyRECTToRect(rectWin, rect);
2d33aec9 1187
67561862
VZ
1188 // there is no way to retrieve the first sub item bounding rectangle using
1189 // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to
1190 // truncate it at first column ourselves
1191 if ( subItem == 0 )
1192 rect.width = GetColumnWidth(0);
e974c5d2
VZ
1193
1194 return true;
2bda0e17
KB
1195}
1196
164a7972
VZ
1197
1198
1199
2bda0e17 1200// Gets the item position
debe6624 1201bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
2bda0e17 1202{
0b5efdc7 1203 POINT pt;
2bda0e17 1204
edccf428 1205 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
2bda0e17 1206
0b5efdc7
VZ
1207 pos.x = pt.x; pos.y = pt.y;
1208 return success;
2bda0e17
KB
1209}
1210
1211// Sets the item position.
debe6624 1212bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
2bda0e17 1213{
edccf428 1214 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
2bda0e17
KB
1215}
1216
1217// Gets the number of items in the list control
c42404a5 1218int wxListCtrl::GetItemCount() const
2bda0e17 1219{
68c124a1 1220 return m_count;
2bda0e17
KB
1221}
1222
5db8d758
VZ
1223wxSize wxListCtrl::GetItemSpacing() const
1224{
66b9ec3d 1225 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
5db8d758
VZ
1226
1227 return wxSize(LOWORD(spacing), HIWORD(spacing));
1228}
1229
40ff126a
WS
1230#if WXWIN_COMPATIBILITY_2_6
1231
2bda0e17
KB
1232int wxListCtrl::GetItemSpacing(bool isSmall) const
1233{
edccf428 1234 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
2bda0e17
KB
1235}
1236
40ff126a
WS
1237#endif // WXWIN_COMPATIBILITY_2_6
1238
5b98eb2f
RL
1239void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1240{
1241 wxListItem info;
1242 info.m_itemId = item;
1243 info.SetTextColour( col );
1244 SetItem( info );
1245}
1246
1247wxColour wxListCtrl::GetItemTextColour( long item ) const
1248{
97c611f8 1249 wxColour col;
c767a5bb 1250 wxMSWListItemData *data = MSWGetItemData(item);
97c611f8
VZ
1251 if ( data && data->attr )
1252 col = data->attr->GetTextColour();
1253
1254 return col;
5b98eb2f
RL
1255}
1256
1257void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1258{
1259 wxListItem info;
1260 info.m_itemId = item;
1261 info.SetBackgroundColour( col );
1262 SetItem( info );
1263}
1264
1265wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1266{
97c611f8 1267 wxColour col;
c767a5bb 1268 wxMSWListItemData *data = MSWGetItemData(item);
97c611f8
VZ
1269 if ( data && data->attr )
1270 col = data->attr->GetBackgroundColour();
1271
1272 return col;
5b98eb2f
RL
1273}
1274
35c2acd4
MW
1275void wxListCtrl::SetItemFont( long item, const wxFont &f )
1276{
1277 wxListItem info;
1278 info.m_itemId = item;
1279 info.SetFont( f );
1280 SetItem( info );
1281}
1282
1283wxFont wxListCtrl::GetItemFont( long item ) const
1284{
1285 wxFont f;
c767a5bb 1286 wxMSWListItemData *data = MSWGetItemData(item);
35c2acd4
MW
1287 if ( data && data->attr )
1288 f = data->attr->GetFont();
1289
1290 return f;
1291}
1292
2bda0e17 1293// Gets the number of selected items in the list control
c42404a5 1294int wxListCtrl::GetSelectedItemCount() const
2bda0e17 1295{
edccf428 1296 return ListView_GetSelectedCount(GetHwnd());
2bda0e17
KB
1297}
1298
1299// Gets the text colour of the listview
c42404a5 1300wxColour wxListCtrl::GetTextColour() const
2bda0e17 1301{
6e21a3db
KH
1302 COLORREF ref = ListView_GetTextColor(GetHwnd());
1303 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1304 return col;
2bda0e17
KB
1305}
1306
1307// Sets the text colour of the listview
1308void wxListCtrl::SetTextColour(const wxColour& col)
1309{
910484a6 1310 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
2bda0e17
KB
1311}
1312
1313// Gets the index of the topmost visible item when in
1314// list or report view
c42404a5 1315long wxListCtrl::GetTopItem() const
2bda0e17 1316{
edccf428 1317 return (long) ListView_GetTopIndex(GetHwnd());
2bda0e17
KB
1318}
1319
1320// Searches for an item, starting from 'item'.
1321// 'geometry' is one of
1322// wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1323// 'state' is a state bit flag, one or more of
1324// wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1325// item can be -1 to find the first item that matches the
1326// specified flags.
1327// Returns the item or -1 if unsuccessful.
debe6624 1328long wxListCtrl::GetNextItem(long item, int geom, int state) const
2bda0e17 1329{
0b5efdc7 1330 long flags = 0;
2bda0e17 1331
0b5efdc7
VZ
1332 if ( geom == wxLIST_NEXT_ABOVE )
1333 flags |= LVNI_ABOVE;
1334 if ( geom == wxLIST_NEXT_ALL )
1335 flags |= LVNI_ALL;
1336 if ( geom == wxLIST_NEXT_BELOW )
1337 flags |= LVNI_BELOW;
1338 if ( geom == wxLIST_NEXT_LEFT )
1339 flags |= LVNI_TOLEFT;
1340 if ( geom == wxLIST_NEXT_RIGHT )
1341 flags |= LVNI_TORIGHT;
2bda0e17 1342
0b5efdc7
VZ
1343 if ( state & wxLIST_STATE_CUT )
1344 flags |= LVNI_CUT;
1345 if ( state & wxLIST_STATE_DROPHILITED )
1346 flags |= LVNI_DROPHILITED;
1347 if ( state & wxLIST_STATE_FOCUSED )
1348 flags |= LVNI_FOCUSED;
1349 if ( state & wxLIST_STATE_SELECTED )
1350 flags |= LVNI_SELECTED;
2bda0e17 1351
edccf428 1352 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
2bda0e17
KB
1353}
1354
1355
debe6624 1356wxImageList *wxListCtrl::GetImageList(int which) const
2bda0e17 1357{
0b5efdc7 1358 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 1359 {
0b5efdc7
VZ
1360 return m_imageListNormal;
1361 }
1362 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 1363 {
0b5efdc7
VZ
1364 return m_imageListSmall;
1365 }
1366 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 1367 {
0b5efdc7
VZ
1368 return m_imageListState;
1369 }
1370 return NULL;
2bda0e17
KB
1371}
1372
debe6624 1373void wxListCtrl::SetImageList(wxImageList *imageList, int which)
2bda0e17 1374{
0b5efdc7
VZ
1375 int flags = 0;
1376 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 1377 {
0b5efdc7 1378 flags = LVSIL_NORMAL;
2e12c11a 1379 if (m_ownsImageListNormal) delete m_imageListNormal;
0b5efdc7 1380 m_imageListNormal = imageList;
598ddd96 1381 m_ownsImageListNormal = false;
0b5efdc7
VZ
1382 }
1383 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 1384 {
0b5efdc7 1385 flags = LVSIL_SMALL;
2e12c11a 1386 if (m_ownsImageListSmall) delete m_imageListSmall;
0b5efdc7 1387 m_imageListSmall = imageList;
598ddd96 1388 m_ownsImageListSmall = false;
0b5efdc7
VZ
1389 }
1390 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 1391 {
0b5efdc7 1392 flags = LVSIL_STATE;
2e12c11a 1393 if (m_ownsImageListState) delete m_imageListState;
0b5efdc7 1394 m_imageListState = imageList;
598ddd96 1395 m_ownsImageListState = false;
0b5efdc7 1396 }
8ecd5de2 1397 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
2bda0e17
KB
1398}
1399
2e12c11a
VS
1400void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1401{
1402 SetImageList(imageList, which);
1403 if ( which == wxIMAGE_LIST_NORMAL )
598ddd96 1404 m_ownsImageListNormal = true;
2e12c11a 1405 else if ( which == wxIMAGE_LIST_SMALL )
598ddd96 1406 m_ownsImageListSmall = true;
2e12c11a 1407 else if ( which == wxIMAGE_LIST_STATE )
598ddd96 1408 m_ownsImageListState = true;
2e12c11a
VS
1409}
1410
edccf428 1411// ----------------------------------------------------------------------------
2bda0e17 1412// Operations
edccf428 1413// ----------------------------------------------------------------------------
2bda0e17
KB
1414
1415// Arranges the items
debe6624 1416bool wxListCtrl::Arrange(int flag)
2bda0e17 1417{
0b5efdc7
VZ
1418 UINT code = 0;
1419 if ( flag == wxLIST_ALIGN_LEFT )
1420 code = LVA_ALIGNLEFT;
1421 else if ( flag == wxLIST_ALIGN_TOP )
1422 code = LVA_ALIGNTOP;
1423 else if ( flag == wxLIST_ALIGN_DEFAULT )
1424 code = LVA_DEFAULT;
1425 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1426 code = LVA_SNAPTOGRID;
2bda0e17 1427
edccf428 1428 return (ListView_Arrange(GetHwnd(), code) != 0);
2bda0e17
KB
1429}
1430
1431// Deletes an item
debe6624 1432bool wxListCtrl::DeleteItem(long item)
2bda0e17 1433{
2e9f62da
VZ
1434 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1435 {
1436 wxLogLastError(_T("ListView_DeleteItem"));
598ddd96 1437 return false;
2e9f62da
VZ
1438 }
1439
7a462631 1440 m_count--;
68c124a1
RD
1441 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1442 wxT("m_count should match ListView_GetItemCount"));
1443
2e9f62da
VZ
1444 // the virtual list control doesn't refresh itself correctly, help it
1445 if ( IsVirtual() )
1446 {
1447 // we need to refresh all the lines below the one which was deleted
1448 wxRect rectItem;
1449 if ( item > 0 && GetItemCount() )
1450 {
1451 GetItemRect(item - 1, rectItem);
1452 }
1453 else
1454 {
1455 rectItem.y =
1456 rectItem.height = 0;
1457 }
1458
1459 wxRect rectWin = GetRect();
1460 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1461 rectWin.y = rectItem.GetBottom();
1462
1463 RefreshRect(rectWin);
1464 }
1465
598ddd96 1466 return true;
2bda0e17
KB
1467}
1468
1469// Deletes all items
0b5efdc7 1470bool wxListCtrl::DeleteAllItems()
2bda0e17 1471{
2e9f62da 1472 return ListView_DeleteAllItems(GetHwnd()) != 0;
2bda0e17
KB
1473}
1474
1475// Deletes all items
0b5efdc7 1476bool wxListCtrl::DeleteAllColumns()
2bda0e17 1477{
edccf428 1478 while ( m_colCount > 0 )
2bda0e17 1479 {
edccf428
VZ
1480 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1481 {
f6bcfd97 1482 wxLogLastError(wxT("ListView_DeleteColumn"));
edccf428 1483
598ddd96 1484 return false;
edccf428
VZ
1485 }
1486
1487 m_colCount--;
2bda0e17 1488 }
edccf428 1489
223d09f6 1490 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
edccf428 1491
598ddd96 1492 return true;
2bda0e17
KB
1493}
1494
1495// Deletes a column
debe6624 1496bool wxListCtrl::DeleteColumn(int col)
2bda0e17 1497{
edccf428 1498 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
2bda0e17
KB
1499
1500 if ( success && (m_colCount > 0) )
1501 m_colCount --;
1502 return success;
1503}
1504
1505// Clears items, and columns if there are any.
0b5efdc7 1506void wxListCtrl::ClearAll()
2bda0e17
KB
1507{
1508 DeleteAllItems();
1509 if ( m_colCount > 0 )
1510 DeleteAllColumns();
1511}
1512
508b6523
VZ
1513void wxListCtrl::InitEditControl(WXHWND hWnd)
1514{
1515 m_textCtrl->SetHWND(hWnd);
1516 m_textCtrl->SubclassWin(hWnd);
1517 m_textCtrl->SetParent(this);
1518
1519 // we must disallow TABbing away from the control while the edit contol is
1520 // shown because this leaves it in some strange state (just try removing
1521 // this line and then pressing TAB while editing an item in listctrl
1522 // inside a panel)
1523 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1524}
1525
bbcdf8bc
JS
1526wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1527{
508b6523
VZ
1528 wxCHECK_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)), NULL,
1529 "control used for label editing must be a wxTextCtrl" );
bbcdf8bc 1530
6aaef140 1531 // ListView_EditLabel requires that the list has focus.
aa50e893 1532 SetFocus();
2b5f62a0 1533
508b6523
VZ
1534 // create m_textCtrl here before calling ListView_EditLabel() because it
1535 // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and
1536 // the user handler for it can call GetEditControl() resulting in an on
1537 // demand creation of a stock wxTextCtrl instead of the control of a
1538 // (possibly) custom wxClassInfo
1539 DeleteEditControl();
1540 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1541
6aaef140 1542 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
2b5f62a0
VZ
1543 if ( !hWnd )
1544 {
1545 // failed to start editing
508b6523
VZ
1546 delete m_textCtrl;
1547 m_textCtrl = NULL;
1548
2b5f62a0
VZ
1549 return NULL;
1550 }
bbcdf8bc 1551
508b6523
VZ
1552 // if GetEditControl() hasn't been called, we need to initialize the edit
1553 // control ourselves
1554 if ( !m_textCtrl->GetHWND() )
1555 InitEditControl(hWnd);
2b5f62a0 1556
bbcdf8bc
JS
1557 return m_textCtrl;
1558}
1559
1560// End label editing, optionally cancelling the edit
4496eadc 1561bool wxListCtrl::EndEditLabel(bool cancel)
2bda0e17 1562{
4496eadc
JS
1563 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1564 HWND hwnd = ListView_GetEditControl(GetHwnd());
55f42db2
VZ
1565 if ( !hwnd )
1566 return false;
1567
1568 if ( cancel )
1569 ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing
1570
1571 // we shouldn't destroy the control ourselves according to MSDN, which
1572 // proposes WM_CANCELMODE to do this, but it doesn't seem to work
1573 //
1574 // posting WM_CLOSE to it does seem to work without any side effects
1575 ::PostMessage(hwnd, WM_CLOSE, 0, 0);
1576
1577 return true;
2bda0e17
KB
1578}
1579
1580// Ensures this item is visible
debe6624 1581bool wxListCtrl::EnsureVisible(long item)
2bda0e17 1582{
598ddd96 1583 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE;
2bda0e17
KB
1584}
1585
1586// Find an item whose label matches this string, starting from the item after 'start'
1587// or the beginning if 'start' is -1.
debe6624 1588long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
2bda0e17 1589{
0b5efdc7 1590 LV_FINDINFO findInfo;
2bda0e17 1591
0b5efdc7
VZ
1592 findInfo.flags = LVFI_STRING;
1593 if ( partial )
7edc258e 1594 findInfo.flags |= LVFI_PARTIAL;
e0a050e3 1595 findInfo.psz = str.wx_str();
2bda0e17 1596
3ca6a5f0
BP
1597 // ListView_FindItem() excludes the first item from search and to look
1598 // through all the items you need to start from -1 which is unnatural and
8036af01
JS
1599 // inconsistent with the generic version - so we adjust the index
1600 if (start != -1)
1601 start --;
c767a5bb 1602 return ListView_FindItem(GetHwnd(), start, &findInfo);
2bda0e17
KB
1603}
1604
c767a5bb
VZ
1605// Find an item whose data matches this data, starting from the item after
1606// 'start' or the beginning if 'start' is -1.
81ce36aa 1607long wxListCtrl::FindItem(long start, wxUIntPtr data)
2bda0e17 1608{
c767a5bb
VZ
1609 // we can't use ListView_FindItem() directly as we don't store the data
1610 // pointer itself in the control but rather our own internal data, so first
1611 // we need to find the right value to search for (and there can be several
1612 // of them)
1613 int idx = wxNOT_FOUND;
1614 const unsigned count = m_internalData.size();
1615 for ( unsigned n = 0; n < count; n++ )
72db8894 1616 {
c767a5bb
VZ
1617 if ( m_internalData[n]->lParam == (LPARAM)data )
1618 {
1619 LV_FINDINFO findInfo;
1620 findInfo.flags = LVFI_PARAM;
1621 findInfo.lParam = (LPARAM)wxPtrToUInt(m_internalData[n]);
1622
1623 int rc = ListView_FindItem(GetHwnd(), start, &findInfo);
1624 if ( rc != -1 )
1625 {
1626 if ( idx == wxNOT_FOUND || rc < idx )
1627 {
1628 idx = rc;
1629 if ( idx == start + 1 )
1630 {
1631 // we can stop here, we don't risk finding a closer
1632 // match
1633 break;
1634 }
1635 }
1636 //else: this item is after the previously found one
1637 }
1638 }
dcc3f1a5 1639 }
2bda0e17 1640
c767a5bb 1641 return idx;
2bda0e17
KB
1642}
1643
1644// Find an item nearest this position in the specified direction, starting from
1645// the item after 'start' or the beginning if 'start' is -1.
debe6624 1646long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
2bda0e17 1647{
0b5efdc7 1648 LV_FINDINFO findInfo;
2bda0e17 1649
0b5efdc7
VZ
1650 findInfo.flags = LVFI_NEARESTXY;
1651 findInfo.pt.x = pt.x;
1652 findInfo.pt.y = pt.y;
acb62b84 1653 findInfo.vkDirection = VK_RIGHT;
2bda0e17 1654
0b5efdc7
VZ
1655 if ( direction == wxLIST_FIND_UP )
1656 findInfo.vkDirection = VK_UP;
1657 else if ( direction == wxLIST_FIND_DOWN )
1658 findInfo.vkDirection = VK_DOWN;
1659 else if ( direction == wxLIST_FIND_LEFT )
1660 findInfo.vkDirection = VK_LEFT;
1661 else if ( direction == wxLIST_FIND_RIGHT )
1662 findInfo.vkDirection = VK_RIGHT;
1663
c767a5bb 1664 return ListView_FindItem(GetHwnd(), start, &findInfo);
2bda0e17
KB
1665}
1666
1667// Determines which item (if any) is at the specified point,
1668// giving details in 'flags' (see wxLIST_HITTEST_... flags above)
be0e5d69
VZ
1669long
1670wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
2bda0e17
KB
1671{
1672 LV_HITTESTINFO hitTestInfo;
0b5efdc7
VZ
1673 hitTestInfo.pt.x = (int) point.x;
1674 hitTestInfo.pt.y = (int) point.y;
2bda0e17 1675
164a7972
VZ
1676 long item;
1677#ifdef LVM_SUBITEMHITTEST
1678 if ( ptrSubItem && wxApp::GetComCtl32Version() >= 470 )
1679 {
1680 item = ListView_SubItemHitTest(GetHwnd(), &hitTestInfo);
1681 *ptrSubItem = hitTestInfo.iSubItem;
1682 }
1683 else
1684#endif // LVM_SUBITEMHITTEST
1685 {
1686 item = ListView_HitTest(GetHwnd(), &hitTestInfo);
1687 }
2bda0e17 1688
0b5efdc7 1689 flags = 0;
698b34fa 1690
0b5efdc7
VZ
1691 if ( hitTestInfo.flags & LVHT_ABOVE )
1692 flags |= wxLIST_HITTEST_ABOVE;
1693 if ( hitTestInfo.flags & LVHT_BELOW )
1694 flags |= wxLIST_HITTEST_BELOW;
0b5efdc7
VZ
1695 if ( hitTestInfo.flags & LVHT_TOLEFT )
1696 flags |= wxLIST_HITTEST_TOLEFT;
1697 if ( hitTestInfo.flags & LVHT_TORIGHT )
1698 flags |= wxLIST_HITTEST_TORIGHT;
1699
698b34fa
VZ
1700 if ( hitTestInfo.flags & LVHT_NOWHERE )
1701 flags |= wxLIST_HITTEST_NOWHERE;
1702
1703 // note a bug or at least a very strange feature of comtl32.dll (tested
1704 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1705 // the right of the item label, ListView_HitTest() returns a combination of
1706 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1707 // the bits which don't make sense
1708 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1709 {
1710 flags |= wxLIST_HITTEST_ONITEMLABEL;
1711
1712 // do not translate LVHT_ONITEMICON here, as per above
1713 }
1714 else
1715 {
1716 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1717 flags |= wxLIST_HITTEST_ONITEMICON;
1718 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1719 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1720 }
1721
164a7972 1722 return item;
2bda0e17
KB
1723}
1724
164a7972 1725
2bda0e17
KB
1726// Inserts an item, returning the index of the new item if successful,
1727// -1 otherwise.
fbfb8bcc 1728long wxListCtrl::InsertItem(const wxListItem& info)
2bda0e17 1729{
98ec9dbe
VZ
1730 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1731
0b5efdc7
VZ
1732 LV_ITEM item;
1733 wxConvertToMSWListItem(this, info, item);
7cf46fdb 1734 item.mask &= ~LVIF_PARAM;
2bda0e17 1735
c767a5bb
VZ
1736 // check whether we need to allocate our internal data
1737 bool needInternalData = (info.m_mask & wxLIST_MASK_DATA) ||
1738 info.HasAttributes();
1739 if ( needInternalData )
bdc72a22 1740 {
7cf46fdb 1741 item.mask |= LVIF_PARAM;
2702ed5a 1742
c767a5bb
VZ
1743 wxMSWListItemData * const data = new wxMSWListItemData;
1744 m_internalData.push_back(data);
1745 item.lParam = (LPARAM)data;
2702ed5a 1746
c767a5bb 1747 if ( info.m_mask & wxLIST_MASK_DATA )
7cf46fdb 1748 data->lParam = info.m_data;
2702ed5a 1749
b653e655
VZ
1750 // check whether it has any custom attributes
1751 if ( info.HasAttributes() )
1752 {
7cf46fdb
VZ
1753 // take copy of attributes
1754 data->attr = new wxListItemAttr(*info.GetAttributes());
5205e26b
VZ
1755
1756 // and remember that we have some now...
598ddd96 1757 m_hasAnyAttr = true;
b653e655 1758 }
dcc3f1a5 1759 }
7cf46fdb 1760
68c124a1 1761 long rv = ListView_InsertItem(GetHwnd(), & item);
b653e655
VZ
1762
1763 m_count++;
68c124a1
RD
1764 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1765 wxT("m_count should match ListView_GetItemCount"));
bdc72a22 1766
68c124a1 1767 return rv;
2bda0e17
KB
1768}
1769
debe6624 1770long wxListCtrl::InsertItem(long index, const wxString& label)
2bda0e17 1771{
0b5efdc7
VZ
1772 wxListItem info;
1773 info.m_text = label;
1774 info.m_mask = wxLIST_MASK_TEXT;
1775 info.m_itemId = index;
1776 return InsertItem(info);
2bda0e17
KB
1777}
1778
1779// Inserts an image item
debe6624 1780long wxListCtrl::InsertItem(long index, int imageIndex)
2bda0e17 1781{
0b5efdc7
VZ
1782 wxListItem info;
1783 info.m_image = imageIndex;
1784 info.m_mask = wxLIST_MASK_IMAGE;
1785 info.m_itemId = index;
1786 return InsertItem(info);
2bda0e17
KB
1787}
1788
1789// Inserts an image/string item
debe6624 1790long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
2bda0e17 1791{
0b5efdc7
VZ
1792 wxListItem info;
1793 info.m_image = imageIndex;
1794 info.m_text = label;
1795 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1796 info.m_itemId = index;
1797 return InsertItem(info);
2bda0e17
KB
1798}
1799
1800// For list view mode (only), inserts a column.
fbfb8bcc 1801long wxListCtrl::InsertColumn(long col, const wxListItem& item)
2bda0e17 1802{
0b5efdc7 1803 LV_COLUMN lvCol;
e64658ed 1804 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
0b5efdc7 1805
6f806543 1806 if ( !(lvCol.mask & LVCF_WIDTH) )
e373f51b
VZ
1807 {
1808 // always give some width to the new column: this one is compatible
6f806543
VZ
1809 // with the generic version
1810 lvCol.mask |= LVCF_WIDTH;
e373f51b 1811 lvCol.cx = 80;
0b5efdc7 1812 }
e373f51b 1813
8b3a4016
VZ
1814 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1815 if ( n != -1 )
e373f51b
VZ
1816 {
1817 m_colCount++;
1818 }
8b3a4016 1819 else // failed to insert?
e373f51b 1820 {
223d09f6 1821 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
e373f51b
VZ
1822 lvCol.pszText);
1823 }
1824
8b3a4016 1825 return n;
2bda0e17
KB
1826}
1827
bdc72a22
VZ
1828long wxListCtrl::InsertColumn(long col,
1829 const wxString& heading,
1830 int format,
1831 int width)
2bda0e17 1832{
0b5efdc7
VZ
1833 wxListItem item;
1834 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1835 item.m_text = heading;
1836 if ( width > -1 )
1837 {
1838 item.m_mask |= wxLIST_MASK_WIDTH;
1839 item.m_width = width;
1840 }
1841 item.m_format = format;
2bda0e17 1842
0b5efdc7 1843 return InsertColumn(col, item);
2bda0e17
KB
1844}
1845
2b5f62a0
VZ
1846// scroll the control by the given number of pixels (exception: in list view,
1847// dx is interpreted as number of columns)
debe6624 1848bool wxListCtrl::ScrollList(int dx, int dy)
2bda0e17 1849{
2b5f62a0
VZ
1850 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1851 {
1852 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1853
598ddd96 1854 return false;
2b5f62a0
VZ
1855 }
1856
598ddd96 1857 return true;
2bda0e17
KB
1858}
1859
1860// Sort items.
1861
1862// fn is a function which takes 3 long arguments: item1, item2, data.
1863// item1 is the long data associated with a first item (NOT the index).
1864// item2 is the long data associated with a second item (NOT the index).
1865// data is the same value as passed to SortItems.
1866// The return value is a negative number if the first item should precede the second
1867// item, a positive number of the second item should precede the first,
1868// or zero if the two items are equivalent.
1869
1870// data is arbitrary data to be passed to the sort function.
19230604 1871
7cf46fdb
VZ
1872// Internal structures for proxying the user compare function
1873// so that we can pass it the *real* user data
19230604 1874
7cf46fdb
VZ
1875// translate lParam data and call user func
1876struct wxInternalDataSort
19230604 1877{
7cf46fdb 1878 wxListCtrlCompare user_fn;
b18e2046 1879 wxIntPtr data;
7cf46fdb 1880};
19230604 1881
7cf46fdb 1882int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
2bda0e17 1883{
6e2f3084 1884 wxInternalDataSort * const internalData = (wxInternalDataSort *) lParamSort;
19230604 1885
c767a5bb
VZ
1886 wxMSWListItemData *data1 = (wxMSWListItemData *) lParam1;
1887 wxMSWListItemData *data2 = (wxMSWListItemData *) lParam2;
19230604 1888
7cf46fdb
VZ
1889 long d1 = (data1 == NULL ? 0 : data1->lParam);
1890 long d2 = (data2 == NULL ? 0 : data2->lParam);
19230604 1891
7cf46fdb 1892 return internalData->user_fn(d1, d2, internalData->data);
19230604 1893
259c43f6 1894}
19230604 1895
b18e2046 1896bool wxListCtrl::SortItems(wxListCtrlCompare fn, wxIntPtr data)
7cf46fdb 1897{
6e2f3084 1898 wxInternalDataSort internalData;
7cf46fdb
VZ
1899 internalData.user_fn = fn;
1900 internalData.data = data;
19230604 1901
14090241
VZ
1902 // WPARAM cast is needed for mingw/cygwin
1903 if ( !ListView_SortItems(GetHwnd(),
1904 wxInternalDataCompareFunc,
1905 (WPARAM) &internalData) )
19230604
RD
1906 {
1907 wxLogDebug(_T("ListView_SortItems() failed"));
1908
598ddd96 1909 return false;
19230604
RD
1910 }
1911
598ddd96 1912 return true;
2bda0e17
KB
1913}
1914
19230604
RD
1915
1916
edccf428
VZ
1917// ----------------------------------------------------------------------------
1918// message processing
1919// ----------------------------------------------------------------------------
1920
90c6edd7
VZ
1921bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1922{
1923 if ( msg->message == WM_KEYDOWN )
1924 {
6719c06a
VZ
1925 // Only eat VK_RETURN if not being used by the application in
1926 // conjunction with modifiers
1927 if ( msg->wParam == VK_RETURN && !wxIsAnyModifierDown() )
90c6edd7 1928 {
6719c06a 1929 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
90c6edd7
VZ
1930 return false;
1931 }
1932 }
90c6edd7
VZ
1933 return wxControl::MSWShouldPreProcessMessage(msg);
1934}
1935
0edeeb6d 1936bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
2bda0e17 1937{
0edeeb6d 1938 const int id = (signed short)id_;
0b5efdc7 1939 if (cmd == EN_UPDATE)
acb62b84 1940 {
0b5efdc7
VZ
1941 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1942 event.SetEventObject( this );
1943 ProcessCommand(event);
598ddd96 1944 return true;
acb62b84 1945 }
0b5efdc7 1946 else if (cmd == EN_KILLFOCUS)
acb62b84 1947 {
0b5efdc7
VZ
1948 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1949 event.SetEventObject( this );
1950 ProcessCommand(event);
598ddd96 1951 return true;
acb62b84 1952 }
edccf428 1953 else
598ddd96 1954 return false;
0b5efdc7
VZ
1955}
1956
a9fdf824 1957// utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify
a68962fc 1958int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick)
a9fdf824 1959{
674c2750
VZ
1960 // find the column clicked: we have to search for it ourselves as the
1961 // notification message doesn't provide this info
a9fdf824
RR
1962
1963 // where did the click occur?
1964#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
674c2750 1965 if ( nmhdr->code == GN_CONTEXTMENU )
a9fdf824
RR
1966 {
1967 *ptClick = ((NMRGINFO*)nmhdr)->ptAction;
f4322df6 1968 }
a9fdf824
RR
1969 else
1970#endif //__WXWINCE__
1971 if ( !::GetCursorPos(ptClick) )
1972 {
1973 wxLogLastError(_T("GetCursorPos"));
1974 }
1975
674c2750
VZ
1976 // we need to use listctrl coordinates for the event point so this is what
1977 // we return in ptClick, but for comparison with Header_GetItemRect()
1978 // result below we need to use header window coordinates
1979 POINT ptClickHeader = *ptClick;
1980 if ( !::ScreenToClient(nmhdr->hwndFrom, &ptClickHeader) )
a9fdf824 1981 {
674c2750 1982 wxLogLastError(_T("ScreenToClient(listctrl header)"));
a9fdf824
RR
1983 }
1984
674c2750
VZ
1985 if ( !::ScreenToClient(::GetParent(nmhdr->hwndFrom), ptClick) )
1986 {
1987 wxLogLastError(_T("ScreenToClient(listctrl)"));
1988 }
a9fdf824 1989
674c2750 1990 const int colCount = Header_GetItemCount(nmhdr->hwndFrom);
a9fdf824
RR
1991 for ( int col = 0; col < colCount; col++ )
1992 {
674c2750 1993 RECT rect;
a9fdf824
RR
1994 if ( Header_GetItemRect(nmhdr->hwndFrom, col, &rect) )
1995 {
674c2750 1996 if ( ::PtInRect(&rect, ptClickHeader) )
a9fdf824
RR
1997 {
1998 return col;
1999 }
2000 }
2001 }
2002
2003 return wxNOT_FOUND;
2004}
2005
a23fd0e1 2006bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
0b5efdc7 2007{
68c124a1 2008
bdc72a22
VZ
2009 // prepare the event
2010 // -----------------
2011
0b5efdc7 2012 wxListEvent event(wxEVT_NULL, m_windowId);
648bec3e
VZ
2013 event.SetEventObject(this);
2014
0b5efdc7 2015 wxEventType eventType = wxEVT_NULL;
225fe9d6 2016
bdc72a22 2017 NMHDR *nmhdr = (NMHDR *)lParam;
225fe9d6 2018
d1f2eb1d
VZ
2019 // if your compiler is as broken as this, you should really change it: this
2020 // code is needed for normal operation! #ifdef below is only useful for
2021 // automatic rebuilds which are done with a very old compiler version
0cf5b099 2022#ifdef HDN_BEGINTRACKA
d1f2eb1d 2023
11358d39
VZ
2024 // check for messages from the header (in report view)
2025 HWND hwndHdr = ListView_GetHeader(GetHwnd());
225fe9d6 2026
11358d39
VZ
2027 // is it a message from the header?
2028 if ( nmhdr->hwndFrom == hwndHdr )
acb62b84 2029 {
00811ff9 2030 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
0cf5b099 2031
11358d39 2032 event.m_itemIndex = -1;
cb0f56f9 2033
11358d39
VZ
2034 switch ( nmhdr->code )
2035 {
2036 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
2037 // TRACK messages even to ANSI programs: on my system I get
2038 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
2039 //
2040 // work around is to simply catch both versions and hope that it
2041 // works (why should this message exist in ANSI and Unicode is
2042 // beyond me as it doesn't deal with strings at all...)
2b5f62a0
VZ
2043 //
2044 // note that fr HDN_TRACK another possibility could be to use
2045 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
2046 // something other than the item width changes so we'd have to
2047 // filter out the unwanted events then
11358d39
VZ
2048 case HDN_BEGINTRACKA:
2049 case HDN_BEGINTRACKW:
2050 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
2051 // fall through
2052
2053 case HDN_TRACKA:
2054 case HDN_TRACKW:
2055 if ( eventType == wxEVT_NULL )
2056 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
2057 // fall through
2058
2059 case HDN_ENDTRACKA:
2060 case HDN_ENDTRACKW:
2061 if ( eventType == wxEVT_NULL )
2062 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
2b5f62a0
VZ
2063
2064 event.m_item.m_width = nmHDR->pitem->cxy;
11358d39
VZ
2065 event.m_col = nmHDR->iItem;
2066 break;
2067
781a24e8 2068#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
80a81a12 2069 case GN_CONTEXTMENU:
a61d4011 2070#endif //__WXWINCE__
11358d39
VZ
2071 case NM_RCLICK:
2072 {
11358d39 2073 POINT ptClick;
5ea47806 2074
a9fdf824
RR
2075 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
2076 event.m_col = wxMSWGetColumnClicked(nmhdr, &ptClick);
11358d39
VZ
2077 event.m_pointDrag.x = ptClick.x;
2078 event.m_pointDrag.y = ptClick.y;
11358d39
VZ
2079 }
2080 break;
5ea47806 2081
2b5f62a0 2082 case HDN_GETDISPINFOW:
c68f590a
VZ
2083 // letting Windows XP handle this message results in mysterious
2084 // crashes in comctl32.dll seemingly because of bad message
2085 // parameters
2086 //
2087 // I have no idea what is the real cause of the bug (which is,
a9fdf824 2088 // just to make things interesting, impossible to reproduce
c68f590a
VZ
2089 // reliably) but ignoring all these messages does fix it and
2090 // doesn't seem to have any negative consequences
2091 return true;
2b5f62a0 2092
11358d39
VZ
2093 default:
2094 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2095 }
2096 }
d1f2eb1d 2097 else
0cf5b099 2098#endif // defined(HDN_BEGINTRACKA)
164e8d41 2099 if ( nmhdr->hwndFrom == GetHwnd() )
11358d39
VZ
2100 {
2101 // almost all messages use NM_LISTVIEW
2102 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
bdc72a22 2103
2b5f62a0
VZ
2104 const int iItem = nmLV->iItem;
2105
68c124a1 2106
68c124a1
RD
2107 // If we have a valid item then check if there is a data value
2108 // associated with it and put it in the event.
2109 if ( iItem >= 0 && iItem < GetItemCount() )
2110 {
c767a5bb
VZ
2111 wxMSWListItemData *internaldata =
2112 MSWGetItemData(iItem);
2b5f62a0 2113
68c124a1
RD
2114 if ( internaldata )
2115 event.m_item.m_data = internaldata->lParam;
2b5f62a0 2116 }
bdc72a22 2117
f0755097 2118 bool processed = true;
11358d39
VZ
2119 switch ( nmhdr->code )
2120 {
2121 case LVN_BEGINRDRAG:
2122 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
2123 // fall through
7bf1474a 2124
11358d39
VZ
2125 case LVN_BEGINDRAG:
2126 if ( eventType == wxEVT_NULL )
2127 {
2128 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
2129 }
bdc72a22 2130
2b5f62a0 2131 event.m_itemIndex = iItem;
11358d39
VZ
2132 event.m_pointDrag.x = nmLV->ptAction.x;
2133 event.m_pointDrag.y = nmLV->ptAction.y;
2134 break;
2135
8c21905b 2136 // NB: we have to handle both *A and *W versions here because some
d2ed74b9
VZ
2137 // versions of comctl32.dll send ANSI messages even to the
2138 // Unicode windows
8c21905b 2139 case LVN_BEGINLABELEDITA:
8c21905b
VS
2140 case LVN_BEGINLABELEDITW:
2141 {
d2ed74b9
VZ
2142 wxLV_ITEM item;
2143 if ( nmhdr->code == LVN_BEGINLABELEDITA )
2144 {
2145 item.Init(((LV_DISPINFOA *)lParam)->item);
2146 }
2147 else // LVN_BEGINLABELEDITW
2148 {
2149 item.Init(((LV_DISPINFOW *)lParam)->item);
2150 }
2151
8c21905b 2152 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
8c21905b
VS
2153 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
2154 event.m_itemIndex = event.m_item.m_itemId;
2155 }
2156 break;
2157
2158 case LVN_ENDLABELEDITA:
d2ed74b9 2159 case LVN_ENDLABELEDITW:
8c21905b 2160 {
d2ed74b9
VZ
2161 wxLV_ITEM item;
2162 if ( nmhdr->code == LVN_ENDLABELEDITA )
c546974f 2163 {
d2ed74b9
VZ
2164 item.Init(((LV_DISPINFOA *)lParam)->item);
2165 }
2166 else // LVN_ENDLABELEDITW
2167 {
2168 item.Init(((LV_DISPINFOW *)lParam)->item);
c546974f 2169 }
8c21905b 2170
d2ed74b9
VZ
2171 // was editing cancelled?
2172 const LV_ITEM& lvi = (LV_ITEM)item;
2173 if ( !lvi.pszText || lvi.iItem == -1 )
c546974f 2174 {
5cd4cb75
VZ
2175 // EDIT control will be deleted by the list control
2176 // itself so prevent us from deleting it as well
2177 DeleteEditControl();
d2ed74b9
VZ
2178
2179 event.SetEditCanceled(true);
c546974f 2180 }
8c21905b 2181
d2ed74b9
VZ
2182 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
2183 wxConvertFromMSWListItem(NULL, event.m_item, item);
11358d39
VZ
2184 event.m_itemIndex = event.m_item.m_itemId;
2185 }
2186 break;
edccf428 2187
11358d39
VZ
2188 case LVN_COLUMNCLICK:
2189 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
2190 event.m_itemIndex = -1;
2191 event.m_col = nmLV->iSubItem;
2192 break;
bdc72a22 2193
11358d39
VZ
2194 case LVN_DELETEALLITEMS:
2195 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
2196 event.m_itemIndex = -1;
11358d39
VZ
2197 break;
2198
2199 case LVN_DELETEITEM:
07763c99
VZ
2200 if ( m_count == 0 )
2201 {
2202 // this should be prevented by the post-processing code
2203 // below, but "just in case"
598ddd96 2204 return false;
07763c99 2205 }
68c124a1 2206
11358d39 2207 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
2b5f62a0 2208 event.m_itemIndex = iItem;
c767a5bb
VZ
2209
2210 // delete the associated internal data
2211 if ( wxMSWListItemData *data = MSWGetItemData(iItem) )
2212 {
2213 const unsigned count = m_internalData.size();
2214 for ( unsigned n = 0; n < count; n++ )
2215 {
2216 if ( m_internalData[n] == data )
2217 {
2218 m_internalData.erase(m_internalData.begin() + n);
2219 delete data;
2220 data = NULL;
2221 break;
2222 }
2223 }
2224
31cc5049 2225 wxASSERT_MSG( !data, "invalid internal data pointer?" );
c767a5bb 2226 }
11358d39
VZ
2227 break;
2228
11358d39
VZ
2229 case LVN_INSERTITEM:
2230 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
2b5f62a0 2231 event.m_itemIndex = iItem;
11358d39 2232 break;
edccf428 2233
11358d39 2234 case LVN_ITEMCHANGED:
648bec3e 2235 // we translate this catch all message into more interesting
77ffb593 2236 // (and more easy to process) wxWidgets events
648bec3e 2237
2b5f62a0
VZ
2238 // first of all, we deal with the state change events only and
2239 // only for valid items (item == -1 for the virtual list
2240 // control)
2241 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
11358d39 2242 {
648bec3e
VZ
2243 // temp vars for readability
2244 const UINT stOld = nmLV->uOldState;
2245 const UINT stNew = nmLV->uNewState;
2246
2b5f62a0
VZ
2247 event.m_item.SetId(iItem);
2248 event.m_item.SetMask(wxLIST_MASK_TEXT |
2249 wxLIST_MASK_IMAGE |
2250 wxLIST_MASK_DATA);
2251 GetItem(event.m_item);
2252
648bec3e
VZ
2253 // has the focus changed?
2254 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2255 {
2256 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2b5f62a0 2257 event.m_itemIndex = iItem;
648bec3e
VZ
2258 }
2259
2260 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2261 {
2262 if ( eventType != wxEVT_NULL )
2263 {
2264 // focus and selection have both changed: send the
2265 // focus event from here and the selection one
2266 // below
2267 event.SetEventType(eventType);
937013e0 2268 (void)HandleWindowEvent(event);
648bec3e
VZ
2269 }
2270 else // no focus event to send
2271 {
2272 // then need to set m_itemIndex as it wasn't done
2273 // above
2b5f62a0 2274 event.m_itemIndex = iItem;
648bec3e
VZ
2275 }
2276
2277 eventType = stNew & LVIS_SELECTED
2278 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2279 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2280 }
edccf428 2281 }
648bec3e
VZ
2282
2283 if ( eventType == wxEVT_NULL )
edccf428 2284 {
648bec3e 2285 // not an interesting event for us
598ddd96 2286 return false;
edccf428 2287 }
648bec3e 2288
11358d39 2289 break;
225fe9d6 2290
11358d39
VZ
2291 case LVN_KEYDOWN:
2292 {
2293 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2294 WORD wVKey = info->wVKey;
2295
2296 // get the current selection
2297 long lItem = GetNextItem(-1,
2298 wxLIST_NEXT_ALL,
2299 wxLIST_STATE_SELECTED);
2300
2301 // <Enter> or <Space> activate the selected item if any (but
ec27ba21
VZ
2302 // not with any modifiers as they have a predefined meaning
2303 // then)
11358d39
VZ
2304 if ( lItem != -1 &&
2305 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
ec27ba21 2306 !wxIsAnyModifierDown() )
11358d39
VZ
2307 {
2308 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2309 }
2310 else
2311 {
2312 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
185d0094
VZ
2313
2314 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2315 // value which should be used as is
2316 int code = wxCharCodeMSWToWX(wVKey);
2317 event.m_code = code ? code : wVKey;
11358d39 2318 }
7db91489 2319
11358d39
VZ
2320 event.m_itemIndex =
2321 event.m_item.m_itemId = lItem;
2322
2323 if ( lItem != -1 )
2324 {
2325 // fill the other fields too
2326 event.m_item.m_text = GetItemText(lItem);
2327 event.m_item.m_data = GetItemData(lItem);
2328 }
2329 }
2330 break;
2331
2332 case NM_DBLCLK:
2333 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2334 // anything else
2335 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
f6bcfd97 2336 {
598ddd96 2337 return true;
f6bcfd97 2338 }
edccf428 2339
11358d39
VZ
2340 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2341 // if it happened on an item (and not on empty place)
2b5f62a0 2342 if ( iItem == -1 )
11358d39
VZ
2343 {
2344 // not on item
598ddd96 2345 return false;
11358d39 2346 }
edccf428 2347
11358d39 2348 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2b5f62a0
VZ
2349 event.m_itemIndex = iItem;
2350 event.m_item.m_text = GetItemText(iItem);
2351 event.m_item.m_data = GetItemData(iItem);
11358d39 2352 break;
225fe9d6 2353
781a24e8 2354#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
80a81a12 2355 case GN_CONTEXTMENU:
a61d4011 2356#endif //__WXWINCE__
11358d39 2357 case NM_RCLICK:
225fe9d6
VZ
2358 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2359 // don't do anything else
2360 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2361 {
598ddd96 2362 return true;
225fe9d6 2363 }
cb0f56f9 2364
225fe9d6
VZ
2365 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2366 LV_HITTESTINFO lvhti;
2367 wxZeroMemory(lvhti);
11c7d5b6 2368
781a24e8 2369#if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
fb4079af
VZ
2370 if ( nmhdr->code == GN_CONTEXTMENU )
2371 {
2372 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2373 }
2374 else
a61d4011 2375#endif //__WXWINCE__
fb4079af
VZ
2376 {
2377 ::GetCursorPos(&(lvhti.pt));
2378 }
2379
2380 ::ScreenToClient(GetHwnd(), &lvhti.pt);
2381 if ( ListView_HitTest(GetHwnd(), &lvhti) != -1 )
cb0f56f9 2382 {
225fe9d6
VZ
2383 if ( lvhti.flags & LVHT_ONITEM )
2384 {
2385 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2386 event.m_itemIndex = lvhti.iItem;
a1f79c1e
VZ
2387 event.m_pointDrag.x = lvhti.pt.x;
2388 event.m_pointDrag.y = lvhti.pt.y;
225fe9d6 2389 }
cb0f56f9 2390 }
11358d39 2391 break;
bdc72a22 2392
5b59df83 2393#ifdef NM_CUSTOMDRAW
80c54c3d 2394 case NM_CUSTOMDRAW:
11358d39 2395 *result = OnCustomDraw(lParam);
63166611 2396
e6b1317b 2397 return *result != CDRF_DODEFAULT;
6ecfe2ac 2398#endif // _WIN32_IE >= 0x300
0b5efdc7 2399
11358d39
VZ
2400 case LVN_ODCACHEHINT:
2401 {
2402 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
614391dc 2403
11358d39 2404 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
e623926d 2405
0ea0359b
VZ
2406 // we get some really stupid cache hints like ones for
2407 // items in range 0..0 for an empty control or, after
2408 // deleting an item, for items in invalid range -- filter
2409 // this garbage out
2410 if ( cacheHint->iFrom > cacheHint->iTo )
598ddd96 2411 return false;
0ea0359b
VZ
2412
2413 event.m_oldItemIndex = cacheHint->iFrom;
2414
2415 const long iMax = GetItemCount();
2416 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2417 : iMax - 1;
e623926d 2418 }
11358d39 2419 break;
614391dc 2420
206391cb 2421#ifdef HAVE_NMLVFINDITEM
f0755097
VZ
2422 case LVN_ODFINDITEM:
2423 // this message is only used with the virtual list control but
2424 // even there we don't want to always use it: in a control with
2425 // sufficiently big number of items (defined as > 1000 here),
2426 // accidentally pressing a key could result in hanging an
2427 // application waiting while it performs linear search
2428 if ( IsVirtual() && GetItemCount() <= 1000 )
2429 {
2430 NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
2431
2432 // no match by default
2433 *result = -1;
2434
2435 // we only handle string-based searches here
2436 //
2437 // TODO: what about LVFI_PARTIAL, should we handle this?
2438 if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
2439 {
2440 return false;
2441 }
2442
2443 const wxChar * const searchstr = pFindInfo->lvfi.psz;
2444 const size_t len = wxStrlen(searchstr);
2445
2446 // this is the first item we should examine, search from it
2447 // wrapping if necessary
2448 const int startPos = pFindInfo->iStart;
2449 const int maxPos = GetItemCount();
2450 wxCHECK_MSG( startPos <= maxPos, false,
2451 _T("bad starting position in LVN_ODFINDITEM") );
2452
2453 int currentPos = startPos;
2454 do
2455 {
2456 // wrap to the beginning if necessary
2457 if ( currentPos == maxPos )
2458 {
2459 // somewhat surprizingly, LVFI_WRAP isn't set in
2460 // flags but we still should wrap
2461 currentPos = 0;
2462 }
2463
2464 // does this item begin with searchstr?
2465 if ( wxStrnicmp(searchstr,
2466 GetItemText(currentPos), len) == 0 )
2467 {
2468 *result = currentPos;
2469 break;
2470 }
2471 }
2472 while ( ++currentPos != startPos );
2473
2474 if ( *result == -1 )
2475 {
2476 // not found
2477 return false;
2478 }
2479
2480 SetItemState(*result,
2481 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
2482 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
2483 EnsureVisible(*result);
2484 return true;
2485 }
2486 else
2487 {
2488 processed = false;
2489 }
2490 break;
206391cb 2491#endif // HAVE_NMLVFINDITEM
f0755097 2492
11358d39
VZ
2493 case LVN_GETDISPINFO:
2494 if ( IsVirtual() )
2495 {
2496 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
98ec9dbe 2497
11358d39
VZ
2498 LV_ITEM& lvi = info->item;
2499 long item = lvi.iItem;
98ec9dbe 2500
11358d39
VZ
2501 if ( lvi.mask & LVIF_TEXT )
2502 {
2503 wxString text = OnGetItemText(item, lvi.iSubItem);
64accea5 2504 wxStrlcpy(lvi.pszText, text.c_str(), lvi.cchTextMax);
11358d39 2505 }
98ec9dbe 2506
5b59df83
VZ
2507 // see comment at the end of wxListCtrl::GetColumn()
2508#ifdef NM_CUSTOMDRAW
11358d39
VZ
2509 if ( lvi.mask & LVIF_IMAGE )
2510 {
208458a7 2511 lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem);
11358d39 2512 }
5b59df83 2513#endif // NM_CUSTOMDRAW
98ec9dbe 2514
19def5e7
VZ
2515 // even though we never use LVM_SETCALLBACKMASK, we still
2516 // can get messages with LVIF_STATE in lvi.mask under Vista
2517 if ( lvi.mask & LVIF_STATE )
2518 {
2519 // we don't have anything to return from here...
2520 lvi.stateMask = 0;
2521 }
98ec9dbe 2522
598ddd96 2523 return true;
11358d39
VZ
2524 }
2525 // fall through
98ec9dbe 2526
11358d39 2527 default:
f0755097 2528 processed = false;
11358d39 2529 }
f0755097
VZ
2530
2531 if ( !processed )
2532 return wxControl::MSWOnNotify(idCtrl, lParam, result);
11358d39
VZ
2533 }
2534 else
2535 {
2536 // where did this one come from?
598ddd96 2537 return false;
acb62b84
VZ
2538 }
2539
bdc72a22
VZ
2540 // process the event
2541 // -----------------
2542
0b5efdc7 2543 event.SetEventType(eventType);
acb62b84 2544
fb4079af
VZ
2545 // fill in the item before passing it to the event handler if we do have a
2546 // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED)
2547 if ( event.m_itemIndex != -1 && !event.m_item.GetMask() )
2548 {
2549 wxListItem& item = event.m_item;
2550
2551 item.SetId(event.m_itemIndex);
2552 item.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_DATA);
2553 GetItem(item);
2554 }
2555
937013e0 2556 bool processed = HandleWindowEvent(event);
acb62b84 2557
bdc72a22
VZ
2558 // post processing
2559 // ---------------
225fe9d6 2560 switch ( nmhdr->code )
acb62b84 2561 {
6932a32c 2562 case LVN_DELETEALLITEMS:
598ddd96 2563 // always return true to suppress all additional LVN_DELETEITEM
6932a32c
VZ
2564 // notifications - this makes deleting all items from a list ctrl
2565 // much faster
2566 *result = TRUE;
f7df21aa
VZ
2567
2568 // also, we may free all user data now (couldn't do it before as
2569 // the user should have access to it in OnDeleteAllItems() handler)
2570 FreeAllInternalData();
07763c99
VZ
2571
2572 // the control is empty now, synchronize the cached number of items
2573 // with the real one
2574 m_count = 0;
598ddd96 2575 return true;
6932a32c 2576
8c21905b
VS
2577 case LVN_ENDLABELEDITA:
2578 case LVN_ENDLABELEDITW:
3103e8a9 2579 // logic here is inverted compared to all the other messages
614391dc
VZ
2580 *result = event.IsAllowed();
2581
5cd4cb75
VZ
2582 // EDIT control will be deleted by the list control itself so
2583 // prevent us from deleting it as well
2584 DeleteEditControl();
2b5f62a0 2585
598ddd96 2586 return true;
acb62b84 2587 }
acb62b84 2588
68c124a1
RD
2589 if ( processed )
2590 *result = !event.IsAllowed();
fd3f686c 2591
68c124a1 2592 return processed;
2bda0e17
KB
2593}
2594
e6b1317b
VZ
2595// ----------------------------------------------------------------------------
2596// custom draw stuff
2597// ----------------------------------------------------------------------------
2598
5b59df83
VZ
2599// see comment at the end of wxListCtrl::GetColumn()
2600#ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
63166611 2601
e6b1317b
VZ
2602static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd)
2603{
2604 RECT rc;
89cafab0 2605 wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_BOUNDS, rc);
e6b1317b
VZ
2606
2607 RECT rcIcon;
89cafab0 2608 wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_ICON, rcIcon);
e6b1317b
VZ
2609
2610 // exclude the icon part, neither the selection background nor focus rect
2611 // should cover it
2612 rc.left = rcIcon.right;
2613
2614 return rc;
2615}
2616
0c1602b8
VZ
2617static
2618bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont, int colCount)
e6b1317b
VZ
2619{
2620 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2621
2622 HDC hdc = nmcd.hdc;
2623 HWND hwndList = nmcd.hdr.hwndFrom;
223b7504 2624 const int col = pLVCD->iSubItem;
e6b1317b
VZ
2625 const DWORD item = nmcd.dwItemSpec;
2626
e6b1317b
VZ
2627 // the font must be valid, otherwise we wouldn't be painting the item at all
2628 SelectInHDC selFont(hdc, hfont);
2629
2630 // get the rectangle to paint
89cafab0 2631 int subitem = colCount ? col + 1 : col;
e6b1317b 2632 RECT rc;
89cafab0
VZ
2633 wxGetListCtrlSubItemRect(hwndList, item, subitem, LVIR_BOUNDS, rc);
2634 rc.left += 6;
e6b1317b
VZ
2635
2636 // get the image and text to draw
2637 wxChar text[512];
2638 LV_ITEM it;
2639 wxZeroMemory(it);
2640 it.mask = LVIF_TEXT | LVIF_IMAGE;
2641 it.iItem = item;
223b7504 2642 it.iSubItem = col;
e6b1317b
VZ
2643 it.pszText = text;
2644 it.cchTextMax = WXSIZEOF(text);
2645 ListView_GetItem(hwndList, &it);
2646
25e6a4a6
VZ
2647 HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL);
2648 if ( himl && ImageList_GetImageCount(himl) )
e6b1317b 2649 {
25e6a4a6
VZ
2650 if ( it.iImage != -1 )
2651 {
2652 ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top,
2653 nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED
2654 : ILD_TRANSPARENT);
2655 }
e6b1317b 2656
25e6a4a6 2657 // notice that even if this item doesn't have any image, the list
23535447
VZ
2658 // control still leaves space for the image in the first column if the
2659 // image list is not empty (presumably so that items with and without
2660 // images align?)
2661 if ( it.iImage != -1 || it.iSubItem == 0 )
2662 {
2663 int wImage, hImage;
2664 ImageList_GetIconSize(himl, &wImage, &hImage);
e6b1317b 2665
23535447
VZ
2666 rc.left += wImage + 2;
2667 }
e6b1317b
VZ
2668 }
2669
2670 ::SetBkMode(hdc, TRANSPARENT);
2671
223b7504 2672 UINT fmt = DT_SINGLELINE |
5cce8340
VZ
2673#ifndef __WXWINCE__
2674 DT_WORD_ELLIPSIS |
2675#endif // __WXWINCE__
223b7504
VZ
2676 DT_NOPREFIX |
2677 DT_VCENTER;
2678
2679 LV_COLUMN lvCol;
2680 wxZeroMemory(lvCol);
2681 lvCol.mask = LVCF_FMT;
2682 if ( ListView_GetColumn(hwndList, col, &lvCol) )
2683 {
2684 switch ( lvCol.fmt & LVCFMT_JUSTIFYMASK )
2685 {
2686 case LVCFMT_LEFT:
2687 fmt |= DT_LEFT;
2688 break;
2689
2690 case LVCFMT_CENTER:
2691 fmt |= DT_CENTER;
2692 break;
2693
2694 case LVCFMT_RIGHT:
2695 fmt |= DT_RIGHT;
2696 break;
2697 }
2698 }
2699 //else: failed to get alignment, assume it's DT_LEFT (default)
2700
2701 DrawText(hdc, text, -1, &rc, fmt);
2702
2703 return true;
e6b1317b
VZ
2704}
2705
2706static void HandleItemPostpaint(NMCUSTOMDRAW nmcd)
2707{
2708 if ( nmcd.uItemState & CDIS_FOCUS )
2709 {
2710 RECT rc = GetCustomDrawnItemRect(nmcd);
2711
2712 // don't use the provided HDC, it's in some strange state by now
2713 ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc);
2714 }
2715}
2716
2717// pLVCD->clrText and clrTextBk should contain the colours to use
2718static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2719{
2720 NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut
2721
13845bd5
VZ
2722 const HWND hwndList = nmcd.hdr.hwndFrom;
2723 const int item = nmcd.dwItemSpec;
e6b1317b
VZ
2724
2725 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2726 // the item is not at all selected for some reason (comctl32 6), but we
2727 // also can't always trust ListView_GetItem() as it could return the old
2728 // item status if we're called just after the (de)selection, so remember
2729 // the last item to gain selection and also check for it here
2730 for ( int i = -1;; )
2731 {
2732 i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED);
2733 if ( i == -1 )
2734 {
2735 nmcd.uItemState &= ~CDIS_SELECTED;
2736 break;
2737 }
2738
13845bd5 2739 if ( i == item )
e6b1317b
VZ
2740 {
2741 nmcd.uItemState |= CDIS_SELECTED;
2742 break;
2743 }
2744 }
2745
13845bd5 2746 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
36e5a9a7
VZ
2747 //
2748 // NB: cast is needed to work around the bug in mingw32 headers which don't
2749 // have it inside ListView_GetNextItem() itself (unlike SDK ones)
728f972b 2750 if ( ::GetFocus() == hwndList &&
36e5a9a7
VZ
2751 ListView_GetNextItem(
2752 hwndList, static_cast<WPARAM>(-1), LVNI_FOCUSED) == item )
13845bd5
VZ
2753 {
2754 nmcd.uItemState |= CDIS_FOCUS;
2755 }
2756 else
2757 {
2758 nmcd.uItemState &= ~CDIS_FOCUS;
2759 }
2760
e6b1317b
VZ
2761 if ( nmcd.uItemState & CDIS_SELECTED )
2762 {
2763 int syscolFg, syscolBg;
2764 if ( ::GetFocus() == hwndList )
2765 {
2766 syscolFg = COLOR_HIGHLIGHTTEXT;
2767 syscolBg = COLOR_HIGHLIGHT;
2768 }
2769 else // selected but unfocused
2770 {
2771 syscolFg = COLOR_WINDOWTEXT;
2772 syscolBg = COLOR_BTNFACE;
2773
2774 // don't grey out the icon in this case neither
2775 nmcd.uItemState &= ~CDIS_SELECTED;
2776 }
2777
2778 pLVCD->clrText = ::GetSysColor(syscolFg);
2779 pLVCD->clrTextBk = ::GetSysColor(syscolBg);
2780 }
2781 //else: not selected, use normal colours from pLVCD
2782
2783 HDC hdc = nmcd.hdc;
2784 RECT rc = GetCustomDrawnItemRect(nmcd);
2785
2786 ::SetTextColor(hdc, pLVCD->clrText);
2787 ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk));
2788
2789 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2790 // problems so just draw everything except the focus rect from here instead
2791 const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList));
2792 for ( int col = 0; col < colCount; col++ )
2793 {
2794 pLVCD->iSubItem = col;
0c1602b8 2795 HandleSubItemPrepaint(pLVCD, hfont, colCount);
e6b1317b
VZ
2796 }
2797
2798 HandleItemPostpaint(nmcd);
2799}
2800
2801static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl,
2802 LPNMLVCUSTOMDRAW pLVCD,
2803 wxListItemAttr *attr)
2804{
2805 if ( !attr )
2806 {
2807 // nothing to do for this item
2808 return CDRF_DODEFAULT;
2809 }
2810
2811
2812 // set the colours to use for text drawing
3568f700
WS
2813 pLVCD->clrText = attr->HasTextColour()
2814 ? wxColourToRGB(attr->GetTextColour())
2815 : wxColourToRGB(listctrl->GetTextColour());
2816 pLVCD->clrTextBk = attr->HasBackgroundColour()
2817 ? wxColourToRGB(attr->GetBackgroundColour())
2818 : wxColourToRGB(listctrl->GetBackgroundColour());
e6b1317b
VZ
2819
2820 // select the font if non default one is specified
2821 if ( attr->HasFont() )
2822 {
2823 wxFont font = attr->GetFont();
2824 if ( font.GetEncoding() != wxFONTENCODING_SYSTEM )
2825 {
2826 // the standard control ignores the font encoding/charset, at least
2827 // with recent comctl32.dll versions (5 and 6, it uses to work with
2828 // 4.something) so we have to draw the item entirely ourselves in
2829 // this case
2830 HandleItemPaint(pLVCD, GetHfontOf(font));
2831 return CDRF_SKIPDEFAULT;
2832 }
2833
2834 ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font));
2835
2836 return CDRF_NEWFONT;
2837 }
2838
2839 return CDRF_DODEFAULT;
2840}
2841
63166611
VZ
2842WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2843{
e6b1317b
VZ
2844 LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam;
2845 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
63166611
VZ
2846 switch ( nmcd.dwDrawStage )
2847 {
2848 case CDDS_PREPAINT:
2849 // if we've got any items with non standard attributes,
2850 // notify us before painting each item
2851 //
2852 // for virtual controls, always suppose that we have attributes as
2853 // there is no way to check for this
e6b1317b
VZ
2854 if ( IsVirtual() || m_hasAnyAttr )
2855 return CDRF_NOTIFYITEMDRAW;
2856 break;
63166611
VZ
2857
2858 case CDDS_ITEMPREPAINT:
eab1336c
VZ
2859 // get a message for each subitem
2860 return CDRF_NOTIFYITEMDRAW;
2861
2862 case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
e6b1317b 2863 const int item = nmcd.dwItemSpec;
eab1336c 2864 const int column = pLVCD->iSubItem;
63166611 2865
e6b1317b
VZ
2866 // we get this message with item == 0 for an empty control, we
2867 // must ignore it as calling OnGetItemAttr() would be wrong
2868 if ( item < 0 || item >= GetItemCount() )
2869 break;
eab1336c
VZ
2870 // same for columns
2871 if ( column < 0 || column >= GetColumnCount() )
2872 break;
63166611 2873
eab1336c 2874 return HandleItemPrepaint(this, pLVCD, DoGetItemColumnAttr(item, column));
63166611 2875 }
e6b1317b
VZ
2876
2877 return CDRF_DODEFAULT;
63166611
VZ
2878}
2879
2880#endif // NM_CUSTOMDRAW supported
2881
2702ed5a
JS
2882// Necessary for drawing hrules and vrules, if specified
2883void wxListCtrl::OnPaint(wxPaintEvent& event)
2884{
99366b91 2885 const int itemCount = GetItemCount();
d3acc83a
VZ
2886 const bool drawHRules = HasFlag(wxLC_HRULES);
2887 const bool drawVRules = HasFlag(wxLC_VRULES);
2e6a9dad 2888
99366b91 2889 if (!InReportView() || !(drawHRules || drawVRules) || !itemCount)
2e6a9dad
VS
2890 {
2891 event.Skip();
2892 return;
2893 }
2894
2702ed5a
JS
2895 wxPaintDC dc(this);
2896
2897 wxControl::OnPaint(event);
2898
2899 // Reset the device origin since it may have been set
2900 dc.SetDeviceOrigin(0, 0);
2901
36c10aa1 2902 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
2702ed5a
JS
2903 dc.SetPen(pen);
2904 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2905
2906 wxSize clientSize = GetClientSize();
2907 wxRect itemRect;
2702ed5a 2908
625cb8c0 2909 if (drawHRules)
2702ed5a 2910 {
99366b91
VZ
2911 const long top = GetTopItem();
2912 for ( int i = top; i < top + GetCountPerPage() + 1; i++ )
2702ed5a 2913 {
625cb8c0 2914 if (GetItemRect(i, itemRect))
ca286917 2915 {
5cb598ae 2916 int cy = itemRect.GetTop();
625cb8c0
RD
2917 if (i != 0) // Don't draw the first one
2918 {
2919 dc.DrawLine(0, cy, clientSize.x, cy);
2920 }
2921 // Draw last line
2cefcb34 2922 if (i == itemCount - 1)
625cb8c0
RD
2923 {
2924 cy = itemRect.GetBottom();
2925 dc.DrawLine(0, cy, clientSize.x, cy);
99366b91 2926 break;
625cb8c0 2927 }
2702ed5a
JS
2928 }
2929 }
2930 }
99366b91
VZ
2931
2932 if (drawVRules)
2702ed5a
JS
2933 {
2934 wxRect firstItemRect;
2935 GetItemRect(0, firstItemRect);
2936
99366b91 2937 if (GetItemRect(itemCount - 1, itemRect))
2702ed5a 2938 {
9eaba692
VZ
2939 // this is a fix for bug 673394: erase the pixels which we would
2940 // otherwise leave on the screen
2941 static const int gap = 2;
2942 dc.SetPen(*wxTRANSPARENT_PEN);
2943 dc.SetBrush(wxBrush(GetBackgroundColour()));
2944 dc.DrawRectangle(0, firstItemRect.GetY() - gap,
2945 clientSize.GetWidth(), gap);
2946
2947 dc.SetPen(pen);
2948 dc.SetBrush(*wxTRANSPARENT_BRUSH);
67d3fc49 2949
ceef3893
VZ
2950 const int numCols = GetColumnCount();
2951 wxVector<int> indexArray(numCols);
2952 if ( !ListView_GetColumnOrderArray(GetHwnd(),
2953 numCols,
2954 &indexArray[0]) )
7a8dce71
VZ
2955 {
2956 wxFAIL_MSG( _T("invalid column index array in OnPaint()") );
ceef3893 2957 return;
7a8dce71 2958 }
67d3fc49 2959
2702ed5a 2960 int x = itemRect.GetX();
67d3fc49 2961 for (int col = 0; col < numCols; col++)
2702ed5a 2962 {
67d3fc49 2963 int colWidth = GetColumnWidth(indexArray[col]);
2702ed5a 2964 x += colWidth ;
9eaba692
VZ
2965 dc.DrawLine(x-1, firstItemRect.GetY() - gap,
2966 x-1, itemRect.GetBottom());
2702ed5a
JS
2967 }
2968 }
2969 }
2970}
2971
4bd6ae0f
VZ
2972WXLRESULT
2973wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2974{
777f37e0 2975 switch ( nMsg )
4bd6ae0f 2976 {
777f37e0
VZ
2977#ifdef WM_PRINT
2978 case WM_PRINT:
2979 // we should bypass our own WM_PRINT handling as we don't handle
2980 // PRF_CHILDREN flag, so leave it to the native control itself
2981 return MSWDefWindowProc(nMsg, wParam, lParam);
4bd6ae0f
VZ
2982#endif // WM_PRINT
2983
777f37e0
VZ
2984 case WM_CONTEXTMENU:
2985 // because this message is propagated upwards the child-parent
2986 // chain, we get it for the right clicks on the header window but
2987 // this is confusing in wx as right clicking there already
2988 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2989 // so just ignore them
2990 if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) )
2991 return 0;
2992 //else: break
2993 }
2994
4bd6ae0f
VZ
2995 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
2996}
2997
98ec9dbe
VZ
2998// ----------------------------------------------------------------------------
2999// virtual list controls
3000// ----------------------------------------------------------------------------
3001
d699f48b 3002wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
98ec9dbe
VZ
3003{
3004 // this is a pure virtual function, in fact - which is not really pure
3005 // because the controls which are not virtual don't need to implement it
271d1110 3006 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
98ec9dbe
VZ
3007
3008 return wxEmptyString;
3009}
3010
d699f48b 3011int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
98ec9dbe 3012{
611a725e
RD
3013 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
3014 -1,
208458a7
RD
3015 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
3016 return -1;
3017}
3018
3019int wxListCtrl::OnGetItemColumnImage(long item, long column) const
3020{
3021 if (!column)
3022 return OnGetItemImage(item);
3023
98ec9dbe
VZ
3024 return -1;
3025}
3026
d699f48b 3027wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
6c02c329 3028{
63166611 3029 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
6c02c329
VZ
3030 _T("invalid item index in OnGetItemAttr()") );
3031
3032 // no attributes by default
3033 return NULL;
3034}
3035
eab1336c 3036wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const
e6b1317b 3037{
c767a5bb
VZ
3038 if ( IsVirtual() )
3039 return OnGetItemColumnAttr(item, column);
3040
3041 wxMSWListItemData * const data = MSWGetItemData(item);
3042 return data ? data->attr : NULL;
e6b1317b
VZ
3043}
3044
98ec9dbe
VZ
3045void wxListCtrl::SetItemCount(long count)
3046{
3047 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
3048
54759554
VZ
3049 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
3050 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
98ec9dbe
VZ
3051 {
3052 wxLogLastError(_T("ListView_SetItemCount"));
3053 }
68c124a1
RD
3054 m_count = count;
3055 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
3056 wxT("m_count should match ListView_GetItemCount"));
98ec9dbe
VZ
3057}
3058
a7f560a2
VZ
3059void wxListCtrl::RefreshItem(long item)
3060{
ebc9b89d 3061 RefreshItems(item, item);
a7f560a2
VZ
3062}
3063
3064void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
3065{
ebc9b89d 3066 ListView_RedrawItems(GetHwnd(), itemFrom, itemTo);
a7f560a2
VZ
3067}
3068
97c611f8
VZ
3069// ----------------------------------------------------------------------------
3070// wxWin <-> MSW items conversions
3071// ----------------------------------------------------------------------------
3072
8dff2b5c
VZ
3073static void wxConvertFromMSWListItem(HWND hwndListCtrl,
3074 wxListItem& info,
3075 LV_ITEM& lvItem)
2bda0e17 3076{
c767a5bb
VZ
3077 wxMSWListItemData *internaldata =
3078 (wxMSWListItemData *) lvItem.lParam;
7cf46fdb
VZ
3079
3080 if (internaldata)
3081 info.m_data = internaldata->lParam;
3082
0b5efdc7
VZ
3083 info.m_mask = 0;
3084 info.m_state = 0;
3085 info.m_stateMask = 0;
3086 info.m_itemId = lvItem.iItem;
acb62b84 3087
0b5efdc7 3088 long oldMask = lvItem.mask;
acb62b84 3089
598ddd96 3090 bool needText = false;
8dff2b5c 3091 if (hwndListCtrl != 0)
acb62b84 3092 {
0b5efdc7 3093 if ( lvItem.mask & LVIF_TEXT )
598ddd96 3094 needText = false;
0b5efdc7 3095 else
598ddd96 3096 needText = true;
acb62b84 3097
0b5efdc7
VZ
3098 if ( needText )
3099 {
837e5743 3100 lvItem.pszText = new wxChar[513];
0b5efdc7
VZ
3101 lvItem.cchTextMax = 512;
3102 }
edccf428 3103 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
8dff2b5c 3104 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
0b5efdc7 3105 }
acb62b84 3106
0b5efdc7 3107 if ( lvItem.mask & LVIF_STATE )
acb62b84 3108 {
0b5efdc7
VZ
3109 info.m_mask |= wxLIST_MASK_STATE;
3110
3111 if ( lvItem.stateMask & LVIS_CUT)
3112 {
edccf428 3113 info.m_stateMask |= wxLIST_STATE_CUT;
0b5efdc7 3114 if ( lvItem.state & LVIS_CUT )
edccf428 3115 info.m_state |= wxLIST_STATE_CUT;
0b5efdc7
VZ
3116 }
3117 if ( lvItem.stateMask & LVIS_DROPHILITED)
3118 {
edccf428 3119 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
0b5efdc7 3120 if ( lvItem.state & LVIS_DROPHILITED )
edccf428 3121 info.m_state |= wxLIST_STATE_DROPHILITED;
0b5efdc7
VZ
3122 }
3123 if ( lvItem.stateMask & LVIS_FOCUSED)
3124 {
edccf428 3125 info.m_stateMask |= wxLIST_STATE_FOCUSED;
0b5efdc7 3126 if ( lvItem.state & LVIS_FOCUSED )
edccf428 3127 info.m_state |= wxLIST_STATE_FOCUSED;
0b5efdc7
VZ
3128 }
3129 if ( lvItem.stateMask & LVIS_SELECTED)
3130 {
edccf428 3131 info.m_stateMask |= wxLIST_STATE_SELECTED;
0b5efdc7 3132 if ( lvItem.state & LVIS_SELECTED )
edccf428 3133 info.m_state |= wxLIST_STATE_SELECTED;
0b5efdc7 3134 }
acb62b84 3135 }
0b5efdc7
VZ
3136
3137 if ( lvItem.mask & LVIF_TEXT )
acb62b84 3138 {
0b5efdc7
VZ
3139 info.m_mask |= wxLIST_MASK_TEXT;
3140 info.m_text = lvItem.pszText;
acb62b84 3141 }
0b5efdc7 3142 if ( lvItem.mask & LVIF_IMAGE )
acb62b84 3143 {
0b5efdc7
VZ
3144 info.m_mask |= wxLIST_MASK_IMAGE;
3145 info.m_image = lvItem.iImage;
acb62b84 3146 }
0b5efdc7
VZ
3147 if ( lvItem.mask & LVIF_PARAM )
3148 info.m_mask |= wxLIST_MASK_DATA;
3149 if ( lvItem.mask & LVIF_DI_SETITEM )
3150 info.m_mask |= wxLIST_SET_ITEM;
3151 info.m_col = lvItem.iSubItem;
3152
3153 if (needText)
acb62b84 3154 {
0b5efdc7
VZ
3155 if (lvItem.pszText)
3156 delete[] lvItem.pszText;
acb62b84 3157 }
edccf428 3158 lvItem.mask = oldMask;
2bda0e17
KB
3159}
3160
8dff2b5c
VZ
3161static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
3162{
3163 if (stateMask & wxLIST_STATE_CUT)
3164 {
3165 lvItem.stateMask |= LVIS_CUT;
3166 if (state & wxLIST_STATE_CUT)
3167 lvItem.state |= LVIS_CUT;
3168 }
3169 if (stateMask & wxLIST_STATE_DROPHILITED)
3170 {
3171 lvItem.stateMask |= LVIS_DROPHILITED;
3172 if (state & wxLIST_STATE_DROPHILITED)
3173 lvItem.state |= LVIS_DROPHILITED;
3174 }
3175 if (stateMask & wxLIST_STATE_FOCUSED)
3176 {
3177 lvItem.stateMask |= LVIS_FOCUSED;
3178 if (state & wxLIST_STATE_FOCUSED)
3179 lvItem.state |= LVIS_FOCUSED;
3180 }
3181 if (stateMask & wxLIST_STATE_SELECTED)
3182 {
3183 lvItem.stateMask |= LVIS_SELECTED;
3184 if (state & wxLIST_STATE_SELECTED)
3185 lvItem.state |= LVIS_SELECTED;
3186 }
3187}
3188
3189static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
3190 const wxListItem& info,
3191 LV_ITEM& lvItem)
2bda0e17 3192{
dc6a272b
VZ
3193 if ( ctrl->InReportView() )
3194 {
3195 wxASSERT_MSG( 0 <= info.m_col && info.m_col < ctrl->GetColumnCount(),
3196 "wxListCtrl column index out of bounds" );
3197 }
3198 else // not in report view
3199 {
3200 wxASSERT_MSG( info.m_col == 0, "columns only exist in report view" );
3201 }
14263426 3202
edccf428 3203 lvItem.iItem = (int) info.m_itemId;
acb62b84 3204
edccf428 3205 lvItem.iImage = info.m_image;
0b5efdc7
VZ
3206 lvItem.stateMask = 0;
3207 lvItem.state = 0;
3208 lvItem.mask = 0;
3209 lvItem.iSubItem = info.m_col;
acb62b84 3210
0b5efdc7 3211 if (info.m_mask & wxLIST_MASK_STATE)
acb62b84 3212 {
edccf428 3213 lvItem.mask |= LVIF_STATE;
8dff2b5c
VZ
3214
3215 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
acb62b84 3216 }
acb62b84 3217
0b5efdc7 3218 if (info.m_mask & wxLIST_MASK_TEXT)
acb62b84 3219 {
edccf428 3220 lvItem.mask |= LVIF_TEXT;
b5d43d1d 3221 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
0b5efdc7
VZ
3222 {
3223 lvItem.pszText = LPSTR_TEXTCALLBACK;
3224 }
3225 else
3226 {
e421922f 3227 // pszText is not const, hence the cast
c9f78968 3228 lvItem.pszText = (wxChar *)info.m_text.wx_str();
0b5efdc7 3229 if ( lvItem.pszText )
8ecd5de2 3230 lvItem.cchTextMax = info.m_text.length();
0b5efdc7
VZ
3231 else
3232 lvItem.cchTextMax = 0;
3233 }
acb62b84 3234 }
0b5efdc7 3235 if (info.m_mask & wxLIST_MASK_IMAGE)
edccf428 3236 lvItem.mask |= LVIF_IMAGE;
2bda0e17
KB
3237}
3238
e64658ed
VZ
3239static void wxConvertToMSWListCol(HWND hwndList,
3240 int col,
3241 const wxListItem& item,
6f806543
VZ
3242 LV_COLUMN& lvCol)
3243{
3244 wxZeroMemory(lvCol);
3245
3246 if ( item.m_mask & wxLIST_MASK_TEXT )
3247 {
3248 lvCol.mask |= LVCF_TEXT;
c9f78968 3249 lvCol.pszText = (wxChar *)item.m_text.wx_str(); // cast is safe
6f806543
VZ
3250 }
3251
3252 if ( item.m_mask & wxLIST_MASK_FORMAT )
3253 {
3254 lvCol.mask |= LVCF_FMT;
3255
3256 if ( item.m_format == wxLIST_FORMAT_LEFT )
3257 lvCol.fmt = LVCFMT_LEFT;
3258 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
3259 lvCol.fmt = LVCFMT_RIGHT;
3260 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
3261 lvCol.fmt = LVCFMT_CENTER;
3262 }
3263
3264 if ( item.m_mask & wxLIST_MASK_WIDTH )
3265 {
3266 lvCol.mask |= LVCF_WIDTH;
3267 if ( item.m_width == wxLIST_AUTOSIZE)
3268 lvCol.cx = LVSCW_AUTOSIZE;
3269 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3270 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
3271 else
3272 lvCol.cx = item.m_width;
3273 }
3274
5b59df83
VZ
3275 // see comment at the end of wxListCtrl::GetColumn()
3276#ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
6f806543
VZ
3277 if ( item.m_mask & wxLIST_MASK_IMAGE )
3278 {
5625d0d4 3279 if ( wxApp::GetComCtl32Version() >= 470 )
6f806543 3280 {
e64658ed 3281 lvCol.mask |= LVCF_IMAGE;
8b3a4016 3282
e64658ed 3283 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
8b3a4016
VZ
3284 // seem to be generally nicer than on the left and the generic
3285 // version only draws them on the right (we don't have a flag to
3286 // specify the image location anyhow)
3287 //
3288 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3289 // make any difference in my tests -- but maybe we should?
73bb5654 3290 if ( item.m_image != -1 )
e64658ed
VZ
3291 {
3292 // as we're going to overwrite the format field, get its
3293 // current value first -- unless we want to overwrite it anyhow
3294 if ( !(lvCol.mask & LVCF_FMT) )
3295 {
3296 LV_COLUMN lvColOld;
3297 wxZeroMemory(lvColOld);
3298 lvColOld.mask = LVCF_FMT;
3299 if ( ListView_GetColumn(hwndList, col, &lvColOld) )
3300 {
3301 lvCol.fmt = lvColOld.fmt;
3302 }
3303
3304 lvCol.mask |= LVCF_FMT;
3305 }
3306
73bb5654 3307 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
e64658ed 3308 }
8b3a4016 3309
6f806543
VZ
3310 lvCol.iImage = item.m_image;
3311 }
3312 //else: it doesn't support item images anyhow
3313 }
5b59df83 3314#endif // _WIN32_IE >= 0x0300
6f806543
VZ
3315}
3316
1e6feb95 3317#endif // wxUSE_LISTCTRL