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