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