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