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