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