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