]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listctrl.cpp
Check for NULL before dereferencing
[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
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
acb62b84 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
0b5efdc7 21 #pragma implementation "listctrl.h"
3c1a88d8 22 #pragma implementation "listctrlbase.h"
2bda0e17
KB
23#endif
24
25// For compilers that support precompilation, includes "wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
0b5efdc7 29 #pragma hdrstop
2bda0e17
KB
30#endif
31
98ec9dbe 32#if wxUSE_LISTCTRL && defined(__WIN95__)
9f303149 33
2bda0e17 34#ifndef WX_PRECOMP
9f303149 35 #include "wx/app.h"
9f303149
VZ
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/settings.h"
2bda0e17
KB
39#endif
40
de8e98f1
JS
41#include "wx/textctrl.h"
42#include "wx/imaglist.h"
2bda0e17 43#include "wx/listctrl.h"
68fdcf29 44#include "wx/dcclient.h"
2bda0e17
KB
45
46#include "wx/msw/private.h"
47
ae090fdb 48#if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
edccf428
VZ
49 #include "wx/msw/gnuwin32/extra.h"
50#else
0b5efdc7 51 #include <commctrl.h>
2bda0e17
KB
52#endif
53
7391216e 54#include "wx/msw/missing.h"
12979259 55
edccf428
VZ
56// ----------------------------------------------------------------------------
57// private functions
58// ----------------------------------------------------------------------------
2bda0e17 59
8dff2b5c
VZ
60// convert our state and mask flags to LV_ITEM constants
61static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem);
62
63// convert wxListItem to LV_ITEM
64static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
65 const wxListItem& info, LV_ITEM& lvItem);
66
67// convert LV_ITEM to wxListItem
68static void wxConvertFromMSWListItem(HWND hwndListCtrl,
69 wxListItem& info,
70 /* const */ LV_ITEM& lvItem);
2bda0e17 71
6f806543
VZ
72// convert our wxListItem to LV_COLUMN
73static void wxConvertToMSWListCol(int col, const wxListItem& item,
74 LV_COLUMN& lvCol);
75
8c21905b
VS
76// ----------------------------------------------------------------------------
77// private helper classes
78// ----------------------------------------------------------------------------
79
80// We have to handle both fooW and fooA notifications in several cases
81// because of broken commctl.dll and/or unicows.dll. This class is used to
73d8c5fd 82// convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
8c21905b
VS
83// LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
84// by wxConvertToMSWListItem().
85class wxLV_ITEM
86{
87public:
88 ~wxLV_ITEM() { delete m_buf; }
89 operator LV_ITEM&() const { return *m_item; }
90
91#if wxUSE_UNICODE
92 wxLV_ITEM(LV_ITEMW &item) : m_buf(NULL), m_item(&item) {}
93 wxLV_ITEM(LV_ITEMA &item)
94 {
95 m_item = new LV_ITEM((LV_ITEM&)item);
96 if ( (item.mask & LVIF_TEXT) && item.pszText )
97 {
98 m_buf = new wxMB2WXbuf(wxConvLocal.cMB2WX(item.pszText));
99 m_item->pszText = (wxChar*)m_buf->data();
100 }
101 else
102 m_buf = NULL;
73d8c5fd 103 }
8c21905b
VS
104private:
105 wxMB2WXbuf *m_buf;
106
107#else
108 wxLV_ITEM(LV_ITEMW &item)
109 {
110 m_item = new LV_ITEM((LV_ITEM&)item);
111 if ( (item.mask & LVIF_TEXT) && item.pszText )
112 {
113 m_buf = new wxWC2WXbuf(wxConvLocal.cWC2WX(item.pszText));
114 m_item->pszText = (wxChar*)m_buf->data();
115 }
116 else
117 m_buf = NULL;
73d8c5fd 118 }
8c21905b
VS
119 wxLV_ITEM(LV_ITEMA &item) : m_buf(NULL), m_item(&item) {}
120private:
121 wxWC2WXbuf *m_buf;
122#endif
123
124 LV_ITEM *m_item;
125};
126
7cf46fdb
VZ
127///////////////////////////////////////////////////////
128// Problem:
73d8c5fd
RD
129// The MSW version had problems with SetTextColour() et
130// al as the wxListItemAttr's were stored keyed on the
131// item index. If a item was inserted anywhere but the end
132// of the list the the text attributes (colour etc) for
7cf46fdb 133// the following items were out of sync.
73d8c5fd 134//
7cf46fdb 135// Solution:
73d8c5fd
RD
136// Under MSW the only way to associate data with a List
137// item independant of its position in the list is to
138// store a pointer to it in its lParam attribute. However
139// user programs are already using this (via the
7cf46fdb 140// SetItemData() GetItemData() calls).
73d8c5fd
RD
141//
142// However what we can do is store a pointer to a
143// structure which contains the attributes we want *and*
7cf46fdb 144// a lParam for the users data, e.g.
73d8c5fd 145//
7cf46fdb
VZ
146// class wxListItemInternalData
147// {
148// public:
73d8c5fd 149// wxListItemAttr *attr;
7cf46fdb
VZ
150// long lParam; // user data
151// };
73d8c5fd
RD
152//
153// To conserve memory, a wxListItemInternalData is
154// only allocated for a LV_ITEM if text attributes or
7cf46fdb
VZ
155// user data(lparam) are being set.
156
157
158// class wxListItemInternalData
159class wxListItemInternalData
160{
161public:
73d8c5fd 162 wxListItemAttr *attr;
7cf46fdb
VZ
163 LPARAM lParam; // user data
164
165 wxListItemInternalData() : attr(NULL), lParam(0) {}
166 ~wxListItemInternalData()
167 {
168 if (attr)
169 delete attr;
170 };
171};
172
173// Get the internal data structure
6149de71
RD
174static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId);
175static wxListItemInternalData *wxGetInternalData(wxListCtrl *ctl, long itemId);
176static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId);
177static void wxDeleteInternalData(wxListCtrl* ctl, long itemId);
7cf46fdb
VZ
178
179
edccf428 180// ----------------------------------------------------------------------------
2e4df4bf 181// events
edccf428
VZ
182// ----------------------------------------------------------------------------
183
2e4df4bf
VZ
184DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG)
185DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG)
186DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT)
187DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT)
188DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM)
189DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS)
190DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO)
191DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO)
192DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED)
193DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED)
194DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN)
195DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM)
196DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK)
11358d39
VZ
197DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK)
198DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG)
199DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING)
200DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG)
2e4df4bf
VZ
201DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)
202DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK)
203DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)
0ddefeb0 204DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED)
614391dc 205DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT)
2e4df4bf 206
8f177c8e 207IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
bf9b6266 208IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
8f177c8e 209IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
2bda0e17 210
2d33aec9
VZ
211IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
212
2702ed5a
JS
213BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
214 EVT_PAINT(wxListCtrl::OnPaint)
215END_EVENT_TABLE()
216
edccf428
VZ
217// ============================================================================
218// implementation
219// ============================================================================
220
221// ----------------------------------------------------------------------------
222// wxListCtrl construction
223// ----------------------------------------------------------------------------
224
bdc72a22 225void wxListCtrl::Init()
2bda0e17 226{
0b5efdc7
VZ
227 m_imageListNormal = NULL;
228 m_imageListSmall = NULL;
229 m_imageListState = NULL;
2e12c11a 230 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE;
0b5efdc7 231 m_baseStyle = 0;
2bda0e17 232 m_colCount = 0;
bbcdf8bc 233 m_textCtrl = NULL;
7cf46fdb 234 m_AnyInternalData = FALSE;
bdc72a22 235 m_hasAnyAttr = FALSE;
2bda0e17
KB
236}
237
0b5efdc7
VZ
238bool wxListCtrl::Create(wxWindow *parent,
239 wxWindowID id,
240 const wxPoint& pos,
241 const wxSize& size,
242 long style,
243 const wxValidator& validator,
244 const wxString& name)
2bda0e17 245{
11b6a93b 246#if wxUSE_VALIDATORS
0b5efdc7 247 SetValidator(validator);
11b6a93b
VZ
248#endif // wxUSE_VALIDATORS
249
0b5efdc7 250 SetName(name);
2bda0e17 251
0b5efdc7
VZ
252 int x = pos.x;
253 int y = pos.y;
254 int width = size.x;
255 int height = size.y;
2bda0e17 256
0b5efdc7 257 m_windowStyle = style;
2bda0e17 258
0b5efdc7 259 SetParent(parent);
2bda0e17 260
0b5efdc7
VZ
261 if (width <= 0)
262 width = 100;
263 if (height <= 0)
264 height = 30;
265 if (x < 0)
266 x = 0;
267 if (y < 0)
268 y = 0;
2bda0e17 269
0b5efdc7 270 m_windowId = (id == -1) ? NewControlId() : id;
2bda0e17 271
edccf428
VZ
272 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
273 LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
b0766406
JS
274
275 if ( m_windowStyle & wxCLIP_SIBLINGS )
276 wstyle |= WS_CLIPSIBLINGS;
277
edccf428
VZ
278 if ( wxStyleHasBorder(m_windowStyle) )
279 wstyle |= WS_BORDER;
280 m_baseStyle = wstyle;
281
282 if ( !DoCreateControl(x, y, width, height) )
283 return FALSE;
284
285 if (parent)
286 parent->AddChild(this);
287
288 return TRUE;
289}
290
291bool wxListCtrl::DoCreateControl(int x, int y, int w, int h)
292{
293 DWORD wstyle = m_baseStyle;
2bda0e17 294
0b5efdc7 295 bool want3D;
edccf428 296 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 297
0b5efdc7
VZ
298 // Even with extended styles, need to combine with WS_BORDER
299 // for them to look right.
edccf428 300 if ( want3D )
0b5efdc7 301 wstyle |= WS_BORDER;
2bda0e17 302
0b5efdc7
VZ
303 long oldStyle = 0; // Dummy
304 wstyle |= ConvertToMSWStyle(oldStyle, m_windowStyle);
2bda0e17 305
0b5efdc7
VZ
306 // Create the ListView control.
307 m_hWnd = (WXHWND)CreateWindowEx(exStyle,
308 WC_LISTVIEW,
223d09f6 309 wxT(""),
0b5efdc7 310 wstyle,
edccf428
VZ
311 x, y, w, h,
312 GetWinHwnd(GetParent()),
0b5efdc7
VZ
313 (HMENU)m_windowId,
314 wxGetInstance(),
315 NULL);
f8352807 316
edccf428
VZ
317 if ( !m_hWnd )
318 {
f6bcfd97 319 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
0b5efdc7
VZ
320
321 return FALSE;
322 }
323
324 // for comctl32.dll v 4.70+ we want to have this attribute because it's
325 // prettier (and also because wxGTK does it like this)
225fe9d6 326 if ( (wstyle & LVS_REPORT) && wxTheApp->GetComCtl32Version() >= 470 )
0b5efdc7 327 {
225fe9d6
VZ
328 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
329 0, LVS_EX_FULLROWSELECT);
0b5efdc7 330 }
f8352807 331
a756f210 332 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
edccf428 333 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17 334
edccf428 335 SubclassWin(m_hWnd);
2bda0e17 336
0b5efdc7 337 return TRUE;
2bda0e17
KB
338}
339
edccf428
VZ
340void wxListCtrl::UpdateStyle()
341{
342 if ( GetHWND() )
343 {
344 // The new window view style
345 long dummy;
346 DWORD dwStyleNew = ConvertToMSWStyle(dummy, m_windowStyle);
347 dwStyleNew |= m_baseStyle;
348
910484a6 349 // Get the current window style.
edccf428
VZ
350 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
351
910484a6 352 // Only set the window style if the view bits have changed.
edccf428
VZ
353 if ( dwStyleOld != dwStyleNew )
354 {
355 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
356 }
357 }
358}
359
7cf46fdb 360void wxListCtrl::FreeAllInternalData()
2bda0e17 361{
7cf46fdb 362 if (m_AnyInternalData)
73d8c5fd 363 {
7cf46fdb
VZ
364 int n = GetItemCount();
365 int i = 0;
6932a32c 366
7cf46fdb 367 for (i = 0; i < n; i++)
6149de71
RD
368 wxDeleteInternalData(this, i);
369
73d8c5fd
RD
370 m_AnyInternalData = FALSE;
371 }
6932a32c 372}
d62228a6 373
6932a32c
VZ
374wxListCtrl::~wxListCtrl()
375{
7cf46fdb 376 FreeAllInternalData();
91b4c08d 377
d62228a6 378 if ( m_textCtrl )
bbcdf8bc 379 {
0b5efdc7 380 m_textCtrl->SetHWND(0);
7bf1474a 381 m_textCtrl->UnsubclassWin();
0b5efdc7
VZ
382 delete m_textCtrl;
383 m_textCtrl = NULL;
bbcdf8bc 384 }
33ac7e6f 385
2e12c11a
VS
386 if (m_ownsImageListNormal) delete m_imageListNormal;
387 if (m_ownsImageListSmall) delete m_imageListSmall;
388 if (m_ownsImageListState) delete m_imageListState;
2bda0e17
KB
389}
390
edccf428
VZ
391// ----------------------------------------------------------------------------
392// set/get/change style
393// ----------------------------------------------------------------------------
394
2bda0e17 395// Add or remove a single window style
debe6624 396void wxListCtrl::SetSingleStyle(long style, bool add)
2bda0e17 397{
0b5efdc7 398 long flag = GetWindowStyleFlag();
2bda0e17 399
0b5efdc7 400 // Get rid of conflicting styles
acb62b84
VZ
401 if ( add )
402 {
0b5efdc7 403 if ( style & wxLC_MASK_TYPE)
edccf428 404 flag = flag & ~wxLC_MASK_TYPE;
0b5efdc7 405 if ( style & wxLC_MASK_ALIGN )
edccf428 406 flag = flag & ~wxLC_MASK_ALIGN;
0b5efdc7 407 if ( style & wxLC_MASK_SORT )
edccf428 408 flag = flag & ~wxLC_MASK_SORT;
acb62b84 409 }
2bda0e17 410
0b5efdc7
VZ
411 if ( flag & style )
412 {
413 if ( !add )
414 flag -= style;
415 }
416 else
417 {
418 if ( add )
419 {
420 flag |= style;
421 }
422 }
423
424 m_windowStyle = flag;
2bda0e17 425
edccf428 426 UpdateStyle();
2bda0e17
KB
427}
428
429// Set the whole window style
debe6624 430void wxListCtrl::SetWindowStyleFlag(long flag)
2bda0e17 431{
0b5efdc7 432 m_windowStyle = flag;
2bda0e17 433
edccf428 434 UpdateStyle();
2bda0e17
KB
435}
436
0b5efdc7
VZ
437// Can be just a single style, or a bitlist
438long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const
2bda0e17 439{
0b5efdc7
VZ
440 long wstyle = 0;
441 if ( style & wxLC_ICON )
442 {
443 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
444 oldStyle -= LVS_SMALLICON;
445 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
446 oldStyle -= LVS_REPORT;
447 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
448 oldStyle -= LVS_LIST;
449 wstyle |= LVS_ICON;
450 }
451
452 if ( style & wxLC_SMALL_ICON )
453 {
454 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
455 oldStyle -= LVS_ICON;
456 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
457 oldStyle -= LVS_REPORT;
458 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
459 oldStyle -= LVS_LIST;
460 wstyle |= LVS_SMALLICON;
461 }
462
463 if ( style & wxLC_LIST )
464 {
465 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
466 oldStyle -= LVS_ICON;
467 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
468 oldStyle -= LVS_REPORT;
469 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
470 oldStyle -= LVS_SMALLICON;
471 wstyle |= LVS_LIST;
472 }
2bda0e17 473
0b5efdc7
VZ
474 if ( style & wxLC_REPORT )
475 {
476 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
477 oldStyle -= LVS_ICON;
478 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
479 oldStyle -= LVS_LIST;
480 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
481 oldStyle -= LVS_SMALLICON;
482
483 wstyle |= LVS_REPORT;
484 }
2bda0e17 485
0b5efdc7
VZ
486 if ( style & wxLC_ALIGN_LEFT )
487 {
488 if ( oldStyle & LVS_ALIGNTOP )
489 oldStyle -= LVS_ALIGNTOP;
490 wstyle |= LVS_ALIGNLEFT;
491 }
2bda0e17 492
0b5efdc7
VZ
493 if ( style & wxLC_ALIGN_TOP )
494 {
495 if ( oldStyle & LVS_ALIGNLEFT )
496 oldStyle -= LVS_ALIGNLEFT;
497 wstyle |= LVS_ALIGNTOP;
498 }
2bda0e17 499
0b5efdc7
VZ
500 if ( style & wxLC_AUTOARRANGE )
501 wstyle |= LVS_AUTOARRANGE;
2bda0e17 502
0b5efdc7
VZ
503 if ( style & wxLC_NO_SORT_HEADER )
504 wstyle |= LVS_NOSORTHEADER;
2bda0e17 505
0b5efdc7
VZ
506 if ( style & wxLC_NO_HEADER )
507 wstyle |= LVS_NOCOLUMNHEADER;
508
509 if ( style & wxLC_EDIT_LABELS )
510 wstyle |= LVS_EDITLABELS;
511
512 if ( style & wxLC_SINGLE_SEL )
513 wstyle |= LVS_SINGLESEL;
514
515 if ( style & wxLC_SORT_ASCENDING )
516 {
517 if ( oldStyle & LVS_SORTDESCENDING )
518 oldStyle -= LVS_SORTDESCENDING;
519 wstyle |= LVS_SORTASCENDING;
520 }
521
522 if ( style & wxLC_SORT_DESCENDING )
523 {
524 if ( oldStyle & LVS_SORTASCENDING )
525 oldStyle -= LVS_SORTASCENDING;
526 wstyle |= LVS_SORTDESCENDING;
527 }
528
bf9b6266 529#if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
98ec9dbe
VZ
530 if ( style & wxLC_VIRTUAL )
531 {
30a72e62
VZ
532 int ver = wxTheApp->GetComCtl32Version();
533 if ( ver < 470 )
534 {
bf9b6266 535 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."),
30a72e62
VZ
536 ver / 100, ver % 100);
537 }
538
98ec9dbe
VZ
539 wstyle |= LVS_OWNERDATA;
540 }
bf9b6266 541#endif
98ec9dbe 542
0b5efdc7 543 return wstyle;
2bda0e17
KB
544}
545
edccf428
VZ
546// ----------------------------------------------------------------------------
547// accessors
548// ----------------------------------------------------------------------------
549
91b4c08d
VZ
550// Sets the foreground, i.e. text, colour
551bool wxListCtrl::SetForegroundColour(const wxColour& col)
552{
553 if ( !wxWindow::SetForegroundColour(col) )
554 return FALSE;
555
556 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
557
558 return TRUE;
559}
560
561// Sets the background colour
cc2b7472 562bool wxListCtrl::SetBackgroundColour(const wxColour& col)
2bda0e17 563{
cc2b7472
VZ
564 if ( !wxWindow::SetBackgroundColour(col) )
565 return FALSE;
2bda0e17 566
91b4c08d
VZ
567 // we set the same colour for both the "empty" background and the items
568 // background
569 COLORREF color = wxColourToRGB(col);
570 ListView_SetBkColor(GetHwnd(), color);
571 ListView_SetTextBkColor(GetHwnd(), color);
cc2b7472
VZ
572
573 return TRUE;
2bda0e17
KB
574}
575
576// Gets information about this column
debe6624 577bool wxListCtrl::GetColumn(int col, wxListItem& item) const
2bda0e17 578{
0b5efdc7 579 LV_COLUMN lvCol;
6f806543 580 wxZeroMemory(lvCol);
0b5efdc7 581
37094564
RD
582 lvCol.mask = LVCF_WIDTH;
583
0b5efdc7
VZ
584 if ( item.m_mask & wxLIST_MASK_TEXT )
585 {
586 lvCol.mask |= LVCF_TEXT;
837e5743 587 lvCol.pszText = new wxChar[513];
0b5efdc7
VZ
588 lvCol.cchTextMax = 512;
589 }
590
37094564
RD
591 if ( item.m_mask & wxLIST_MASK_FORMAT )
592 {
593 lvCol.mask |= LVCF_FMT;
594 }
595
596 if ( item.m_mask & wxLIST_MASK_IMAGE )
597 {
598 lvCol.mask |= LVCF_IMAGE;
599 }
600
6f806543 601 bool success = ListView_GetColumn(GetHwnd(), col, & lvCol) != 0;
0b5efdc7
VZ
602
603 // item.m_subItem = lvCol.iSubItem;
604 item.m_width = lvCol.cx;
605
606 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
607 {
608 item.m_text = lvCol.pszText;
609 delete[] lvCol.pszText;
610 }
611
612 if ( item.m_mask & wxLIST_MASK_FORMAT )
613 {
614 if (lvCol.fmt == LVCFMT_LEFT)
615 item.m_format = wxLIST_FORMAT_LEFT;
616 else if (lvCol.fmt == LVCFMT_RIGHT)
617 item.m_format = wxLIST_FORMAT_RIGHT;
618 else if (lvCol.fmt == LVCFMT_CENTER)
619 item.m_format = wxLIST_FORMAT_CENTRE;
2bda0e17
KB
620 }
621
8ac67aa1 622#if _WIN32_IE >= 0x0300
37094564
RD
623 if ( item.m_mask & wxLIST_MASK_IMAGE )
624 {
625 item.m_image = lvCol.iImage;
626 }
8ac67aa1 627#endif
37094564 628
0b5efdc7 629 return success;
2bda0e17
KB
630}
631
632// Sets information about this column
debe6624 633bool wxListCtrl::SetColumn(int col, wxListItem& item)
2bda0e17 634{
0b5efdc7 635 LV_COLUMN lvCol;
6f806543 636 wxConvertToMSWListCol(col, item, lvCol);
0b5efdc7 637
6f806543 638 return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
2bda0e17
KB
639}
640
641// Gets the column width
debe6624 642int wxListCtrl::GetColumnWidth(int col) const
2bda0e17 643{
edccf428 644 return ListView_GetColumnWidth(GetHwnd(), col);
2bda0e17
KB
645}
646
647// Sets the column width
debe6624 648bool wxListCtrl::SetColumnWidth(int col, int width)
2bda0e17 649{
0b5efdc7
VZ
650 int col2 = col;
651 if ( m_windowStyle & wxLC_LIST )
652 col2 = -1;
2bda0e17 653
0b5efdc7
VZ
654 int width2 = width;
655 if ( width2 == wxLIST_AUTOSIZE)
656 width2 = LVSCW_AUTOSIZE;
657 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
658 width2 = LVSCW_AUTOSIZE_USEHEADER;
2bda0e17 659
6f806543 660 return ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0;
2bda0e17
KB
661}
662
663// Gets the number of items that can fit vertically in the
664// visible area of the list control (list or report view)
665// or the total number of items in the list control (icon
666// or small icon view)
c42404a5 667int wxListCtrl::GetCountPerPage() const
2bda0e17 668{
edccf428 669 return ListView_GetCountPerPage(GetHwnd());
2bda0e17
KB
670}
671
672// Gets the edit control for editing labels.
c42404a5 673wxTextCtrl* wxListCtrl::GetEditControl() const
2bda0e17 674{
bbcdf8bc 675 return m_textCtrl;
2bda0e17
KB
676}
677
678// Gets information about the item
679bool wxListCtrl::GetItem(wxListItem& info) const
680{
0b5efdc7 681 LV_ITEM lvItem;
11c7d5b6 682 wxZeroMemory(lvItem);
2bda0e17 683
0b5efdc7 684 lvItem.iItem = info.m_itemId;
fc5a93cd 685 lvItem.iSubItem = info.m_col;
0b5efdc7
VZ
686
687 if ( info.m_mask & wxLIST_MASK_TEXT )
688 {
689 lvItem.mask |= LVIF_TEXT;
837e5743 690 lvItem.pszText = new wxChar[513];
0b5efdc7
VZ
691 lvItem.cchTextMax = 512;
692 }
693 else
694 {
695 lvItem.pszText = NULL;
696 }
697
698 if (info.m_mask & wxLIST_MASK_DATA)
edccf428 699 lvItem.mask |= LVIF_PARAM;
0b5efdc7 700
2189c644
JS
701 if (info.m_mask & wxLIST_MASK_IMAGE)
702 lvItem.mask |= LVIF_IMAGE;
703
0b5efdc7
VZ
704 if ( info.m_mask & wxLIST_MASK_STATE )
705 {
706 lvItem.mask |= LVIF_STATE;
707 // the other bits are hardly interesting anyhow
708 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
709 }
710
711 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
712 if ( !success )
713 {
714 wxLogError(_("Couldn't retrieve information about list control item %d."),
715 lvItem.iItem);
716 }
717 else
718 {
dccde0c0
VZ
719 // give NULL as hwnd as we already have everything we need
720 wxConvertFromMSWListItem(NULL, info, lvItem);
0b5efdc7
VZ
721 }
722
723 if (lvItem.pszText)
724 delete[] lvItem.pszText;
725
726 return success;
2bda0e17
KB
727}
728
729// Sets information about the item
730bool wxListCtrl::SetItem(wxListItem& info)
731{
0b5efdc7 732 LV_ITEM item;
2bda0e17 733 wxConvertToMSWListItem(this, info, item);
bdc72a22 734
7cf46fdb
VZ
735 // we never update the lParam if it contains our pointer
736 // to the wxListItemInternalData structure
737 item.mask &= ~LVIF_PARAM;
738
739 // check if setting attributes or lParam
740 if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA))
741 {
742 // get internal item data
743 // perhaps a cache here ?
6149de71 744 wxListItemInternalData *data = wxGetInternalData(this, info.m_itemId);
73d8c5fd 745
7cf46fdb
VZ
746 if (! data)
747 {
748 // need to set it
749 m_AnyInternalData = TRUE;
750 data = new wxListItemInternalData();
751 item.lParam = (LPARAM) data;
752 item.mask |= LVIF_PARAM;
753 };
754
755
756 // user data
757 if (info.m_mask & wxLIST_MASK_DATA)
758 data->lParam = info.m_data;
759
760 // attributes
761 if (info.HasAttributes())
762 {
763 if (data->attr)
764 *data->attr = *info.GetAttributes();
765 else
766 data->attr = new wxListItemAttr(*info.GetAttributes());
767 };
768 };
769
770
2d33aec9
VZ
771 // we could be changing only the attribute in which case we don't need to
772 // call ListView_SetItem() at all
773 if ( item.mask )
bdc72a22 774 {
2d33aec9
VZ
775 item.cchTextMax = 0;
776 if ( !ListView_SetItem(GetHwnd(), &item) )
777 {
778 wxLogDebug(_T("ListView_SetItem() failed"));
2702ed5a 779
2d33aec9
VZ
780 return FALSE;
781 }
233d0295 782 }
2702ed5a 783
233d0295
VZ
784 // we need to update the item immediately to show the new image
785 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
2702ed5a 786
233d0295
VZ
787 // check whether it has any custom attributes
788 if ( info.HasAttributes() )
789 {
bdc72a22 790 m_hasAnyAttr = TRUE;
233d0295
VZ
791
792 // if the colour has changed, we must redraw the item
793 updateNow = TRUE;
bdc72a22 794 }
bdc72a22 795
233d0295 796 if ( updateNow )
7cc98b3e 797 {
233d0295 798 // we need this to make the change visible right now
2d33aec9 799 RefreshItem(item.iItem);
7cc98b3e
VZ
800 }
801
233d0295 802 return TRUE;
2bda0e17
KB
803}
804
debe6624 805long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
2bda0e17 806{
0b5efdc7
VZ
807 wxListItem info;
808 info.m_text = label;
809 info.m_mask = wxLIST_MASK_TEXT;
810 info.m_itemId = index;
811 info.m_col = col;
812 if ( imageId > -1 )
813 {
814 info.m_image = imageId;
815 info.m_mask |= wxLIST_MASK_IMAGE;
816 }
817 return SetItem(info);
2bda0e17
KB
818}
819
820
821// Gets the item state
debe6624 822int wxListCtrl::GetItemState(long item, long stateMask) const
2bda0e17 823{
0b5efdc7 824 wxListItem info;
2bda0e17 825
edccf428 826 info.m_mask = wxLIST_MASK_STATE;
0b5efdc7
VZ
827 info.m_stateMask = stateMask;
828 info.m_itemId = item;
2bda0e17 829
0b5efdc7
VZ
830 if (!GetItem(info))
831 return 0;
2bda0e17 832
0b5efdc7 833 return info.m_state;
2bda0e17
KB
834}
835
836// Sets the item state
debe6624 837bool wxListCtrl::SetItemState(long item, long state, long stateMask)
2bda0e17 838{
8dff2b5c
VZ
839 // NB: don't use SetItem() here as it doesn't work with the virtual list
840 // controls
841 LV_ITEM lvItem;
842 wxZeroMemory(lvItem);
2bda0e17 843
8dff2b5c 844 wxConvertToMSWFlags(state, stateMask, lvItem);
2bda0e17 845
2d33aec9 846 // for the virtual list controls we need to refresh the previously focused
ad9f477d
VZ
847 // item manually when changing focus without changing selection
848 // programmatically because otherwise it keeps its focus rectangle until
849 // next repaint (yet another comctl32 bug)
2d33aec9
VZ
850 long focusOld;
851 if ( IsVirtual() &&
852 (stateMask & wxLIST_STATE_FOCUSED) &&
853 (state & wxLIST_STATE_FOCUSED) )
854 {
855 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
856 }
857 else
858 {
859 focusOld = -1;
860 }
861
8dff2b5c
VZ
862 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
863 (WPARAM)item, (LPARAM)&lvItem) )
864 {
865 wxLogLastError(_T("ListView_SetItemState"));
866
867 return FALSE;
868 }
869
2d33aec9
VZ
870 if ( focusOld != -1 )
871 {
ad9f477d
VZ
872 // no need to refresh the item if it was previously selected, it would
873 // only result in annoying flicker
874 if ( !(GetItemState(focusOld,
875 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
876 {
877 RefreshItem(focusOld);
878 }
2d33aec9
VZ
879 }
880
8dff2b5c 881 return TRUE;
2bda0e17
KB
882}
883
884// Sets the item image
33ac7e6f 885bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
2bda0e17 886{
0b5efdc7 887 wxListItem info;
2bda0e17 888
edccf428 889 info.m_mask = wxLIST_MASK_IMAGE;
0b5efdc7
VZ
890 info.m_image = image;
891 info.m_itemId = item;
2bda0e17 892
0b5efdc7 893 return SetItem(info);
2bda0e17
KB
894}
895
896// Gets the item text
debe6624 897wxString wxListCtrl::GetItemText(long item) const
2bda0e17 898{
0b5efdc7 899 wxListItem info;
2bda0e17 900
edccf428 901 info.m_mask = wxLIST_MASK_TEXT;
0b5efdc7 902 info.m_itemId = item;
2bda0e17 903
0b5efdc7
VZ
904 if (!GetItem(info))
905 return wxString("");
906 return info.m_text;
2bda0e17
KB
907}
908
909// Sets the item text
debe6624 910void wxListCtrl::SetItemText(long item, const wxString& str)
2bda0e17 911{
0b5efdc7 912 wxListItem info;
2bda0e17 913
edccf428 914 info.m_mask = wxLIST_MASK_TEXT;
0b5efdc7
VZ
915 info.m_itemId = item;
916 info.m_text = str;
2bda0e17 917
0b5efdc7 918 SetItem(info);
2bda0e17
KB
919}
920
921// Gets the item data
debe6624 922long wxListCtrl::GetItemData(long item) const
2bda0e17 923{
0b5efdc7 924 wxListItem info;
2bda0e17 925
edccf428 926 info.m_mask = wxLIST_MASK_DATA;
0b5efdc7 927 info.m_itemId = item;
2bda0e17 928
0b5efdc7
VZ
929 if (!GetItem(info))
930 return 0;
931 return info.m_data;
2bda0e17
KB
932}
933
934// Sets the item data
debe6624 935bool wxListCtrl::SetItemData(long item, long data)
2bda0e17 936{
0b5efdc7 937 wxListItem info;
2bda0e17 938
edccf428 939 info.m_mask = wxLIST_MASK_DATA;
0b5efdc7
VZ
940 info.m_itemId = item;
941 info.m_data = data;
2bda0e17 942
0b5efdc7 943 return SetItem(info);
2bda0e17
KB
944}
945
946// Gets the item rectangle
16e93305 947bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
2bda0e17 948{
2d33aec9 949 RECT rectWin;
2bda0e17 950
2d33aec9 951 int codeWin;
0b5efdc7 952 if ( code == wxLIST_RECT_BOUNDS )
2d33aec9 953 codeWin = LVIR_BOUNDS;
0b5efdc7 954 else if ( code == wxLIST_RECT_ICON )
2d33aec9 955 codeWin = LVIR_ICON;
0b5efdc7 956 else if ( code == wxLIST_RECT_LABEL )
2d33aec9
VZ
957 codeWin = LVIR_LABEL;
958 else
959 {
960 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
961
962 codeWin = LVIR_BOUNDS;
963 }
2bda0e17 964
5ea105e0 965#ifdef __WXWINE__
2d33aec9 966 bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin ) != 0;
5ea105e0 967#else
2d33aec9 968 bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
5ea105e0 969#endif
2bda0e17 970
2d33aec9
VZ
971 rect.x = rectWin.left;
972 rect.y = rectWin.top;
973 rect.width = rectWin.right - rectWin.left;
974 rect.height = rectWin.bottom - rectWin.top;
975
0b5efdc7 976 return success;
2bda0e17
KB
977}
978
979// Gets the item position
debe6624 980bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
2bda0e17 981{
0b5efdc7 982 POINT pt;
2bda0e17 983
edccf428 984 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
2bda0e17 985
0b5efdc7
VZ
986 pos.x = pt.x; pos.y = pt.y;
987 return success;
2bda0e17
KB
988}
989
990// Sets the item position.
debe6624 991bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
2bda0e17 992{
edccf428 993 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
2bda0e17
KB
994}
995
996// Gets the number of items in the list control
c42404a5 997int wxListCtrl::GetItemCount() const
2bda0e17 998{
edccf428 999 return ListView_GetItemCount(GetHwnd());
2bda0e17
KB
1000}
1001
1002// Retrieves the spacing between icons in pixels.
1003// If small is TRUE, gets the spacing for the small icon
1004// view, otherwise the large icon view.
1005int wxListCtrl::GetItemSpacing(bool isSmall) const
1006{
edccf428 1007 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
2bda0e17
KB
1008}
1009
5b98eb2f
RL
1010void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1011{
1012 wxListItem info;
1013 info.m_itemId = item;
1014 info.SetTextColour( col );
1015 SetItem( info );
1016}
1017
1018wxColour wxListCtrl::GetItemTextColour( long item ) const
1019{
1020 wxListItem info;
1021 info.m_itemId = item;
1022 GetItem( info );
1023 return info.GetTextColour();
1024}
1025
1026void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1027{
1028 wxListItem info;
1029 info.m_itemId = item;
1030 info.SetBackgroundColour( col );
1031 SetItem( info );
1032}
1033
1034wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1035{
1036 wxListItem info;
1037 info.m_itemId = item;
1038 GetItem( info );
1039 return info.GetBackgroundColour();
1040}
1041
2bda0e17 1042// Gets the number of selected items in the list control
c42404a5 1043int wxListCtrl::GetSelectedItemCount() const
2bda0e17 1044{
edccf428 1045 return ListView_GetSelectedCount(GetHwnd());
2bda0e17
KB
1046}
1047
1048// Gets the text colour of the listview
c42404a5 1049wxColour wxListCtrl::GetTextColour() const
2bda0e17 1050{
edccf428 1051 COLORREF ref = ListView_GetTextColor(GetHwnd());
0b5efdc7
VZ
1052 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1053 return col;
2bda0e17
KB
1054}
1055
1056// Sets the text colour of the listview
1057void wxListCtrl::SetTextColour(const wxColour& col)
1058{
910484a6 1059 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
2bda0e17
KB
1060}
1061
1062// Gets the index of the topmost visible item when in
1063// list or report view
c42404a5 1064long wxListCtrl::GetTopItem() const
2bda0e17 1065{
edccf428 1066 return (long) ListView_GetTopIndex(GetHwnd());
2bda0e17
KB
1067}
1068
1069// Searches for an item, starting from 'item'.
1070// 'geometry' is one of
1071// wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1072// 'state' is a state bit flag, one or more of
1073// wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1074// item can be -1 to find the first item that matches the
1075// specified flags.
1076// Returns the item or -1 if unsuccessful.
debe6624 1077long wxListCtrl::GetNextItem(long item, int geom, int state) const
2bda0e17 1078{
0b5efdc7 1079 long flags = 0;
2bda0e17 1080
0b5efdc7
VZ
1081 if ( geom == wxLIST_NEXT_ABOVE )
1082 flags |= LVNI_ABOVE;
1083 if ( geom == wxLIST_NEXT_ALL )
1084 flags |= LVNI_ALL;
1085 if ( geom == wxLIST_NEXT_BELOW )
1086 flags |= LVNI_BELOW;
1087 if ( geom == wxLIST_NEXT_LEFT )
1088 flags |= LVNI_TOLEFT;
1089 if ( geom == wxLIST_NEXT_RIGHT )
1090 flags |= LVNI_TORIGHT;
2bda0e17 1091
0b5efdc7
VZ
1092 if ( state & wxLIST_STATE_CUT )
1093 flags |= LVNI_CUT;
1094 if ( state & wxLIST_STATE_DROPHILITED )
1095 flags |= LVNI_DROPHILITED;
1096 if ( state & wxLIST_STATE_FOCUSED )
1097 flags |= LVNI_FOCUSED;
1098 if ( state & wxLIST_STATE_SELECTED )
1099 flags |= LVNI_SELECTED;
2bda0e17 1100
edccf428 1101 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
2bda0e17
KB
1102}
1103
1104
debe6624 1105wxImageList *wxListCtrl::GetImageList(int which) const
2bda0e17 1106{
0b5efdc7 1107 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 1108 {
0b5efdc7
VZ
1109 return m_imageListNormal;
1110 }
1111 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 1112 {
0b5efdc7
VZ
1113 return m_imageListSmall;
1114 }
1115 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 1116 {
0b5efdc7
VZ
1117 return m_imageListState;
1118 }
1119 return NULL;
2bda0e17
KB
1120}
1121
debe6624 1122void wxListCtrl::SetImageList(wxImageList *imageList, int which)
2bda0e17 1123{
0b5efdc7
VZ
1124 int flags = 0;
1125 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 1126 {
0b5efdc7 1127 flags = LVSIL_NORMAL;
2e12c11a 1128 if (m_ownsImageListNormal) delete m_imageListNormal;
0b5efdc7 1129 m_imageListNormal = imageList;
2e12c11a 1130 m_ownsImageListNormal = FALSE;
0b5efdc7
VZ
1131 }
1132 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 1133 {
0b5efdc7 1134 flags = LVSIL_SMALL;
2e12c11a 1135 if (m_ownsImageListSmall) delete m_imageListSmall;
0b5efdc7 1136 m_imageListSmall = imageList;
2e12c11a 1137 m_ownsImageListSmall = FALSE;
0b5efdc7
VZ
1138 }
1139 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 1140 {
0b5efdc7 1141 flags = LVSIL_STATE;
2e12c11a 1142 if (m_ownsImageListState) delete m_imageListState;
0b5efdc7 1143 m_imageListState = imageList;
2e12c11a 1144 m_ownsImageListState = FALSE;
0b5efdc7 1145 }
edccf428 1146 ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
2bda0e17
KB
1147}
1148
2e12c11a
VS
1149void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1150{
1151 SetImageList(imageList, which);
1152 if ( which == wxIMAGE_LIST_NORMAL )
1153 m_ownsImageListNormal = TRUE;
1154 else if ( which == wxIMAGE_LIST_SMALL )
1155 m_ownsImageListSmall = TRUE;
1156 else if ( which == wxIMAGE_LIST_STATE )
1157 m_ownsImageListState = TRUE;
1158}
1159
edccf428 1160// ----------------------------------------------------------------------------
2bda0e17 1161// Operations
edccf428 1162// ----------------------------------------------------------------------------
2bda0e17
KB
1163
1164// Arranges the items
debe6624 1165bool wxListCtrl::Arrange(int flag)
2bda0e17 1166{
0b5efdc7
VZ
1167 UINT code = 0;
1168 if ( flag == wxLIST_ALIGN_LEFT )
1169 code = LVA_ALIGNLEFT;
1170 else if ( flag == wxLIST_ALIGN_TOP )
1171 code = LVA_ALIGNTOP;
1172 else if ( flag == wxLIST_ALIGN_DEFAULT )
1173 code = LVA_DEFAULT;
1174 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1175 code = LVA_SNAPTOGRID;
2bda0e17 1176
edccf428 1177 return (ListView_Arrange(GetHwnd(), code) != 0);
2bda0e17
KB
1178}
1179
1180// Deletes an item
debe6624 1181bool wxListCtrl::DeleteItem(long item)
2bda0e17 1182{
2e9f62da
VZ
1183 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1184 {
1185 wxLogLastError(_T("ListView_DeleteItem"));
1186 return FALSE;
1187 }
1188
1189 // the virtual list control doesn't refresh itself correctly, help it
1190 if ( IsVirtual() )
1191 {
1192 // we need to refresh all the lines below the one which was deleted
1193 wxRect rectItem;
1194 if ( item > 0 && GetItemCount() )
1195 {
1196 GetItemRect(item - 1, rectItem);
1197 }
1198 else
1199 {
1200 rectItem.y =
1201 rectItem.height = 0;
1202 }
1203
1204 wxRect rectWin = GetRect();
1205 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1206 rectWin.y = rectItem.GetBottom();
1207
1208 RefreshRect(rectWin);
1209 }
1210
1211 return TRUE;
2bda0e17
KB
1212}
1213
1214// Deletes all items
0b5efdc7 1215bool wxListCtrl::DeleteAllItems()
2bda0e17 1216{
2e9f62da 1217 return ListView_DeleteAllItems(GetHwnd()) != 0;
2bda0e17
KB
1218}
1219
1220// Deletes all items
0b5efdc7 1221bool wxListCtrl::DeleteAllColumns()
2bda0e17 1222{
edccf428 1223 while ( m_colCount > 0 )
2bda0e17 1224 {
edccf428
VZ
1225 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1226 {
f6bcfd97 1227 wxLogLastError(wxT("ListView_DeleteColumn"));
edccf428
VZ
1228
1229 return FALSE;
1230 }
1231
1232 m_colCount--;
2bda0e17 1233 }
edccf428 1234
223d09f6 1235 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
edccf428
VZ
1236
1237 return TRUE;
2bda0e17
KB
1238}
1239
1240// Deletes a column
debe6624 1241bool wxListCtrl::DeleteColumn(int col)
2bda0e17 1242{
edccf428 1243 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
2bda0e17
KB
1244
1245 if ( success && (m_colCount > 0) )
1246 m_colCount --;
1247 return success;
1248}
1249
1250// Clears items, and columns if there are any.
0b5efdc7 1251void wxListCtrl::ClearAll()
2bda0e17
KB
1252{
1253 DeleteAllItems();
1254 if ( m_colCount > 0 )
1255 DeleteAllColumns();
1256}
1257
bbcdf8bc
JS
1258wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1259{
1260 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1261
6aaef140 1262 // ListView_EditLabel requires that the list has focus.
aa50e893 1263 SetFocus();
6aaef140 1264 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
bbcdf8bc
JS
1265
1266 if (m_textCtrl)
1267 {
0b5efdc7 1268 m_textCtrl->SetHWND(0);
7bf1474a 1269 m_textCtrl->UnsubclassWin();
0b5efdc7 1270 delete m_textCtrl;
bbcdf8bc
JS
1271 }
1272
1273 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
6aaef140
VZ
1274 m_textCtrl->SetHWND(hWnd);
1275 m_textCtrl->SubclassWin(hWnd);
1276 m_textCtrl->SetParent(this);
bbcdf8bc
JS
1277
1278 return m_textCtrl;
1279}
1280
1281// End label editing, optionally cancelling the edit
33ac7e6f 1282bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel))
2bda0e17 1283{
7bf1474a
VZ
1284 wxFAIL_MSG( _T("not implemented") );
1285
0b5efdc7 1286 return FALSE;
2bda0e17
KB
1287}
1288
1289// Ensures this item is visible
debe6624 1290bool wxListCtrl::EnsureVisible(long item)
2bda0e17 1291{
7bf1474a 1292 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0;
2bda0e17
KB
1293}
1294
1295// Find an item whose label matches this string, starting from the item after 'start'
1296// or the beginning if 'start' is -1.
debe6624 1297long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
2bda0e17 1298{
0b5efdc7 1299 LV_FINDINFO findInfo;
2bda0e17 1300
0b5efdc7
VZ
1301 findInfo.flags = LVFI_STRING;
1302 if ( partial )
7edc258e 1303 findInfo.flags |= LVFI_PARTIAL;
3ca6a5f0 1304 findInfo.psz = str;
2bda0e17 1305
3ca6a5f0
BP
1306 // ListView_FindItem() excludes the first item from search and to look
1307 // through all the items you need to start from -1 which is unnatural and
8036af01
JS
1308 // inconsistent with the generic version - so we adjust the index
1309 if (start != -1)
1310 start --;
1311 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
2bda0e17
KB
1312}
1313
1314// Find an item whose data matches this data, starting from the item after 'start'
1315// or the beginning if 'start' is -1.
72db8894
RD
1316// NOTE : Lindsay Mathieson - 14-July-2002
1317// No longer use ListView_FindItem as the data attribute is now stored
1318// in a wxListItemInternalData structure refernced by the actual lParam
debe6624 1319long wxListCtrl::FindItem(long start, long data)
2bda0e17 1320{
72db8894
RD
1321 long idx = start + 1;
1322 long count = GetItemCount();
2bda0e17 1323
72db8894
RD
1324 while (idx < count)
1325 {
1326 if (GetItemData(idx) == data)
1327 return idx;
1328 idx++;
1329 };
2bda0e17 1330
72db8894 1331 return -1;
2bda0e17
KB
1332}
1333
1334// Find an item nearest this position in the specified direction, starting from
1335// the item after 'start' or the beginning if 'start' is -1.
debe6624 1336long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
2bda0e17 1337{
0b5efdc7 1338 LV_FINDINFO findInfo;
2bda0e17 1339
0b5efdc7
VZ
1340 findInfo.flags = LVFI_NEARESTXY;
1341 findInfo.pt.x = pt.x;
1342 findInfo.pt.y = pt.y;
acb62b84 1343 findInfo.vkDirection = VK_RIGHT;
2bda0e17 1344
0b5efdc7
VZ
1345 if ( direction == wxLIST_FIND_UP )
1346 findInfo.vkDirection = VK_UP;
1347 else if ( direction == wxLIST_FIND_DOWN )
1348 findInfo.vkDirection = VK_DOWN;
1349 else if ( direction == wxLIST_FIND_LEFT )
1350 findInfo.vkDirection = VK_LEFT;
1351 else if ( direction == wxLIST_FIND_RIGHT )
1352 findInfo.vkDirection = VK_RIGHT;
1353
edccf428 1354 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
2bda0e17
KB
1355}
1356
1357// Determines which item (if any) is at the specified point,
1358// giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1359long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1360{
1361 LV_HITTESTINFO hitTestInfo;
0b5efdc7
VZ
1362 hitTestInfo.pt.x = (int) point.x;
1363 hitTestInfo.pt.y = (int) point.y;
2bda0e17 1364
edccf428 1365 ListView_HitTest(GetHwnd(), & hitTestInfo);
2bda0e17 1366
0b5efdc7
VZ
1367 flags = 0;
1368 if ( hitTestInfo.flags & LVHT_ABOVE )
1369 flags |= wxLIST_HITTEST_ABOVE;
1370 if ( hitTestInfo.flags & LVHT_BELOW )
1371 flags |= wxLIST_HITTEST_BELOW;
1372 if ( hitTestInfo.flags & LVHT_NOWHERE )
1373 flags |= wxLIST_HITTEST_NOWHERE;
1374 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1375 flags |= wxLIST_HITTEST_ONITEMICON;
1376 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1377 flags |= wxLIST_HITTEST_ONITEMLABEL;
1378 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1379 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1380 if ( hitTestInfo.flags & LVHT_TOLEFT )
1381 flags |= wxLIST_HITTEST_TOLEFT;
1382 if ( hitTestInfo.flags & LVHT_TORIGHT )
1383 flags |= wxLIST_HITTEST_TORIGHT;
1384
edccf428 1385 return (long) hitTestInfo.iItem;
2bda0e17
KB
1386}
1387
1388// Inserts an item, returning the index of the new item if successful,
1389// -1 otherwise.
2bda0e17
KB
1390long wxListCtrl::InsertItem(wxListItem& info)
1391{
98ec9dbe
VZ
1392 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1393
0b5efdc7
VZ
1394 LV_ITEM item;
1395 wxConvertToMSWListItem(this, info, item);
7cf46fdb 1396 item.mask &= ~LVIF_PARAM;
2bda0e17 1397
7cf46fdb
VZ
1398 // check wether we need to allocate our internal data
1399 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1400 if (needInternalData)
bdc72a22 1401 {
7cf46fdb
VZ
1402 m_AnyInternalData = TRUE;
1403 item.mask |= LVIF_PARAM;
2702ed5a 1404
7cf46fdb
VZ
1405 // internal stucture that manages data
1406 wxListItemInternalData *data = new wxListItemInternalData();
1407 item.lParam = (LPARAM) data;
2702ed5a 1408
7cf46fdb
VZ
1409 if (info.m_mask & wxLIST_MASK_DATA)
1410 data->lParam = info.m_data;
2702ed5a 1411
7cf46fdb
VZ
1412 // check whether it has any custom attributes
1413 if ( info.HasAttributes() )
1414 {
1415 // take copy of attributes
1416 data->attr = new wxListItemAttr(*info.GetAttributes());
bdc72a22 1417 }
7cf46fdb
VZ
1418 };
1419
bdc72a22 1420
edccf428 1421 return (long) ListView_InsertItem(GetHwnd(), & item);
2bda0e17
KB
1422}
1423
debe6624 1424long wxListCtrl::InsertItem(long index, const wxString& label)
2bda0e17 1425{
0b5efdc7
VZ
1426 wxListItem info;
1427 info.m_text = label;
1428 info.m_mask = wxLIST_MASK_TEXT;
1429 info.m_itemId = index;
1430 return InsertItem(info);
2bda0e17
KB
1431}
1432
1433// Inserts an image item
debe6624 1434long wxListCtrl::InsertItem(long index, int imageIndex)
2bda0e17 1435{
0b5efdc7
VZ
1436 wxListItem info;
1437 info.m_image = imageIndex;
1438 info.m_mask = wxLIST_MASK_IMAGE;
1439 info.m_itemId = index;
1440 return InsertItem(info);
2bda0e17
KB
1441}
1442
1443// Inserts an image/string item
debe6624 1444long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
2bda0e17 1445{
0b5efdc7
VZ
1446 wxListItem info;
1447 info.m_image = imageIndex;
1448 info.m_text = label;
1449 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1450 info.m_itemId = index;
1451 return InsertItem(info);
2bda0e17
KB
1452}
1453
1454// For list view mode (only), inserts a column.
debe6624 1455long wxListCtrl::InsertColumn(long col, wxListItem& item)
2bda0e17 1456{
0b5efdc7 1457 LV_COLUMN lvCol;
6f806543 1458 wxConvertToMSWListCol(col, item, lvCol);
0b5efdc7 1459
6f806543 1460 if ( !(lvCol.mask & LVCF_WIDTH) )
e373f51b
VZ
1461 {
1462 // always give some width to the new column: this one is compatible
6f806543
VZ
1463 // with the generic version
1464 lvCol.mask |= LVCF_WIDTH;
e373f51b 1465 lvCol.cx = 80;
0b5efdc7 1466 }
e373f51b 1467
6f806543
VZ
1468 // when we insert a column which can contain an image, we must specify this
1469 // flag right now as doing it later in SetColumn() has no effect
1470 //
1471 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1472 // way to dynamically set/clear the bitmap as the column without a bitmap
1473 // on the left looks ugly (there is a hole)
1474 //
1475 // unfortunately with my version of comctl32.dll (5.80), the left column
1476 // image is always on the left and it seems that it's a "feature" - I
1477 // didn't find any way to work around it in any case
1478 if ( lvCol.mask & LVCF_IMAGE )
1479 {
1480 lvCol.mask |= LVCF_FMT;
1481 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT;
1482 }
2bda0e17 1483
6f806543 1484 bool success = ListView_InsertColumn(GetHwnd(), col, &lvCol) != -1;
2bda0e17 1485 if ( success )
e373f51b
VZ
1486 {
1487 m_colCount++;
1488 }
1489 else
1490 {
223d09f6 1491 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
e373f51b
VZ
1492 lvCol.pszText);
1493 }
1494
2bda0e17
KB
1495 return success;
1496}
1497
bdc72a22
VZ
1498long wxListCtrl::InsertColumn(long col,
1499 const wxString& heading,
1500 int format,
1501 int width)
2bda0e17 1502{
0b5efdc7
VZ
1503 wxListItem item;
1504 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1505 item.m_text = heading;
1506 if ( width > -1 )
1507 {
1508 item.m_mask |= wxLIST_MASK_WIDTH;
1509 item.m_width = width;
1510 }
1511 item.m_format = format;
2bda0e17 1512
0b5efdc7 1513 return InsertColumn(col, item);
2bda0e17
KB
1514}
1515
1516// Scrolls the list control. If in icon, small icon or report view mode,
1517// x specifies the number of pixels to scroll. If in list view mode, x
1518// specifies the number of columns to scroll.
1519// If in icon, small icon or list view mode, y specifies the number of pixels
1520// to scroll. If in report view mode, y specifies the number of lines to scroll.
debe6624 1521bool wxListCtrl::ScrollList(int dx, int dy)
2bda0e17 1522{
edccf428 1523 return (ListView_Scroll(GetHwnd(), dx, dy) != 0);
2bda0e17
KB
1524}
1525
1526// Sort items.
1527
1528// fn is a function which takes 3 long arguments: item1, item2, data.
1529// item1 is the long data associated with a first item (NOT the index).
1530// item2 is the long data associated with a second item (NOT the index).
1531// data is the same value as passed to SortItems.
1532// The return value is a negative number if the first item should precede the second
1533// item, a positive number of the second item should precede the first,
1534// or zero if the two items are equivalent.
1535
1536// data is arbitrary data to be passed to the sort function.
19230604 1537
7cf46fdb
VZ
1538// Internal structures for proxying the user compare function
1539// so that we can pass it the *real* user data
19230604 1540
7cf46fdb
VZ
1541// translate lParam data and call user func
1542struct wxInternalDataSort
19230604 1543{
7cf46fdb
VZ
1544 wxListCtrlCompare user_fn;
1545 long data;
1546};
19230604 1547
7cf46fdb 1548int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
2bda0e17 1549{
7cf46fdb 1550 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
19230604 1551
7cf46fdb
VZ
1552 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1553 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
19230604 1554
7cf46fdb
VZ
1555 long d1 = (data1 == NULL ? 0 : data1->lParam);
1556 long d2 = (data2 == NULL ? 0 : data2->lParam);
19230604 1557
7cf46fdb 1558 return internalData->user_fn(d1, d2, internalData->data);
19230604 1559
7cf46fdb 1560};
19230604 1561
7cf46fdb
VZ
1562bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1563{
1564 struct wxInternalDataSort internalData;
1565 internalData.user_fn = fn;
1566 internalData.data = data;
19230604 1567
14090241
VZ
1568 // WPARAM cast is needed for mingw/cygwin
1569 if ( !ListView_SortItems(GetHwnd(),
1570 wxInternalDataCompareFunc,
1571 (WPARAM) &internalData) )
19230604
RD
1572 {
1573 wxLogDebug(_T("ListView_SortItems() failed"));
1574
1575 return FALSE;
1576 }
1577
1578 return TRUE;
2bda0e17
KB
1579}
1580
19230604
RD
1581
1582
edccf428
VZ
1583// ----------------------------------------------------------------------------
1584// message processing
1585// ----------------------------------------------------------------------------
1586
debe6624 1587bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 1588{
0b5efdc7 1589 if (cmd == EN_UPDATE)
acb62b84 1590 {
0b5efdc7
VZ
1591 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1592 event.SetEventObject( this );
1593 ProcessCommand(event);
1594 return TRUE;
acb62b84 1595 }
0b5efdc7 1596 else if (cmd == EN_KILLFOCUS)
acb62b84 1597 {
0b5efdc7
VZ
1598 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1599 event.SetEventObject( this );
1600 ProcessCommand(event);
1601 return TRUE;
acb62b84 1602 }
edccf428
VZ
1603 else
1604 return FALSE;
0b5efdc7
VZ
1605}
1606
a23fd0e1 1607bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
0b5efdc7 1608{
bdc72a22
VZ
1609 // prepare the event
1610 // -----------------
1611
0b5efdc7 1612 wxListEvent event(wxEVT_NULL, m_windowId);
648bec3e
VZ
1613 event.SetEventObject(this);
1614
0b5efdc7 1615 wxEventType eventType = wxEVT_NULL;
225fe9d6 1616
bdc72a22 1617 NMHDR *nmhdr = (NMHDR *)lParam;
225fe9d6 1618
d1f2eb1d
VZ
1619 // if your compiler is as broken as this, you should really change it: this
1620 // code is needed for normal operation! #ifdef below is only useful for
1621 // automatic rebuilds which are done with a very old compiler version
0cf5b099 1622#ifdef HDN_BEGINTRACKA
d1f2eb1d 1623
11358d39
VZ
1624 // check for messages from the header (in report view)
1625 HWND hwndHdr = ListView_GetHeader(GetHwnd());
225fe9d6 1626
11358d39
VZ
1627 // is it a message from the header?
1628 if ( nmhdr->hwndFrom == hwndHdr )
acb62b84 1629 {
00811ff9 1630 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
0cf5b099 1631
11358d39 1632 event.m_itemIndex = -1;
cb0f56f9 1633
11358d39
VZ
1634 switch ( nmhdr->code )
1635 {
1636 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1637 // TRACK messages even to ANSI programs: on my system I get
1638 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1639 //
1640 // work around is to simply catch both versions and hope that it
1641 // works (why should this message exist in ANSI and Unicode is
1642 // beyond me as it doesn't deal with strings at all...)
1643 case HDN_BEGINTRACKA:
1644 case HDN_BEGINTRACKW:
1645 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1646 // fall through
1647
1648 case HDN_TRACKA:
1649 case HDN_TRACKW:
1650 if ( eventType == wxEVT_NULL )
1651 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1652 // fall through
1653
1654 case HDN_ENDTRACKA:
1655 case HDN_ENDTRACKW:
1656 if ( eventType == wxEVT_NULL )
1657 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1658 event.m_col = nmHDR->iItem;
1659 break;
1660
1661 case NM_RCLICK:
1662 {
1663 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1664 event.m_col = -1;
0b5efdc7 1665
11358d39
VZ
1666 // find the column clicked: we have to search for it
1667 // ourselves as the notification message doesn't provide
1668 // this info
0b5efdc7 1669
11358d39
VZ
1670 // where did the click occur?
1671 POINT ptClick;
1672 if ( !::GetCursorPos(&ptClick) )
1673 {
1674 wxLogLastError(_T("GetCursorPos"));
1675 }
0b5efdc7 1676
11358d39
VZ
1677 if ( !::ScreenToClient(hwndHdr, &ptClick) )
1678 {
1679 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1680 }
5ea47806 1681
11358d39
VZ
1682 event.m_pointDrag.x = ptClick.x;
1683 event.m_pointDrag.y = ptClick.y;
5ea47806 1684
11358d39 1685 int colCount = Header_GetItemCount(hwndHdr);
bdc72a22 1686
11358d39
VZ
1687 RECT rect;
1688 for ( int col = 0; col < colCount; col++ )
1689 {
1690 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1691 {
1692 if ( ::PtInRect(&rect, ptClick) )
1693 {
1694 event.m_col = col;
1695 break;
1696 }
1697 }
1698 }
1699 }
1700 break;
5ea47806 1701
11358d39
VZ
1702 default:
1703 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1704 }
1705 }
d1f2eb1d 1706 else
0cf5b099 1707#endif // defined(HDN_BEGINTRACKA)
d1f2eb1d 1708 if ( nmhdr->hwndFrom == GetHwnd() )
11358d39
VZ
1709 {
1710 // almost all messages use NM_LISTVIEW
1711 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
bdc72a22 1712
11358d39
VZ
1713 // this is true for almost all events
1714 event.m_item.m_data = nmLV->lParam;
bdc72a22 1715
11358d39
VZ
1716 switch ( nmhdr->code )
1717 {
1718 case LVN_BEGINRDRAG:
1719 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1720 // fall through
7bf1474a 1721
11358d39
VZ
1722 case LVN_BEGINDRAG:
1723 if ( eventType == wxEVT_NULL )
1724 {
1725 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1726 }
bdc72a22 1727
11358d39
VZ
1728 event.m_itemIndex = nmLV->iItem;
1729 event.m_pointDrag.x = nmLV->ptAction.x;
1730 event.m_pointDrag.y = nmLV->ptAction.y;
1731 break;
1732
8c21905b
VS
1733 // NB: we have to handle both *A and *W versions here because some
1734 // versions of comctl32.dll send ANSI message to an Unicode app
1735 case LVN_BEGINLABELEDITA:
11358d39
VZ
1736 {
1737 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
8c21905b
VS
1738 wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item);
1739 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1740 event.m_itemIndex = event.m_item.m_itemId;
1741 }
1742 break;
1743 case LVN_BEGINLABELEDITW:
1744 {
1745 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1746 wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item);
1747 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1748 event.m_itemIndex = event.m_item.m_itemId;
1749 }
1750 break;
1751
1752 case LVN_ENDLABELEDITA:
1753 {
1754 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1755 wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item);
1756 wxConvertFromMSWListItem(NULL, event.m_item, item);
73d8c5fd 1757 if ( ((LV_ITEM)item).pszText == NULL ||
8c21905b
VS
1758 ((LV_ITEM)item).iItem == -1 )
1759 return FALSE;
1760
1761 event.m_itemIndex = event.m_item.m_itemId;
1762 }
1763 break;
1764 case LVN_ENDLABELEDITW:
1765 {
1766 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1767 wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item);
1768 wxConvertFromMSWListItem(NULL, event.m_item, item);
73d8c5fd 1769 if ( ((LV_ITEM)item).pszText == NULL ||
8c21905b
VS
1770 ((LV_ITEM)item).iItem == -1 )
1771 return FALSE;
1772
11358d39
VZ
1773 event.m_itemIndex = event.m_item.m_itemId;
1774 }
1775 break;
edccf428 1776
11358d39
VZ
1777 case LVN_COLUMNCLICK:
1778 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1779 event.m_itemIndex = -1;
1780 event.m_col = nmLV->iSubItem;
1781 break;
bdc72a22 1782
11358d39
VZ
1783 case LVN_DELETEALLITEMS:
1784 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1785 event.m_itemIndex = -1;
11358d39
VZ
1786 break;
1787
1788 case LVN_DELETEITEM:
1789 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
225fe9d6 1790 event.m_itemIndex = nmLV->iItem;
7cf46fdb 1791 // delete the assoicated internal data
6149de71 1792 wxDeleteInternalData(this, nmLV->iItem);
11358d39
VZ
1793 break;
1794
11358d39
VZ
1795 case LVN_SETDISPINFO:
1796 {
1797 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1798 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1799 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
1800 }
1801 break;
1802
1803 case LVN_INSERTITEM:
1804 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
225fe9d6 1805 event.m_itemIndex = nmLV->iItem;
11358d39 1806 break;
edccf428 1807
11358d39 1808 case LVN_ITEMCHANGED:
648bec3e
VZ
1809 // we translate this catch all message into more interesting
1810 // (and more easy to process) wxWindows events
1811
1812 // first of all, we deal with the state change events only
1813 if ( nmLV->uChanged & LVIF_STATE )
11358d39 1814 {
648bec3e
VZ
1815 // temp vars for readability
1816 const UINT stOld = nmLV->uOldState;
1817 const UINT stNew = nmLV->uNewState;
1818
1819 // has the focus changed?
1820 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
1821 {
1822 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
1823 event.m_itemIndex = nmLV->iItem;
1824 }
1825
1826 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
1827 {
1828 if ( eventType != wxEVT_NULL )
1829 {
1830 // focus and selection have both changed: send the
1831 // focus event from here and the selection one
1832 // below
1833 event.SetEventType(eventType);
1834 (void)GetEventHandler()->ProcessEvent(event);
1835 }
1836 else // no focus event to send
1837 {
1838 // then need to set m_itemIndex as it wasn't done
1839 // above
1840 event.m_itemIndex = nmLV->iItem;
1841 }
1842
1843 eventType = stNew & LVIS_SELECTED
1844 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1845 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1846 }
edccf428 1847 }
648bec3e
VZ
1848
1849 if ( eventType == wxEVT_NULL )
edccf428 1850 {
648bec3e 1851 // not an interesting event for us
11358d39 1852 return FALSE;
edccf428 1853 }
648bec3e 1854
11358d39 1855 break;
225fe9d6 1856
11358d39
VZ
1857 case LVN_KEYDOWN:
1858 {
1859 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1860 WORD wVKey = info->wVKey;
1861
1862 // get the current selection
1863 long lItem = GetNextItem(-1,
1864 wxLIST_NEXT_ALL,
1865 wxLIST_STATE_SELECTED);
1866
1867 // <Enter> or <Space> activate the selected item if any (but
1868 // not with Shift and/or Ctrl as then they have a predefined
1869 // meaning for the list view)
1870 if ( lItem != -1 &&
1871 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
1872 !(wxIsShiftDown() || wxIsCtrlDown()) )
1873 {
1874 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1875 }
1876 else
1877 {
1878 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
185d0094
VZ
1879
1880 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1881 // value which should be used as is
1882 int code = wxCharCodeMSWToWX(wVKey);
1883 event.m_code = code ? code : wVKey;
11358d39 1884 }
7db91489 1885
11358d39
VZ
1886 event.m_itemIndex =
1887 event.m_item.m_itemId = lItem;
1888
1889 if ( lItem != -1 )
1890 {
1891 // fill the other fields too
1892 event.m_item.m_text = GetItemText(lItem);
1893 event.m_item.m_data = GetItemData(lItem);
1894 }
1895 }
1896 break;
1897
1898 case NM_DBLCLK:
1899 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1900 // anything else
1901 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
f6bcfd97 1902 {
11358d39 1903 return TRUE;
f6bcfd97 1904 }
edccf428 1905
11358d39
VZ
1906 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1907 // if it happened on an item (and not on empty place)
1908 if ( nmLV->iItem == -1 )
1909 {
1910 // not on item
1911 return FALSE;
1912 }
edccf428 1913
11358d39
VZ
1914 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1915 event.m_itemIndex = nmLV->iItem;
1916 event.m_item.m_text = GetItemText(nmLV->iItem);
1917 event.m_item.m_data = GetItemData(nmLV->iItem);
1918 break;
225fe9d6 1919
11358d39 1920 case NM_RCLICK:
225fe9d6
VZ
1921 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1922 // don't do anything else
1923 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1924 {
1925 return TRUE;
1926 }
cb0f56f9 1927
225fe9d6
VZ
1928 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1929 LV_HITTESTINFO lvhti;
1930 wxZeroMemory(lvhti);
11c7d5b6 1931
225fe9d6
VZ
1932 ::GetCursorPos(&(lvhti.pt));
1933 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
1934 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
cb0f56f9 1935 {
225fe9d6
VZ
1936 if ( lvhti.flags & LVHT_ONITEM )
1937 {
1938 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
1939 event.m_itemIndex = lvhti.iItem;
a1f79c1e
VZ
1940 event.m_pointDrag.x = lvhti.pt.x;
1941 event.m_pointDrag.y = lvhti.pt.y;
225fe9d6 1942 }
cb0f56f9 1943 }
11358d39 1944 break;
bdc72a22 1945
bf9b6266 1946#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
11358d39
VZ
1947 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1948 case NM_CUSTOMDRAW:
1949 *result = OnCustomDraw(lParam);
63166611 1950
11358d39 1951 return TRUE;
6ecfe2ac 1952#endif // _WIN32_IE >= 0x300
0b5efdc7 1953
11358d39
VZ
1954 case LVN_ODCACHEHINT:
1955 {
1956 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
614391dc 1957
11358d39 1958 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
e623926d 1959
11358d39
VZ
1960 // we get some really stupid cache hints like ones for items in
1961 // range 0..0 for an empty control or, after deleting an item,
1962 // for items in invalid range - filter this garbage out
1963 if ( cacheHint->iFrom < cacheHint->iTo )
1964 {
1965 event.m_oldItemIndex = cacheHint->iFrom;
e623926d 1966
11358d39
VZ
1967 long iMax = GetItemCount();
1968 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
1969 : iMax - 1;
1970 }
1971 else
1972 {
1973 return FALSE;
1974 }
e623926d 1975 }
11358d39 1976 break;
614391dc 1977
11358d39
VZ
1978 case LVN_GETDISPINFO:
1979 if ( IsVirtual() )
1980 {
1981 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
98ec9dbe 1982
11358d39
VZ
1983 LV_ITEM& lvi = info->item;
1984 long item = lvi.iItem;
98ec9dbe 1985
11358d39
VZ
1986 if ( lvi.mask & LVIF_TEXT )
1987 {
1988 wxString text = OnGetItemText(item, lvi.iSubItem);
1989 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
1990 }
98ec9dbe 1991
244531bc 1992#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
5145e34f 1993 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
11358d39
VZ
1994 if ( lvi.mask & LVIF_IMAGE )
1995 {
1996 lvi.iImage = OnGetItemImage(item);
1997 }
244531bc 1998#endif
98ec9dbe 1999
11358d39
VZ
2000 // a little dose of healthy paranoia: as we never use
2001 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2002 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
2003 _T("we don't support state callbacks yet!") );
98ec9dbe 2004
11358d39
VZ
2005 return TRUE;
2006 }
2007 // fall through
98ec9dbe 2008
11358d39
VZ
2009 default:
2010 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2011 }
2012 }
2013 else
2014 {
2015 // where did this one come from?
2016 return FALSE;
acb62b84
VZ
2017 }
2018
bdc72a22
VZ
2019 // process the event
2020 // -----------------
2021
0b5efdc7 2022 event.SetEventType(eventType);
acb62b84 2023
0b5efdc7
VZ
2024 if ( !GetEventHandler()->ProcessEvent(event) )
2025 return FALSE;
acb62b84 2026
bdc72a22
VZ
2027 // post processing
2028 // ---------------
2029
225fe9d6 2030 switch ( nmhdr->code )
acb62b84 2031 {
6932a32c
VZ
2032 case LVN_DELETEALLITEMS:
2033 // always return TRUE to suppress all additional LVN_DELETEITEM
2034 // notifications - this makes deleting all items from a list ctrl
2035 // much faster
2036 *result = TRUE;
2037
2038 return TRUE;
2039
8c21905b
VS
2040 case LVN_ENDLABELEDITA:
2041 case LVN_ENDLABELEDITW:
614391dc
VZ
2042 // logic here is inversed compared to all the other messages
2043 *result = event.IsAllowed();
2044
2045 return TRUE;
acb62b84 2046 }
acb62b84 2047
0b5efdc7 2048 *result = !event.IsAllowed();
fd3f686c 2049
0b5efdc7 2050 return TRUE;
2bda0e17
KB
2051}
2052
63166611
VZ
2053#if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2054
2055WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2056{
2057 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
2058 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
2059 switch ( nmcd.dwDrawStage )
2060 {
2061 case CDDS_PREPAINT:
2062 // if we've got any items with non standard attributes,
2063 // notify us before painting each item
2064 //
2065 // for virtual controls, always suppose that we have attributes as
2066 // there is no way to check for this
2067 return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2068 : CDRF_DODEFAULT;
2069
2070 case CDDS_ITEMPREPAINT:
2071 {
2072 size_t item = (size_t)nmcd.dwItemSpec;
a7f560a2
VZ
2073 if ( item >= (size_t)GetItemCount() )
2074 {
2075 // we get this message with item == 0 for an empty control,
2076 // we must ignore it as calling OnGetItemAttr() would be
2077 // wrong
2078 return CDRF_DODEFAULT;
2079 }
2080
63166611
VZ
2081 wxListItemAttr *attr =
2082 IsVirtual() ? OnGetItemAttr(item)
6149de71 2083 : wxGetInternalDataAttr(this, item);
63166611
VZ
2084
2085 if ( !attr )
2086 {
2087 // nothing to do for this item
2088 return CDRF_DODEFAULT;
2089 }
2090
2091 HFONT hFont;
2092 wxColour colText, colBack;
2093 if ( attr->HasFont() )
2094 {
2095 wxFont font = attr->GetFont();
2096 hFont = (HFONT)font.GetResourceHandle();
2097 }
2098 else
2099 {
2100 hFont = 0;
2101 }
2102
2103 if ( attr->HasTextColour() )
2104 {
2105 colText = attr->GetTextColour();
2106 }
2107 else
2108 {
2109 colText = GetTextColour();
2110 }
2111
2112 if ( attr->HasBackgroundColour() )
2113 {
2114 colBack = attr->GetBackgroundColour();
2115 }
2116 else
2117 {
2118 colBack = GetBackgroundColour();
2119 }
2120
2121 lplvcd->clrText = wxColourToRGB(colText);
2122 lplvcd->clrTextBk = wxColourToRGB(colBack);
2123
2124 // note that if we wanted to set colours for
2125 // individual columns (subitems), we would have
2126 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2127 if ( hFont )
2128 {
2129 ::SelectObject(nmcd.hdc, hFont);
2130
2131 return CDRF_NEWFONT;
2132 }
2133 }
2134 // fall through to return CDRF_DODEFAULT
2135
2136 default:
2137 return CDRF_DODEFAULT;
2138 }
2139}
2140
2141#endif // NM_CUSTOMDRAW supported
2142
2702ed5a
JS
2143// Necessary for drawing hrules and vrules, if specified
2144void wxListCtrl::OnPaint(wxPaintEvent& event)
2145{
2146 wxPaintDC dc(this);
2147
2148 wxControl::OnPaint(event);
2149
2150 // Reset the device origin since it may have been set
2151 dc.SetDeviceOrigin(0, 0);
2152
2153 bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0);
2154 bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0);
2155
2156 if (!drawHRules && !drawVRules)
2157 return;
2158 if ((GetWindowStyle() & wxLC_REPORT) == 0)
2159 return;
2160
a756f210 2161 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2702ed5a
JS
2162 dc.SetPen(pen);
2163 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2164
2165 wxSize clientSize = GetClientSize();
2166 wxRect itemRect;
2167 int cy=0;
2168
2702ed5a
JS
2169 int itemCount = GetItemCount();
2170 int i;
625cb8c0 2171 if (drawHRules)
2702ed5a 2172 {
6443de02
RD
2173 long top = GetTopItem();
2174 for (i = top; i < top + GetCountPerPage() + 1; i++)
2702ed5a 2175 {
625cb8c0 2176 if (GetItemRect(i, itemRect))
ca286917 2177 {
625cb8c0
RD
2178 cy = itemRect.GetTop();
2179 if (i != 0) // Don't draw the first one
2180 {
2181 dc.DrawLine(0, cy, clientSize.x, cy);
2182 }
2183 // Draw last line
2cefcb34 2184 if (i == itemCount - 1)
625cb8c0
RD
2185 {
2186 cy = itemRect.GetBottom();
2187 dc.DrawLine(0, cy, clientSize.x, cy);
2188 }
2702ed5a
JS
2189 }
2190 }
2191 }
2cefcb34 2192 i = itemCount - 1;
2702ed5a
JS
2193 if (drawVRules && (i > -1))
2194 {
2195 wxRect firstItemRect;
2196 GetItemRect(0, firstItemRect);
2197
2198 if (GetItemRect(i, itemRect))
2199 {
2200 int col;
2201 int x = itemRect.GetX();
2202 for (col = 0; col < GetColumnCount(); col++)
2203 {
2204 int colWidth = GetColumnWidth(col);
2205 x += colWidth ;
2206 dc.DrawLine(x, firstItemRect.GetY() - 2, x, itemRect.GetBottom());
2207 }
2208 }
2209 }
2210}
2211
98ec9dbe
VZ
2212// ----------------------------------------------------------------------------
2213// virtual list controls
2214// ----------------------------------------------------------------------------
2215
d699f48b 2216wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
98ec9dbe
VZ
2217{
2218 // this is a pure virtual function, in fact - which is not really pure
2219 // because the controls which are not virtual don't need to implement it
2220 wxFAIL_MSG( _T("not supposed to be called") );
2221
2222 return wxEmptyString;
2223}
2224
d699f48b 2225int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
98ec9dbe
VZ
2226{
2227 // same as above
2228 wxFAIL_MSG( _T("not supposed to be called") );
2229
2230 return -1;
2231}
2232
d699f48b 2233wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
6c02c329 2234{
63166611 2235 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
6c02c329
VZ
2236 _T("invalid item index in OnGetItemAttr()") );
2237
2238 // no attributes by default
2239 return NULL;
2240}
2241
98ec9dbe
VZ
2242void wxListCtrl::SetItemCount(long count)
2243{
2244 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2245
2246 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, 0) )
2247 {
2248 wxLogLastError(_T("ListView_SetItemCount"));
2249 }
2250}
2251
a7f560a2
VZ
2252void wxListCtrl::RefreshItem(long item)
2253{
2d33aec9
VZ
2254 // strangely enough, ListView_Update() results in much more flicker here
2255 // than a dumb Refresh() -- why?
2256#if 0
a7f560a2
VZ
2257 if ( !ListView_Update(GetHwnd(), item) )
2258 {
2259 wxLogLastError(_T("ListView_Update"));
2260 }
2d33aec9
VZ
2261#else // 1
2262 wxRect rect;
2263 GetItemRect(item, rect);
2264 RefreshRect(rect);
2265#endif // 0/1
a7f560a2
VZ
2266}
2267
2268void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2269{
d0debf52
VZ
2270 wxRect rect1, rect2;
2271 GetItemRect(itemFrom, rect1);
2272 GetItemRect(itemTo, rect2);
2273
2274 wxRect rect = rect1;
2275 rect.height = rect2.GetBottom() - rect1.GetTop();
2276
2277 RefreshRect(rect);
a7f560a2
VZ
2278}
2279
6149de71 2280static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
7cf46fdb
VZ
2281{
2282 LV_ITEM it;
2283 it.mask = LVIF_PARAM;
2284 it.iItem = itemId;
2285
2286 bool success = ListView_GetItem(hwnd, &it) != 0;
2287 if (success)
2288 return (wxListItemInternalData *) it.lParam;
2289 else
2290 return NULL;
2291};
2292
6149de71 2293static wxListItemInternalData *wxGetInternalData(wxListCtrl *ctl, long itemId)
7cf46fdb 2294{
6149de71 2295 return wxGetInternalData((HWND) ctl->GetHWND(), itemId);
7cf46fdb
VZ
2296};
2297
6149de71 2298static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId)
7cf46fdb 2299{
6149de71 2300 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
7cf46fdb
VZ
2301 if (data)
2302 return data->attr;
2303 else
2304 return NULL;
2305};
2306
6149de71
RD
2307static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2308{
2309 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2310 if (data)
2311 {
2312 delete data;
2313 LV_ITEM item;
2314 memset(&item, 0, sizeof(item));
2315 item.iItem = itemId;
2316 item.mask = LVIF_PARAM;
2317 item.lParam = (LPARAM) 0;
2318 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2319 }
2320}
7cf46fdb 2321
8dff2b5c
VZ
2322static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2323 wxListItem& info,
2324 LV_ITEM& lvItem)
2bda0e17 2325{
73d8c5fd 2326 wxListItemInternalData *internaldata =
7cf46fdb
VZ
2327 (wxListItemInternalData *) lvItem.lParam;
2328
2329 if (internaldata)
2330 info.m_data = internaldata->lParam;
2331
0b5efdc7
VZ
2332 info.m_mask = 0;
2333 info.m_state = 0;
2334 info.m_stateMask = 0;
2335 info.m_itemId = lvItem.iItem;
acb62b84 2336
0b5efdc7 2337 long oldMask = lvItem.mask;
acb62b84 2338
0b5efdc7 2339 bool needText = FALSE;
8dff2b5c 2340 if (hwndListCtrl != 0)
acb62b84 2341 {
0b5efdc7
VZ
2342 if ( lvItem.mask & LVIF_TEXT )
2343 needText = FALSE;
2344 else
2345 needText = TRUE;
acb62b84 2346
0b5efdc7
VZ
2347 if ( needText )
2348 {
837e5743 2349 lvItem.pszText = new wxChar[513];
0b5efdc7
VZ
2350 lvItem.cchTextMax = 512;
2351 }
edccf428 2352 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
8dff2b5c 2353 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
0b5efdc7 2354 }
acb62b84 2355
0b5efdc7 2356 if ( lvItem.mask & LVIF_STATE )
acb62b84 2357 {
0b5efdc7
VZ
2358 info.m_mask |= wxLIST_MASK_STATE;
2359
2360 if ( lvItem.stateMask & LVIS_CUT)
2361 {
edccf428 2362 info.m_stateMask |= wxLIST_STATE_CUT;
0b5efdc7 2363 if ( lvItem.state & LVIS_CUT )
edccf428 2364 info.m_state |= wxLIST_STATE_CUT;
0b5efdc7
VZ
2365 }
2366 if ( lvItem.stateMask & LVIS_DROPHILITED)
2367 {
edccf428 2368 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
0b5efdc7 2369 if ( lvItem.state & LVIS_DROPHILITED )
edccf428 2370 info.m_state |= wxLIST_STATE_DROPHILITED;
0b5efdc7
VZ
2371 }
2372 if ( lvItem.stateMask & LVIS_FOCUSED)
2373 {
edccf428 2374 info.m_stateMask |= wxLIST_STATE_FOCUSED;
0b5efdc7 2375 if ( lvItem.state & LVIS_FOCUSED )
edccf428 2376 info.m_state |= wxLIST_STATE_FOCUSED;
0b5efdc7
VZ
2377 }
2378 if ( lvItem.stateMask & LVIS_SELECTED)
2379 {
edccf428 2380 info.m_stateMask |= wxLIST_STATE_SELECTED;
0b5efdc7 2381 if ( lvItem.state & LVIS_SELECTED )
edccf428 2382 info.m_state |= wxLIST_STATE_SELECTED;
0b5efdc7 2383 }
acb62b84 2384 }
0b5efdc7
VZ
2385
2386 if ( lvItem.mask & LVIF_TEXT )
acb62b84 2387 {
0b5efdc7
VZ
2388 info.m_mask |= wxLIST_MASK_TEXT;
2389 info.m_text = lvItem.pszText;
acb62b84 2390 }
0b5efdc7 2391 if ( lvItem.mask & LVIF_IMAGE )
acb62b84 2392 {
0b5efdc7
VZ
2393 info.m_mask |= wxLIST_MASK_IMAGE;
2394 info.m_image = lvItem.iImage;
acb62b84 2395 }
0b5efdc7
VZ
2396 if ( lvItem.mask & LVIF_PARAM )
2397 info.m_mask |= wxLIST_MASK_DATA;
2398 if ( lvItem.mask & LVIF_DI_SETITEM )
2399 info.m_mask |= wxLIST_SET_ITEM;
2400 info.m_col = lvItem.iSubItem;
2401
2402 if (needText)
acb62b84 2403 {
0b5efdc7
VZ
2404 if (lvItem.pszText)
2405 delete[] lvItem.pszText;
acb62b84 2406 }
edccf428 2407 lvItem.mask = oldMask;
2bda0e17
KB
2408}
2409
8dff2b5c
VZ
2410static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2411{
2412 if (stateMask & wxLIST_STATE_CUT)
2413 {
2414 lvItem.stateMask |= LVIS_CUT;
2415 if (state & wxLIST_STATE_CUT)
2416 lvItem.state |= LVIS_CUT;
2417 }
2418 if (stateMask & wxLIST_STATE_DROPHILITED)
2419 {
2420 lvItem.stateMask |= LVIS_DROPHILITED;
2421 if (state & wxLIST_STATE_DROPHILITED)
2422 lvItem.state |= LVIS_DROPHILITED;
2423 }
2424 if (stateMask & wxLIST_STATE_FOCUSED)
2425 {
2426 lvItem.stateMask |= LVIS_FOCUSED;
2427 if (state & wxLIST_STATE_FOCUSED)
2428 lvItem.state |= LVIS_FOCUSED;
2429 }
2430 if (stateMask & wxLIST_STATE_SELECTED)
2431 {
2432 lvItem.stateMask |= LVIS_SELECTED;
2433 if (state & wxLIST_STATE_SELECTED)
2434 lvItem.state |= LVIS_SELECTED;
2435 }
2436}
2437
2438static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2439 const wxListItem& info,
2440 LV_ITEM& lvItem)
2bda0e17 2441{
edccf428 2442 lvItem.iItem = (int) info.m_itemId;
acb62b84 2443
edccf428 2444 lvItem.iImage = info.m_image;
0b5efdc7
VZ
2445 lvItem.stateMask = 0;
2446 lvItem.state = 0;
2447 lvItem.mask = 0;
2448 lvItem.iSubItem = info.m_col;
acb62b84 2449
0b5efdc7 2450 if (info.m_mask & wxLIST_MASK_STATE)
acb62b84 2451 {
edccf428 2452 lvItem.mask |= LVIF_STATE;
8dff2b5c
VZ
2453
2454 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
acb62b84 2455 }
acb62b84 2456
0b5efdc7 2457 if (info.m_mask & wxLIST_MASK_TEXT)
acb62b84 2458 {
edccf428 2459 lvItem.mask |= LVIF_TEXT;
0b5efdc7
VZ
2460 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
2461 {
2462 lvItem.pszText = LPSTR_TEXTCALLBACK;
2463 }
2464 else
2465 {
e421922f
VZ
2466 // pszText is not const, hence the cast
2467 lvItem.pszText = (wxChar *)info.m_text.c_str();
0b5efdc7
VZ
2468 if ( lvItem.pszText )
2469 lvItem.cchTextMax = info.m_text.Length();
2470 else
2471 lvItem.cchTextMax = 0;
2472 }
acb62b84 2473 }
0b5efdc7 2474 if (info.m_mask & wxLIST_MASK_IMAGE)
edccf428 2475 lvItem.mask |= LVIF_IMAGE;
2bda0e17
KB
2476}
2477
574c939e 2478static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
6f806543
VZ
2479 LV_COLUMN& lvCol)
2480{
2481 wxZeroMemory(lvCol);
2482
2483 if ( item.m_mask & wxLIST_MASK_TEXT )
2484 {
2485 lvCol.mask |= LVCF_TEXT;
2486 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
2487 }
2488
2489 if ( item.m_mask & wxLIST_MASK_FORMAT )
2490 {
2491 lvCol.mask |= LVCF_FMT;
2492
2493 if ( item.m_format == wxLIST_FORMAT_LEFT )
2494 lvCol.fmt = LVCFMT_LEFT;
2495 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
2496 lvCol.fmt = LVCFMT_RIGHT;
2497 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
2498 lvCol.fmt = LVCFMT_CENTER;
2499 }
2500
2501 if ( item.m_mask & wxLIST_MASK_WIDTH )
2502 {
2503 lvCol.mask |= LVCF_WIDTH;
2504 if ( item.m_width == wxLIST_AUTOSIZE)
2505 lvCol.cx = LVSCW_AUTOSIZE;
2506 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2507 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
2508 else
2509 lvCol.cx = item.m_width;
2510 }
2511
244531bc 2512#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
5145e34f 2513 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
6f806543
VZ
2514 if ( item.m_mask & wxLIST_MASK_IMAGE )
2515 {
2516 if ( wxTheApp->GetComCtl32Version() >= 470 )
2517 {
2518 lvCol.mask |= LVCF_IMAGE;
2519 lvCol.iImage = item.m_image;
2520 }
2521 //else: it doesn't support item images anyhow
2522 }
244531bc 2523#endif
6f806543
VZ
2524}
2525
1e6feb95 2526#endif // wxUSE_LISTCTRL