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