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