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