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