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