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