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