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