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