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