]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
fix crash which happened if you called SetAttr(NULL) followed by SetAttr(attr) (...
[wxWidgets.git] / src / msw / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
3 // Purpose: wxListCtrl
4 // Author: Julian Smart
5 // Modified by: Agron Selimaj
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTCTRL
28
29 #include "wx/listctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/app.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/settings.h"
37 #include "wx/dcclient.h"
38 #include "wx/textctrl.h"
39 #endif
40
41 #include "wx/imaglist.h"
42
43 #include "wx/msw/private.h"
44
45 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
46 #include <ole2.h>
47 #include <shellapi.h>
48 #if _WIN32_WCE < 400
49 #include <aygshell.h>
50 #endif
51 #endif
52
53 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
54 // it by its old name NM_FINDTIEM.
55 //
56 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
57 #define HAVE_NMLVFINDITEM 1
58 #elif defined(__DMC__) || defined(NM_FINDITEM)
59 #define HAVE_NMLVFINDITEM 1
60 #define NMLVFINDITEM NM_FINDITEM
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // private functions
65 // ----------------------------------------------------------------------------
66
67 // convert our state and mask flags to LV_ITEM constants
68 static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem);
69
70 // convert wxListItem to LV_ITEM
71 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
72 const wxListItem& info, LV_ITEM& lvItem);
73
74 // convert LV_ITEM to wxListItem
75 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
76 wxListItem& info,
77 /* const */ LV_ITEM& lvItem);
78
79 // convert our wxListItem to LV_COLUMN
80 static void wxConvertToMSWListCol(HWND hwndList,
81 int col,
82 const wxListItem& item,
83 LV_COLUMN& lvCol);
84
85 // ----------------------------------------------------------------------------
86 // private helper classes
87 // ----------------------------------------------------------------------------
88
89 // We have to handle both fooW and fooA notifications in several cases
90 // because of broken comctl32.dll and/or unicows.dll. This class is used to
91 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
92 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
93 // by wxConvertToMSWListItem().
94 #if wxUSE_UNICODE
95 #define LV_ITEM_NATIVE LV_ITEMW
96 #define LV_ITEM_OTHER LV_ITEMA
97
98 #define LV_CONV_TO_WX cMB2WX
99 #define LV_CONV_BUF wxMB2WXbuf
100 #else // ANSI
101 #define LV_ITEM_NATIVE LV_ITEMA
102 #define LV_ITEM_OTHER LV_ITEMW
103
104 #define LV_CONV_TO_WX cWC2WX
105 #define LV_CONV_BUF wxWC2WXbuf
106 #endif // Unicode/ANSI
107
108 class wxLV_ITEM
109 {
110 public:
111 // default ctor, use Init() later
112 wxLV_ITEM() { m_buf = NULL; m_pItem = NULL; }
113
114 // init without conversion
115 void Init(LV_ITEM_NATIVE& item)
116 {
117 wxASSERT_MSG( !m_pItem, _T("Init() called twice?") );
118
119 m_pItem = &item;
120 }
121
122 // init with conversion
123 void Init(const LV_ITEM_OTHER& item)
124 {
125 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
126 // point to our own m_item
127
128 // memcpy() can't work if the struct sizes are different
129 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER) == sizeof(LV_ITEM_NATIVE),
130 CodeCantWorkIfDiffSizes);
131
132 memcpy(&m_item, &item, sizeof(LV_ITEM_NATIVE));
133
134 // convert text from ANSI to Unicod if necessary
135 if ( (item.mask & LVIF_TEXT) && item.pszText )
136 {
137 m_buf = new LV_CONV_BUF(wxConvLocal.LV_CONV_TO_WX(item.pszText));
138 m_item.pszText = (wxChar *)m_buf->data();
139 }
140 }
141
142 // ctor without conversion
143 wxLV_ITEM(LV_ITEM_NATIVE& item) : m_buf(NULL), m_pItem(&item) { }
144
145 // ctor with conversion
146 wxLV_ITEM(LV_ITEM_OTHER& item) : m_buf(NULL)
147 {
148 Init(item);
149 }
150
151 ~wxLV_ITEM() { delete m_buf; }
152
153 // conversion to the real LV_ITEM
154 operator LV_ITEM_NATIVE&() const { return *m_pItem; }
155
156 private:
157 LV_CONV_BUF *m_buf;
158
159 LV_ITEM_NATIVE *m_pItem;
160 LV_ITEM_NATIVE m_item;
161
162 DECLARE_NO_COPY_CLASS(wxLV_ITEM)
163 };
164
165 ///////////////////////////////////////////////////////
166 // Problem:
167 // The MSW version had problems with SetTextColour() et
168 // al as the wxListItemAttr's were stored keyed on the
169 // item index. If a item was inserted anywhere but the end
170 // of the list the the text attributes (colour etc) for
171 // the following items were out of sync.
172 //
173 // Solution:
174 // Under MSW the only way to associate data with a List
175 // item independent of its position in the list is to
176 // store a pointer to it in its lParam attribute. However
177 // user programs are already using this (via the
178 // SetItemData() GetItemData() calls).
179 //
180 // However what we can do is store a pointer to a
181 // structure which contains the attributes we want *and*
182 // a lParam for the users data, e.g.
183 //
184 // class wxListItemInternalData
185 // {
186 // public:
187 // wxListItemAttr *attr;
188 // long lParam; // user data
189 // };
190 //
191 // To conserve memory, a wxListItemInternalData is
192 // only allocated for a LV_ITEM if text attributes or
193 // user data(lparam) are being set.
194
195
196 // class wxListItemInternalData
197 class wxListItemInternalData
198 {
199 public:
200 wxListItemAttr *attr;
201 LPARAM lParam; // user data
202
203 wxListItemInternalData() : attr(NULL), lParam(0) {}
204 ~wxListItemInternalData()
205 {
206 if (attr)
207 delete attr;
208 }
209
210 DECLARE_NO_COPY_CLASS(wxListItemInternalData)
211 };
212
213 // Get the internal data structure
214 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId);
215 static wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId);
216 static wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId);
217 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId);
218
219
220 #if wxUSE_EXTENDED_RTTI
221 WX_DEFINE_FLAGS( wxListCtrlStyle )
222
223 wxBEGIN_FLAGS( wxListCtrlStyle )
224 // new style border flags, we put them first to
225 // use them for streaming out
226 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
227 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
228 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
229 wxFLAGS_MEMBER(wxBORDER_RAISED)
230 wxFLAGS_MEMBER(wxBORDER_STATIC)
231 wxFLAGS_MEMBER(wxBORDER_NONE)
232
233 // old style border flags
234 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
235 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
236 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
237 wxFLAGS_MEMBER(wxRAISED_BORDER)
238 wxFLAGS_MEMBER(wxSTATIC_BORDER)
239 wxFLAGS_MEMBER(wxBORDER)
240
241 // standard window styles
242 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
243 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
244 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
245 wxFLAGS_MEMBER(wxWANTS_CHARS)
246 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
247 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
248 wxFLAGS_MEMBER(wxVSCROLL)
249 wxFLAGS_MEMBER(wxHSCROLL)
250
251 wxFLAGS_MEMBER(wxLC_LIST)
252 wxFLAGS_MEMBER(wxLC_REPORT)
253 wxFLAGS_MEMBER(wxLC_ICON)
254 wxFLAGS_MEMBER(wxLC_SMALL_ICON)
255 wxFLAGS_MEMBER(wxLC_ALIGN_TOP)
256 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT)
257 wxFLAGS_MEMBER(wxLC_AUTOARRANGE)
258 wxFLAGS_MEMBER(wxLC_USER_TEXT)
259 wxFLAGS_MEMBER(wxLC_EDIT_LABELS)
260 wxFLAGS_MEMBER(wxLC_NO_HEADER)
261 wxFLAGS_MEMBER(wxLC_SINGLE_SEL)
262 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING)
263 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING)
264 wxFLAGS_MEMBER(wxLC_VIRTUAL)
265
266 wxEND_FLAGS( wxListCtrlStyle )
267
268 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h")
269
270 wxBEGIN_PROPERTIES_TABLE(wxListCtrl)
271 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
272
273 wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
274 wxEND_PROPERTIES_TABLE()
275
276 wxBEGIN_HANDLERS_TABLE(wxListCtrl)
277 wxEND_HANDLERS_TABLE()
278
279 wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
280
281 /*
282 TODO : Expose more information of a list's layout etc. via appropriate objects (a la NotebookPageInfo)
283 */
284 #else
285 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
286 #endif
287
288 IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
289 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
290
291 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
292
293 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
294 EVT_PAINT(wxListCtrl::OnPaint)
295 END_EVENT_TABLE()
296
297 // ============================================================================
298 // implementation
299 // ============================================================================
300
301 // ----------------------------------------------------------------------------
302 // wxListCtrl construction
303 // ----------------------------------------------------------------------------
304
305 void wxListCtrl::Init()
306 {
307 m_imageListNormal = NULL;
308 m_imageListSmall = NULL;
309 m_imageListState = NULL;
310 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false;
311 m_colCount = 0;
312 m_count = 0;
313 m_ignoreChangeMessages = false;
314 m_textCtrl = NULL;
315 m_AnyInternalData = false;
316 m_hasAnyAttr = false;
317 }
318
319 bool wxListCtrl::Create(wxWindow *parent,
320 wxWindowID id,
321 const wxPoint& pos,
322 const wxSize& size,
323 long style,
324 const wxValidator& validator,
325 const wxString& name)
326 {
327 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
328 return false;
329
330 if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) )
331 return false;
332
333 // explicitly say that we want to use Unicode because otherwise we get ANSI
334 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
335 wxSetCCUnicodeFormat(GetHwnd());
336
337 // We must set the default text colour to the system/theme color, otherwise
338 // GetTextColour will always return black
339 SetTextColour(GetDefaultAttributes().colFg);
340
341 if ( InReportView() )
342 MSWSetExListStyles();
343
344 return true;
345 }
346
347 void wxListCtrl::MSWSetExListStyles()
348 {
349 // for comctl32.dll v 4.70+ we want to have some non default extended
350 // styles because it's prettier (and also because wxGTK does it like this)
351 if ( wxApp::GetComCtl32Version() >= 470 )
352 {
353 ::SendMessage
354 (
355 GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
356 // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's
357 // not defined in the SDK headers
358 #ifdef LVS_EX_LABELTIP
359 LVS_EX_LABELTIP |
360 #endif
361 LVS_EX_FULLROWSELECT |
362 LVS_EX_SUBITEMIMAGES |
363 // normally this should be governed by a style as it's probably not
364 // always appropriate, but we don't have any free styles left and
365 // it seems better to enable it by default than disable
366 LVS_EX_HEADERDRAGDROP
367 );
368 }
369 }
370
371 WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
372 {
373 WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle);
374
375 wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
376
377 #ifdef __WXDEBUG__
378 size_t nModes = 0;
379
380 #define MAP_MODE_STYLE(wx, ms) \
381 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
382 #else // !__WXDEBUG__
383 #define MAP_MODE_STYLE(wx, ms) \
384 if ( style & (wx) ) wstyle |= (ms);
385 #endif // __WXDEBUG__
386
387 MAP_MODE_STYLE(wxLC_ICON, LVS_ICON)
388 MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON)
389 MAP_MODE_STYLE(wxLC_LIST, LVS_LIST)
390 MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT)
391
392 wxASSERT_MSG( nModes == 1,
393 _T("wxListCtrl style should have exactly one mode bit set") );
394
395 #undef MAP_MODE_STYLE
396
397 if ( style & wxLC_ALIGN_LEFT )
398 wstyle |= LVS_ALIGNLEFT;
399
400 if ( style & wxLC_ALIGN_TOP )
401 wstyle |= LVS_ALIGNTOP;
402
403 if ( style & wxLC_AUTOARRANGE )
404 wstyle |= LVS_AUTOARRANGE;
405
406 if ( style & wxLC_NO_SORT_HEADER )
407 wstyle |= LVS_NOSORTHEADER;
408
409 if ( style & wxLC_NO_HEADER )
410 wstyle |= LVS_NOCOLUMNHEADER;
411
412 if ( style & wxLC_EDIT_LABELS )
413 wstyle |= LVS_EDITLABELS;
414
415 if ( style & wxLC_SINGLE_SEL )
416 wstyle |= LVS_SINGLESEL;
417
418 if ( style & wxLC_SORT_ASCENDING )
419 {
420 wstyle |= LVS_SORTASCENDING;
421
422 wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING),
423 _T("can't sort in ascending and descending orders at once") );
424 }
425 else if ( style & wxLC_SORT_DESCENDING )
426 wstyle |= LVS_SORTDESCENDING;
427
428 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
429 if ( style & wxLC_VIRTUAL )
430 {
431 int ver = wxApp::GetComCtl32Version();
432 if ( ver < 470 )
433 {
434 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."),
435 ver / 100, ver % 100);
436 }
437
438 wstyle |= LVS_OWNERDATA;
439 }
440 #endif // ancient cygwin
441
442 return wstyle;
443 }
444
445 void wxListCtrl::UpdateStyle()
446 {
447 if ( GetHwnd() )
448 {
449 // The new window view style
450 DWORD dwStyleNew = MSWGetStyle(m_windowStyle, NULL);
451
452 // some styles are not returned by MSWGetStyle()
453 if ( IsShown() )
454 dwStyleNew |= WS_VISIBLE;
455
456 // Get the current window style.
457 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
458
459 // we don't have wxVSCROLL style, but the list control may have it,
460 // don't change it then
461 dwStyleNew |= dwStyleOld & (WS_HSCROLL | WS_VSCROLL);
462
463 // Only set the window style if the view bits have changed.
464 if ( dwStyleOld != dwStyleNew )
465 {
466 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
467
468 // if we switched to the report view, set the extended styles for
469 // it too
470 if ( !(dwStyleOld & LVS_REPORT) && (dwStyleNew & LVS_REPORT) )
471 MSWSetExListStyles();
472 }
473 }
474 }
475
476 void wxListCtrl::FreeAllInternalData()
477 {
478 if (m_AnyInternalData)
479 {
480 int n = GetItemCount();
481
482 m_ignoreChangeMessages = true;
483 for (int i = 0; i < n; i++)
484 wxDeleteInternalData(this, i);
485 m_ignoreChangeMessages = false;
486
487 m_AnyInternalData = false;
488 }
489 }
490
491 void wxListCtrl::DeleteEditControl()
492 {
493 if ( m_textCtrl )
494 {
495 m_textCtrl->UnsubclassWin();
496 m_textCtrl->SetHWND(0);
497 delete m_textCtrl;
498 m_textCtrl = NULL;
499 }
500 }
501
502 wxListCtrl::~wxListCtrl()
503 {
504 FreeAllInternalData();
505
506 DeleteEditControl();
507
508 if (m_ownsImageListNormal)
509 delete m_imageListNormal;
510 if (m_ownsImageListSmall)
511 delete m_imageListSmall;
512 if (m_ownsImageListState)
513 delete m_imageListState;
514 }
515
516 // ----------------------------------------------------------------------------
517 // set/get/change style
518 // ----------------------------------------------------------------------------
519
520 // Add or remove a single window style
521 void wxListCtrl::SetSingleStyle(long style, bool add)
522 {
523 long flag = GetWindowStyleFlag();
524
525 // Get rid of conflicting styles
526 if ( add )
527 {
528 if ( style & wxLC_MASK_TYPE)
529 flag = flag & ~wxLC_MASK_TYPE;
530 if ( style & wxLC_MASK_ALIGN )
531 flag = flag & ~wxLC_MASK_ALIGN;
532 if ( style & wxLC_MASK_SORT )
533 flag = flag & ~wxLC_MASK_SORT;
534 }
535
536 if ( add )
537 flag |= style;
538 else
539 flag &= ~style;
540
541 SetWindowStyleFlag(flag);
542 }
543
544 // Set the whole window style
545 void wxListCtrl::SetWindowStyleFlag(long flag)
546 {
547 if ( flag != m_windowStyle )
548 {
549 wxControl::SetWindowStyleFlag(flag);
550
551 UpdateStyle();
552
553 Refresh();
554 }
555 }
556
557 // ----------------------------------------------------------------------------
558 // accessors
559 // ----------------------------------------------------------------------------
560
561 /* static */ wxVisualAttributes
562 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
563 {
564 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
565
566 // common controls have their own default font
567 attrs.font = wxGetCCDefaultFont();
568
569 return attrs;
570 }
571
572 // Sets the foreground, i.e. text, colour
573 bool wxListCtrl::SetForegroundColour(const wxColour& col)
574 {
575 if ( !wxWindow::SetForegroundColour(col) )
576 return false;
577
578 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
579
580 return true;
581 }
582
583 // Sets the background colour
584 bool wxListCtrl::SetBackgroundColour(const wxColour& col)
585 {
586 if ( !wxWindow::SetBackgroundColour(col) )
587 return false;
588
589 // we set the same colour for both the "empty" background and the items
590 // background
591 COLORREF color = wxColourToRGB(col);
592 ListView_SetBkColor(GetHwnd(), color);
593 ListView_SetTextBkColor(GetHwnd(), color);
594
595 return true;
596 }
597
598 // Gets information about this column
599 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
600 {
601 LV_COLUMN lvCol;
602 wxZeroMemory(lvCol);
603
604 lvCol.mask = LVCF_WIDTH;
605
606 if ( item.m_mask & wxLIST_MASK_TEXT )
607 {
608 lvCol.mask |= LVCF_TEXT;
609 lvCol.pszText = new wxChar[513];
610 lvCol.cchTextMax = 512;
611 }
612
613 if ( item.m_mask & wxLIST_MASK_FORMAT )
614 {
615 lvCol.mask |= LVCF_FMT;
616 }
617
618 if ( item.m_mask & wxLIST_MASK_IMAGE )
619 {
620 lvCol.mask |= LVCF_IMAGE;
621 }
622
623 bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0;
624
625 // item.m_subItem = lvCol.iSubItem;
626 item.m_width = lvCol.cx;
627
628 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
629 {
630 item.m_text = lvCol.pszText;
631 delete[] lvCol.pszText;
632 }
633
634 if ( item.m_mask & wxLIST_MASK_FORMAT )
635 {
636 switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) {
637 case LVCFMT_LEFT:
638 item.m_format = wxLIST_FORMAT_LEFT;
639 break;
640 case LVCFMT_RIGHT:
641 item.m_format = wxLIST_FORMAT_RIGHT;
642 break;
643 case LVCFMT_CENTER:
644 item.m_format = wxLIST_FORMAT_CENTRE;
645 break;
646 default:
647 item.m_format = -1; // Unknown?
648 break;
649 }
650 }
651
652 // the column images were not supported in older versions but how to check
653 // for this? we can't use _WIN32_IE because we always define it to a very
654 // high value, so see if another symbol which is only defined starting from
655 // comctl32.dll 4.70 is available
656 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
657 if ( item.m_mask & wxLIST_MASK_IMAGE )
658 {
659 item.m_image = lvCol.iImage;
660 }
661 #endif // LVCOLUMN::iImage exists
662
663 return success;
664 }
665
666 // Sets information about this column
667 bool wxListCtrl::SetColumn(int col, const wxListItem& item)
668 {
669 LV_COLUMN lvCol;
670 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
671
672 return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
673 }
674
675 // Gets the column width
676 int wxListCtrl::GetColumnWidth(int col) const
677 {
678 return ListView_GetColumnWidth(GetHwnd(), col);
679 }
680
681 // Sets the column width
682 bool wxListCtrl::SetColumnWidth(int col, int width)
683 {
684 if ( m_windowStyle & wxLC_LIST )
685 col = 0;
686
687 if ( width == wxLIST_AUTOSIZE)
688 width = LVSCW_AUTOSIZE;
689 else if ( width == wxLIST_AUTOSIZE_USEHEADER)
690 width = LVSCW_AUTOSIZE_USEHEADER;
691
692 return ListView_SetColumnWidth(GetHwnd(), col, width) != 0;
693 }
694
695 // ----------------------------------------------------------------------------
696 // columns order
697 // ----------------------------------------------------------------------------
698
699 int wxListCtrl::GetColumnOrder(int col) const
700 {
701 const int numCols = GetColumnCount();
702 wxCHECK_MSG( col >= 0 && col < numCols, -1, _T("Col index out of bounds") );
703
704 wxArrayInt indexArray(numCols);
705
706 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) )
707 return -1;
708
709 return indexArray[col];
710 }
711
712 int wxListCtrl::GetColumnIndexFromOrder(int order) const
713 {
714 const int numCols = GetColumnCount();
715 wxASSERT_MSG( order >= 0 && order < numCols, _T("Col order out of bounds") );
716
717 wxArrayInt indexArray(numCols);
718
719 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) )
720 return -1;
721
722 for ( int col = 0; col < numCols; col++ )
723 {
724 if ( indexArray[col] == order )
725 return col;
726 }
727
728 wxFAIL_MSG( _T("no column with with given order?") );
729
730 return -1;
731 }
732
733 // Gets the column order for all columns
734 wxArrayInt wxListCtrl::GetColumnsOrder() const
735 {
736 const int numCols = GetColumnCount();
737
738 wxArrayInt orders(numCols);
739 if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &orders[0]) )
740 orders.clear();
741
742 return orders;
743 }
744
745 // Sets the column order for all columns
746 bool wxListCtrl::SetColumnsOrder(const wxArrayInt& orders)
747 {
748 const int numCols = GetColumnCount();
749
750 wxCHECK_MSG( orders.size() == (size_t)numCols, false,
751 _T("wrong number of elements in column orders array") );
752
753 return ListView_SetColumnOrderArray(GetHwnd(), numCols, &orders[0]) != 0;
754 }
755
756
757 // Gets the number of items that can fit vertically in the
758 // visible area of the list control (list or report view)
759 // or the total number of items in the list control (icon
760 // or small icon view)
761 int wxListCtrl::GetCountPerPage() const
762 {
763 return ListView_GetCountPerPage(GetHwnd());
764 }
765
766 // Gets the edit control for editing labels.
767 wxTextCtrl* wxListCtrl::GetEditControl() const
768 {
769 // first check corresponds to the case when the label editing was started
770 // by user and hence m_textCtrl wasn't created by EditLabel() at all, while
771 // the second case corresponds to us being called from inside EditLabel()
772 // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this
773 // case EditLabel() did create the control but it didn't have an HWND to
774 // initialize it with yet
775 if ( !m_textCtrl || !m_textCtrl->GetHWND() )
776 {
777 HWND hwndEdit = ListView_GetEditControl(GetHwnd());
778 if ( hwndEdit )
779 {
780 wxListCtrl * const self = wx_const_cast(wxListCtrl *, this);
781
782 if ( !m_textCtrl )
783 self->m_textCtrl = new wxTextCtrl;
784 self->InitEditControl((WXHWND)hwndEdit);
785 }
786 }
787
788 return m_textCtrl;
789 }
790
791 // Gets information about the item
792 bool wxListCtrl::GetItem(wxListItem& info) const
793 {
794 LV_ITEM lvItem;
795 wxZeroMemory(lvItem);
796
797 lvItem.iItem = info.m_itemId;
798 lvItem.iSubItem = info.m_col;
799
800 if ( info.m_mask & wxLIST_MASK_TEXT )
801 {
802 lvItem.mask |= LVIF_TEXT;
803 lvItem.pszText = new wxChar[513];
804 lvItem.cchTextMax = 512;
805 }
806 else
807 {
808 lvItem.pszText = NULL;
809 }
810
811 if (info.m_mask & wxLIST_MASK_DATA)
812 lvItem.mask |= LVIF_PARAM;
813
814 if (info.m_mask & wxLIST_MASK_IMAGE)
815 lvItem.mask |= LVIF_IMAGE;
816
817 if ( info.m_mask & wxLIST_MASK_STATE )
818 {
819 lvItem.mask |= LVIF_STATE;
820 wxConvertToMSWFlags(0, info.m_stateMask, lvItem);
821 }
822
823 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
824 if ( !success )
825 {
826 wxLogError(_("Couldn't retrieve information about list control item %d."),
827 lvItem.iItem);
828 }
829 else
830 {
831 // give NULL as hwnd as we already have everything we need
832 wxConvertFromMSWListItem(NULL, info, lvItem);
833 }
834
835 if (lvItem.pszText)
836 delete[] lvItem.pszText;
837
838 return success;
839 }
840
841 // Sets information about the item
842 bool wxListCtrl::SetItem(wxListItem& info)
843 {
844 const long id = info.GetId();
845 wxCHECK_MSG( id >= 0 && id < GetItemCount(), false,
846 _T("invalid item index in SetItem") );
847
848 LV_ITEM item;
849 wxConvertToMSWListItem(this, info, item);
850
851 // we never update the lParam if it contains our pointer
852 // to the wxListItemInternalData structure
853 item.mask &= ~LVIF_PARAM;
854
855 // check if setting attributes or lParam
856 if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA))
857 {
858 // get internal item data
859 // perhaps a cache here ?
860 wxListItemInternalData *data = wxGetInternalData(this, id);
861
862 if (! data)
863 {
864 // need to set it
865 m_AnyInternalData = true;
866 data = new wxListItemInternalData();
867 item.lParam = (LPARAM) data;
868 item.mask |= LVIF_PARAM;
869 };
870
871
872 // user data
873 if (info.m_mask & wxLIST_MASK_DATA)
874 data->lParam = info.m_data;
875
876 // attributes
877 if ( info.HasAttributes() )
878 {
879 const wxListItemAttr& attrNew = *info.GetAttributes();
880
881 // don't overwrite the already set attributes if we have them
882 if ( data->attr )
883 data->attr->AssignFrom(attrNew);
884 else
885 data->attr = new wxListItemAttr(attrNew);
886 };
887 };
888
889
890 // we could be changing only the attribute in which case we don't need to
891 // call ListView_SetItem() at all
892 if ( item.mask )
893 {
894 item.cchTextMax = 0;
895 if ( !ListView_SetItem(GetHwnd(), &item) )
896 {
897 wxLogDebug(_T("ListView_SetItem() failed"));
898
899 return false;
900 }
901 }
902
903 // we need to update the item immediately to show the new image
904 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
905
906 // check whether it has any custom attributes
907 if ( info.HasAttributes() )
908 {
909 m_hasAnyAttr = true;
910
911 // if the colour has changed, we must redraw the item
912 updateNow = true;
913 }
914
915 if ( updateNow )
916 {
917 // we need this to make the change visible right now
918 RefreshItem(item.iItem);
919 }
920
921 return true;
922 }
923
924 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
925 {
926 wxListItem info;
927 info.m_text = label;
928 info.m_mask = wxLIST_MASK_TEXT;
929 info.m_itemId = index;
930 info.m_col = col;
931 if ( imageId > -1 )
932 {
933 info.m_image = imageId;
934 info.m_mask |= wxLIST_MASK_IMAGE;
935 }
936 return SetItem(info);
937 }
938
939
940 // Gets the item state
941 int wxListCtrl::GetItemState(long item, long stateMask) const
942 {
943 wxListItem info;
944
945 info.m_mask = wxLIST_MASK_STATE;
946 info.m_stateMask = stateMask;
947 info.m_itemId = item;
948
949 if (!GetItem(info))
950 return 0;
951
952 return info.m_state;
953 }
954
955 // Sets the item state
956 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
957 {
958 // NB: don't use SetItem() here as it doesn't work with the virtual list
959 // controls
960 LV_ITEM lvItem;
961 wxZeroMemory(lvItem);
962
963 wxConvertToMSWFlags(state, stateMask, lvItem);
964
965 const bool changingFocus = (stateMask & wxLIST_STATE_FOCUSED) &&
966 (state & wxLIST_STATE_FOCUSED);
967
968 // for the virtual list controls we need to refresh the previously focused
969 // item manually when changing focus without changing selection
970 // programmatically because otherwise it keeps its focus rectangle until
971 // next repaint (yet another comctl32 bug)
972 long focusOld;
973 if ( IsVirtual() && changingFocus )
974 {
975 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
976 }
977 else
978 {
979 focusOld = -1;
980 }
981
982 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
983 (WPARAM)item, (LPARAM)&lvItem) )
984 {
985 wxLogLastError(_T("ListView_SetItemState"));
986
987 return false;
988 }
989
990 if ( focusOld != -1 )
991 {
992 // no need to refresh the item if it was previously selected, it would
993 // only result in annoying flicker
994 if ( !(GetItemState(focusOld,
995 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
996 {
997 RefreshItem(focusOld);
998 }
999 }
1000
1001 // we expect the selection anchor, i.e. the item from which multiple
1002 // selection (such as performed with e.g. Shift-arrows) starts, to be the
1003 // same as the currently focused item but the native control doesn't update
1004 // it when we change focus and leaves at the last item it set itself focus
1005 // to, so do it explicitly
1006 if ( changingFocus && !HasFlag(wxLC_SINGLE_SEL) )
1007 {
1008 ListView_SetSelectionMark(GetHwnd(), item);
1009 }
1010
1011 return true;
1012 }
1013
1014 // Sets the item image
1015 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
1016 {
1017 return SetItemColumnImage(item, 0, image);
1018 }
1019
1020 // Sets the item image
1021 bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
1022 {
1023 wxListItem info;
1024
1025 info.m_mask = wxLIST_MASK_IMAGE;
1026 info.m_image = image;
1027 info.m_itemId = item;
1028 info.m_col = column;
1029
1030 return SetItem(info);
1031 }
1032
1033 // Gets the item text
1034 wxString wxListCtrl::GetItemText(long item) const
1035 {
1036 wxListItem info;
1037
1038 info.m_mask = wxLIST_MASK_TEXT;
1039 info.m_itemId = item;
1040
1041 if (!GetItem(info))
1042 return wxEmptyString;
1043 return info.m_text;
1044 }
1045
1046 // Sets the item text
1047 void wxListCtrl::SetItemText(long item, const wxString& str)
1048 {
1049 wxListItem info;
1050
1051 info.m_mask = wxLIST_MASK_TEXT;
1052 info.m_itemId = item;
1053 info.m_text = str;
1054
1055 SetItem(info);
1056 }
1057
1058 // Gets the item data
1059 wxUIntPtr wxListCtrl::GetItemData(long item) const
1060 {
1061 wxListItem info;
1062
1063 info.m_mask = wxLIST_MASK_DATA;
1064 info.m_itemId = item;
1065
1066 if (!GetItem(info))
1067 return 0;
1068 return info.m_data;
1069 }
1070
1071 // Sets the item data
1072 bool wxListCtrl::SetItemPtrData(long item, wxUIntPtr data)
1073 {
1074 wxListItem info;
1075
1076 info.m_mask = wxLIST_MASK_DATA;
1077 info.m_itemId = item;
1078 info.m_data = data;
1079
1080 return SetItem(info);
1081 }
1082
1083 wxRect wxListCtrl::GetViewRect() const
1084 {
1085 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
1086 _T("wxListCtrl::GetViewRect() only works in icon mode") );
1087
1088 RECT rc;
1089 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
1090 {
1091 wxLogDebug(_T("ListView_GetViewRect() failed."));
1092
1093 wxZeroMemory(rc);
1094 }
1095
1096 wxRect rect;
1097 wxCopyRECTToRect(rc, rect);
1098
1099 return rect;
1100 }
1101
1102 // Gets the item rectangle
1103 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
1104 {
1105 return GetSubItemRect( item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code) ;
1106 }
1107
1108 /*!
1109 * Retrieve coordinates and size of a specified subitem of a listview control.
1110 * This function only works if the listview control is in the report mode.
1111 *
1112 * @param item : Item number
1113 * @param subItem : Subitem or column number, use -1 for the whole row including
1114 * all columns or subitems
1115 * @param rect : A pointer to an allocated wxRect object
1116 * @param code : Specify the part of the subitem coordinates you need. Choices are
1117 * wxLIST_RECT_BOUNDS, wxLIST_RECT_ICON, wxLIST_RECT_LABEL
1118 *
1119 * @return bool : True if successful.
1120 */
1121 bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) const
1122 {
1123 RECT rectWin;
1124
1125 int codeWin;
1126 if ( code == wxLIST_RECT_BOUNDS )
1127 codeWin = LVIR_BOUNDS;
1128 else if ( code == wxLIST_RECT_ICON )
1129 codeWin = LVIR_ICON;
1130 else if ( code == wxLIST_RECT_LABEL )
1131 codeWin = LVIR_LABEL;
1132 else
1133 {
1134 wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") );
1135 codeWin = LVIR_BOUNDS;
1136 }
1137
1138 bool success;
1139 if( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM)
1140 {
1141 success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
1142 }
1143 else if( subItem >= 0)
1144 {
1145 success = ListView_GetSubItemRect( GetHwnd(), (int) item, (int) subItem, codeWin, &rectWin) != 0;
1146 }
1147 else
1148 {
1149 wxFAIL_MSG( _T("incorrect subItem number in GetSubItemRect()") );
1150 return false;
1151 }
1152
1153 rect.x = rectWin.left;
1154 rect.y = rectWin.top;
1155 rect.width = rectWin.right - rectWin.left;
1156 rect.height = rectWin.bottom - rectWin.top;
1157
1158 return success;
1159 }
1160
1161
1162
1163
1164 // Gets the item position
1165 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1166 {
1167 POINT pt;
1168
1169 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
1170
1171 pos.x = pt.x; pos.y = pt.y;
1172 return success;
1173 }
1174
1175 // Sets the item position.
1176 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1177 {
1178 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
1179 }
1180
1181 // Gets the number of items in the list control
1182 int wxListCtrl::GetItemCount() const
1183 {
1184 return m_count;
1185 }
1186
1187 wxSize wxListCtrl::GetItemSpacing() const
1188 {
1189 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
1190
1191 return wxSize(LOWORD(spacing), HIWORD(spacing));
1192 }
1193
1194 #if WXWIN_COMPATIBILITY_2_6
1195
1196 int wxListCtrl::GetItemSpacing(bool isSmall) const
1197 {
1198 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
1199 }
1200
1201 #endif // WXWIN_COMPATIBILITY_2_6
1202
1203 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1204 {
1205 wxListItem info;
1206 info.m_itemId = item;
1207 info.SetTextColour( col );
1208 SetItem( info );
1209 }
1210
1211 wxColour wxListCtrl::GetItemTextColour( long item ) const
1212 {
1213 wxColour col;
1214 wxListItemInternalData *data = wxGetInternalData(this, item);
1215 if ( data && data->attr )
1216 col = data->attr->GetTextColour();
1217
1218 return col;
1219 }
1220
1221 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1222 {
1223 wxListItem info;
1224 info.m_itemId = item;
1225 info.SetBackgroundColour( col );
1226 SetItem( info );
1227 }
1228
1229 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1230 {
1231 wxColour col;
1232 wxListItemInternalData *data = wxGetInternalData(this, item);
1233 if ( data && data->attr )
1234 col = data->attr->GetBackgroundColour();
1235
1236 return col;
1237 }
1238
1239 void wxListCtrl::SetItemFont( long item, const wxFont &f )
1240 {
1241 wxListItem info;
1242 info.m_itemId = item;
1243 info.SetFont( f );
1244 SetItem( info );
1245 }
1246
1247 wxFont wxListCtrl::GetItemFont( long item ) const
1248 {
1249 wxFont f;
1250 wxListItemInternalData *data = wxGetInternalData(this, item);
1251 if ( data && data->attr )
1252 f = data->attr->GetFont();
1253
1254 return f;
1255 }
1256
1257 // Gets the number of selected items in the list control
1258 int wxListCtrl::GetSelectedItemCount() const
1259 {
1260 return ListView_GetSelectedCount(GetHwnd());
1261 }
1262
1263 // Gets the text colour of the listview
1264 wxColour wxListCtrl::GetTextColour() const
1265 {
1266 COLORREF ref = ListView_GetTextColor(GetHwnd());
1267 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1268 return col;
1269 }
1270
1271 // Sets the text colour of the listview
1272 void wxListCtrl::SetTextColour(const wxColour& col)
1273 {
1274 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
1275 }
1276
1277 // Gets the index of the topmost visible item when in
1278 // list or report view
1279 long wxListCtrl::GetTopItem() const
1280 {
1281 return (long) ListView_GetTopIndex(GetHwnd());
1282 }
1283
1284 // Searches for an item, starting from 'item'.
1285 // 'geometry' is one of
1286 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1287 // 'state' is a state bit flag, one or more of
1288 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1289 // item can be -1 to find the first item that matches the
1290 // specified flags.
1291 // Returns the item or -1 if unsuccessful.
1292 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1293 {
1294 long flags = 0;
1295
1296 if ( geom == wxLIST_NEXT_ABOVE )
1297 flags |= LVNI_ABOVE;
1298 if ( geom == wxLIST_NEXT_ALL )
1299 flags |= LVNI_ALL;
1300 if ( geom == wxLIST_NEXT_BELOW )
1301 flags |= LVNI_BELOW;
1302 if ( geom == wxLIST_NEXT_LEFT )
1303 flags |= LVNI_TOLEFT;
1304 if ( geom == wxLIST_NEXT_RIGHT )
1305 flags |= LVNI_TORIGHT;
1306
1307 if ( state & wxLIST_STATE_CUT )
1308 flags |= LVNI_CUT;
1309 if ( state & wxLIST_STATE_DROPHILITED )
1310 flags |= LVNI_DROPHILITED;
1311 if ( state & wxLIST_STATE_FOCUSED )
1312 flags |= LVNI_FOCUSED;
1313 if ( state & wxLIST_STATE_SELECTED )
1314 flags |= LVNI_SELECTED;
1315
1316 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1317 }
1318
1319
1320 wxImageList *wxListCtrl::GetImageList(int which) const
1321 {
1322 if ( which == wxIMAGE_LIST_NORMAL )
1323 {
1324 return m_imageListNormal;
1325 }
1326 else if ( which == wxIMAGE_LIST_SMALL )
1327 {
1328 return m_imageListSmall;
1329 }
1330 else if ( which == wxIMAGE_LIST_STATE )
1331 {
1332 return m_imageListState;
1333 }
1334 return NULL;
1335 }
1336
1337 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1338 {
1339 int flags = 0;
1340 if ( which == wxIMAGE_LIST_NORMAL )
1341 {
1342 flags = LVSIL_NORMAL;
1343 if (m_ownsImageListNormal) delete m_imageListNormal;
1344 m_imageListNormal = imageList;
1345 m_ownsImageListNormal = false;
1346 }
1347 else if ( which == wxIMAGE_LIST_SMALL )
1348 {
1349 flags = LVSIL_SMALL;
1350 if (m_ownsImageListSmall) delete m_imageListSmall;
1351 m_imageListSmall = imageList;
1352 m_ownsImageListSmall = false;
1353 }
1354 else if ( which == wxIMAGE_LIST_STATE )
1355 {
1356 flags = LVSIL_STATE;
1357 if (m_ownsImageListState) delete m_imageListState;
1358 m_imageListState = imageList;
1359 m_ownsImageListState = false;
1360 }
1361 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1362 }
1363
1364 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1365 {
1366 SetImageList(imageList, which);
1367 if ( which == wxIMAGE_LIST_NORMAL )
1368 m_ownsImageListNormal = true;
1369 else if ( which == wxIMAGE_LIST_SMALL )
1370 m_ownsImageListSmall = true;
1371 else if ( which == wxIMAGE_LIST_STATE )
1372 m_ownsImageListState = true;
1373 }
1374
1375 // ----------------------------------------------------------------------------
1376 // Operations
1377 // ----------------------------------------------------------------------------
1378
1379 // Arranges the items
1380 bool wxListCtrl::Arrange(int flag)
1381 {
1382 UINT code = 0;
1383 if ( flag == wxLIST_ALIGN_LEFT )
1384 code = LVA_ALIGNLEFT;
1385 else if ( flag == wxLIST_ALIGN_TOP )
1386 code = LVA_ALIGNTOP;
1387 else if ( flag == wxLIST_ALIGN_DEFAULT )
1388 code = LVA_DEFAULT;
1389 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1390 code = LVA_SNAPTOGRID;
1391
1392 return (ListView_Arrange(GetHwnd(), code) != 0);
1393 }
1394
1395 // Deletes an item
1396 bool wxListCtrl::DeleteItem(long item)
1397 {
1398 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1399 {
1400 wxLogLastError(_T("ListView_DeleteItem"));
1401 return false;
1402 }
1403
1404 m_count--;
1405 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1406 wxT("m_count should match ListView_GetItemCount"));
1407
1408 // the virtual list control doesn't refresh itself correctly, help it
1409 if ( IsVirtual() )
1410 {
1411 // we need to refresh all the lines below the one which was deleted
1412 wxRect rectItem;
1413 if ( item > 0 && GetItemCount() )
1414 {
1415 GetItemRect(item - 1, rectItem);
1416 }
1417 else
1418 {
1419 rectItem.y =
1420 rectItem.height = 0;
1421 }
1422
1423 wxRect rectWin = GetRect();
1424 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1425 rectWin.y = rectItem.GetBottom();
1426
1427 RefreshRect(rectWin);
1428 }
1429
1430 return true;
1431 }
1432
1433 // Deletes all items
1434 bool wxListCtrl::DeleteAllItems()
1435 {
1436 return ListView_DeleteAllItems(GetHwnd()) != 0;
1437 }
1438
1439 // Deletes all items
1440 bool wxListCtrl::DeleteAllColumns()
1441 {
1442 while ( m_colCount > 0 )
1443 {
1444 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1445 {
1446 wxLogLastError(wxT("ListView_DeleteColumn"));
1447
1448 return false;
1449 }
1450
1451 m_colCount--;
1452 }
1453
1454 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1455
1456 return true;
1457 }
1458
1459 // Deletes a column
1460 bool wxListCtrl::DeleteColumn(int col)
1461 {
1462 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1463
1464 if ( success && (m_colCount > 0) )
1465 m_colCount --;
1466 return success;
1467 }
1468
1469 // Clears items, and columns if there are any.
1470 void wxListCtrl::ClearAll()
1471 {
1472 DeleteAllItems();
1473 if ( m_colCount > 0 )
1474 DeleteAllColumns();
1475 }
1476
1477 void wxListCtrl::InitEditControl(WXHWND hWnd)
1478 {
1479 m_textCtrl->SetHWND(hWnd);
1480 m_textCtrl->SubclassWin(hWnd);
1481 m_textCtrl->SetParent(this);
1482
1483 // we must disallow TABbing away from the control while the edit contol is
1484 // shown because this leaves it in some strange state (just try removing
1485 // this line and then pressing TAB while editing an item in listctrl
1486 // inside a panel)
1487 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1488 }
1489
1490 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1491 {
1492 wxCHECK_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)), NULL,
1493 "control used for label editing must be a wxTextCtrl" );
1494
1495 // ListView_EditLabel requires that the list has focus.
1496 SetFocus();
1497
1498 // create m_textCtrl here before calling ListView_EditLabel() because it
1499 // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and
1500 // the user handler for it can call GetEditControl() resulting in an on
1501 // demand creation of a stock wxTextCtrl instead of the control of a
1502 // (possibly) custom wxClassInfo
1503 DeleteEditControl();
1504 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1505
1506 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
1507 if ( !hWnd )
1508 {
1509 // failed to start editing
1510 delete m_textCtrl;
1511 m_textCtrl = NULL;
1512
1513 return NULL;
1514 }
1515
1516 // if GetEditControl() hasn't been called, we need to initialize the edit
1517 // control ourselves
1518 if ( !m_textCtrl->GetHWND() )
1519 InitEditControl(hWnd);
1520
1521 return m_textCtrl;
1522 }
1523
1524 // End label editing, optionally cancelling the edit
1525 bool wxListCtrl::EndEditLabel(bool cancel)
1526 {
1527 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1528 HWND hwnd = ListView_GetEditControl(GetHwnd());
1529 if ( !hwnd )
1530 return false;
1531
1532 if ( cancel )
1533 ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing
1534
1535 // we shouldn't destroy the control ourselves according to MSDN, which
1536 // proposes WM_CANCELMODE to do this, but it doesn't seem to work
1537 //
1538 // posting WM_CLOSE to it does seem to work without any side effects
1539 ::PostMessage(hwnd, WM_CLOSE, 0, 0);
1540
1541 return true;
1542 }
1543
1544 // Ensures this item is visible
1545 bool wxListCtrl::EnsureVisible(long item)
1546 {
1547 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE;
1548 }
1549
1550 // Find an item whose label matches this string, starting from the item after 'start'
1551 // or the beginning if 'start' is -1.
1552 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1553 {
1554 LV_FINDINFO findInfo;
1555
1556 findInfo.flags = LVFI_STRING;
1557 if ( partial )
1558 findInfo.flags |= LVFI_PARTIAL;
1559 findInfo.psz = str.wx_str();
1560
1561 // ListView_FindItem() excludes the first item from search and to look
1562 // through all the items you need to start from -1 which is unnatural and
1563 // inconsistent with the generic version - so we adjust the index
1564 if (start != -1)
1565 start --;
1566 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1567 }
1568
1569 // Find an item whose data matches this data, starting from the item after 'start'
1570 // or the beginning if 'start' is -1.
1571 // NOTE : Lindsay Mathieson - 14-July-2002
1572 // No longer use ListView_FindItem as the data attribute is now stored
1573 // in a wxListItemInternalData structure refernced by the actual lParam
1574 long wxListCtrl::FindItem(long start, wxUIntPtr data)
1575 {
1576 long idx = start + 1;
1577 long count = GetItemCount();
1578
1579 while (idx < count)
1580 {
1581 if (GetItemData(idx) == data)
1582 return idx;
1583 idx++;
1584 };
1585
1586 return -1;
1587 }
1588
1589 // Find an item nearest this position in the specified direction, starting from
1590 // the item after 'start' or the beginning if 'start' is -1.
1591 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1592 {
1593 LV_FINDINFO findInfo;
1594
1595 findInfo.flags = LVFI_NEARESTXY;
1596 findInfo.pt.x = pt.x;
1597 findInfo.pt.y = pt.y;
1598 findInfo.vkDirection = VK_RIGHT;
1599
1600 if ( direction == wxLIST_FIND_UP )
1601 findInfo.vkDirection = VK_UP;
1602 else if ( direction == wxLIST_FIND_DOWN )
1603 findInfo.vkDirection = VK_DOWN;
1604 else if ( direction == wxLIST_FIND_LEFT )
1605 findInfo.vkDirection = VK_LEFT;
1606 else if ( direction == wxLIST_FIND_RIGHT )
1607 findInfo.vkDirection = VK_RIGHT;
1608
1609 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1610 }
1611
1612 // Determines which item (if any) is at the specified point,
1613 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1614 long
1615 wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
1616 {
1617 LV_HITTESTINFO hitTestInfo;
1618 hitTestInfo.pt.x = (int) point.x;
1619 hitTestInfo.pt.y = (int) point.y;
1620
1621 long item;
1622 #ifdef LVM_SUBITEMHITTEST
1623 if ( ptrSubItem && wxApp::GetComCtl32Version() >= 470 )
1624 {
1625 item = ListView_SubItemHitTest(GetHwnd(), &hitTestInfo);
1626 *ptrSubItem = hitTestInfo.iSubItem;
1627 }
1628 else
1629 #endif // LVM_SUBITEMHITTEST
1630 {
1631 item = ListView_HitTest(GetHwnd(), &hitTestInfo);
1632 }
1633
1634 flags = 0;
1635
1636 if ( hitTestInfo.flags & LVHT_ABOVE )
1637 flags |= wxLIST_HITTEST_ABOVE;
1638 if ( hitTestInfo.flags & LVHT_BELOW )
1639 flags |= wxLIST_HITTEST_BELOW;
1640 if ( hitTestInfo.flags & LVHT_TOLEFT )
1641 flags |= wxLIST_HITTEST_TOLEFT;
1642 if ( hitTestInfo.flags & LVHT_TORIGHT )
1643 flags |= wxLIST_HITTEST_TORIGHT;
1644
1645 if ( hitTestInfo.flags & LVHT_NOWHERE )
1646 flags |= wxLIST_HITTEST_NOWHERE;
1647
1648 // note a bug or at least a very strange feature of comtl32.dll (tested
1649 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1650 // the right of the item label, ListView_HitTest() returns a combination of
1651 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1652 // the bits which don't make sense
1653 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1654 {
1655 flags |= wxLIST_HITTEST_ONITEMLABEL;
1656
1657 // do not translate LVHT_ONITEMICON here, as per above
1658 }
1659 else
1660 {
1661 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1662 flags |= wxLIST_HITTEST_ONITEMICON;
1663 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1664 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1665 }
1666
1667 return item;
1668 }
1669
1670
1671 // Inserts an item, returning the index of the new item if successful,
1672 // -1 otherwise.
1673 long wxListCtrl::InsertItem(const wxListItem& info)
1674 {
1675 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1676
1677 LV_ITEM item;
1678 wxConvertToMSWListItem(this, info, item);
1679 item.mask &= ~LVIF_PARAM;
1680
1681 // check wether we need to allocate our internal data
1682 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1683 if (needInternalData)
1684 {
1685 m_AnyInternalData = true;
1686 item.mask |= LVIF_PARAM;
1687
1688 // internal stucture that manages data
1689 wxListItemInternalData *data = new wxListItemInternalData();
1690 item.lParam = (LPARAM) data;
1691
1692 if (info.m_mask & wxLIST_MASK_DATA)
1693 data->lParam = info.m_data;
1694
1695 // check whether it has any custom attributes
1696 if ( info.HasAttributes() )
1697 {
1698 // take copy of attributes
1699 data->attr = new wxListItemAttr(*info.GetAttributes());
1700
1701 // and remember that we have some now...
1702 m_hasAnyAttr = true;
1703 }
1704 };
1705
1706 long rv = ListView_InsertItem(GetHwnd(), & item);
1707
1708 m_count++;
1709 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1710 wxT("m_count should match ListView_GetItemCount"));
1711
1712 return rv;
1713 }
1714
1715 long wxListCtrl::InsertItem(long index, const wxString& label)
1716 {
1717 wxListItem info;
1718 info.m_text = label;
1719 info.m_mask = wxLIST_MASK_TEXT;
1720 info.m_itemId = index;
1721 return InsertItem(info);
1722 }
1723
1724 // Inserts an image item
1725 long wxListCtrl::InsertItem(long index, int imageIndex)
1726 {
1727 wxListItem info;
1728 info.m_image = imageIndex;
1729 info.m_mask = wxLIST_MASK_IMAGE;
1730 info.m_itemId = index;
1731 return InsertItem(info);
1732 }
1733
1734 // Inserts an image/string item
1735 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1736 {
1737 wxListItem info;
1738 info.m_image = imageIndex;
1739 info.m_text = label;
1740 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1741 info.m_itemId = index;
1742 return InsertItem(info);
1743 }
1744
1745 // For list view mode (only), inserts a column.
1746 long wxListCtrl::InsertColumn(long col, const wxListItem& item)
1747 {
1748 LV_COLUMN lvCol;
1749 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
1750
1751 if ( !(lvCol.mask & LVCF_WIDTH) )
1752 {
1753 // always give some width to the new column: this one is compatible
1754 // with the generic version
1755 lvCol.mask |= LVCF_WIDTH;
1756 lvCol.cx = 80;
1757 }
1758
1759 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1760 if ( n != -1 )
1761 {
1762 m_colCount++;
1763 }
1764 else // failed to insert?
1765 {
1766 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1767 lvCol.pszText);
1768 }
1769
1770 return n;
1771 }
1772
1773 long wxListCtrl::InsertColumn(long col,
1774 const wxString& heading,
1775 int format,
1776 int width)
1777 {
1778 wxListItem item;
1779 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1780 item.m_text = heading;
1781 if ( width > -1 )
1782 {
1783 item.m_mask |= wxLIST_MASK_WIDTH;
1784 item.m_width = width;
1785 }
1786 item.m_format = format;
1787
1788 return InsertColumn(col, item);
1789 }
1790
1791 // scroll the control by the given number of pixels (exception: in list view,
1792 // dx is interpreted as number of columns)
1793 bool wxListCtrl::ScrollList(int dx, int dy)
1794 {
1795 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1796 {
1797 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1798
1799 return false;
1800 }
1801
1802 return true;
1803 }
1804
1805 // Sort items.
1806
1807 // fn is a function which takes 3 long arguments: item1, item2, data.
1808 // item1 is the long data associated with a first item (NOT the index).
1809 // item2 is the long data associated with a second item (NOT the index).
1810 // data is the same value as passed to SortItems.
1811 // The return value is a negative number if the first item should precede the second
1812 // item, a positive number of the second item should precede the first,
1813 // or zero if the two items are equivalent.
1814
1815 // data is arbitrary data to be passed to the sort function.
1816
1817 // Internal structures for proxying the user compare function
1818 // so that we can pass it the *real* user data
1819
1820 // translate lParam data and call user func
1821 struct wxInternalDataSort
1822 {
1823 wxListCtrlCompare user_fn;
1824 long data;
1825 };
1826
1827 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1828 {
1829 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1830
1831 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1832 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1833
1834 long d1 = (data1 == NULL ? 0 : data1->lParam);
1835 long d2 = (data2 == NULL ? 0 : data2->lParam);
1836
1837 return internalData->user_fn(d1, d2, internalData->data);
1838
1839 }
1840
1841 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1842 {
1843 struct wxInternalDataSort internalData;
1844 internalData.user_fn = fn;
1845 internalData.data = data;
1846
1847 // WPARAM cast is needed for mingw/cygwin
1848 if ( !ListView_SortItems(GetHwnd(),
1849 wxInternalDataCompareFunc,
1850 (WPARAM) &internalData) )
1851 {
1852 wxLogDebug(_T("ListView_SortItems() failed"));
1853
1854 return false;
1855 }
1856
1857 return true;
1858 }
1859
1860
1861
1862 // ----------------------------------------------------------------------------
1863 // message processing
1864 // ----------------------------------------------------------------------------
1865
1866 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1867 {
1868 if ( msg->message == WM_KEYDOWN )
1869 {
1870 // Only eat VK_RETURN if not being used by the application in
1871 // conjunction with modifiers
1872 if ( msg->wParam == VK_RETURN && !wxIsAnyModifierDown() )
1873 {
1874 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
1875 return false;
1876 }
1877 }
1878 return wxControl::MSWShouldPreProcessMessage(msg);
1879 }
1880
1881 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
1882 {
1883 const int id = (signed short)id_;
1884 if (cmd == EN_UPDATE)
1885 {
1886 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1887 event.SetEventObject( this );
1888 ProcessCommand(event);
1889 return true;
1890 }
1891 else if (cmd == EN_KILLFOCUS)
1892 {
1893 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1894 event.SetEventObject( this );
1895 ProcessCommand(event);
1896 return true;
1897 }
1898 else
1899 return false;
1900 }
1901
1902 // utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify
1903 int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick)
1904 {
1905 // find the column clicked: we have to search for it ourselves as the
1906 // notification message doesn't provide this info
1907
1908 // where did the click occur?
1909 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1910 if ( nmhdr->code == GN_CONTEXTMENU )
1911 {
1912 *ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1913 }
1914 else
1915 #endif //__WXWINCE__
1916 if ( !::GetCursorPos(ptClick) )
1917 {
1918 wxLogLastError(_T("GetCursorPos"));
1919 }
1920
1921 // we need to use listctrl coordinates for the event point so this is what
1922 // we return in ptClick, but for comparison with Header_GetItemRect()
1923 // result below we need to use header window coordinates
1924 POINT ptClickHeader = *ptClick;
1925 if ( !::ScreenToClient(nmhdr->hwndFrom, &ptClickHeader) )
1926 {
1927 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1928 }
1929
1930 if ( !::ScreenToClient(::GetParent(nmhdr->hwndFrom), ptClick) )
1931 {
1932 wxLogLastError(_T("ScreenToClient(listctrl)"));
1933 }
1934
1935 const int colCount = Header_GetItemCount(nmhdr->hwndFrom);
1936 for ( int col = 0; col < colCount; col++ )
1937 {
1938 RECT rect;
1939 if ( Header_GetItemRect(nmhdr->hwndFrom, col, &rect) )
1940 {
1941 if ( ::PtInRect(&rect, ptClickHeader) )
1942 {
1943 return col;
1944 }
1945 }
1946 }
1947
1948 return wxNOT_FOUND;
1949 }
1950
1951 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1952 {
1953
1954 // prepare the event
1955 // -----------------
1956
1957 wxListEvent event(wxEVT_NULL, m_windowId);
1958 event.SetEventObject(this);
1959
1960 wxEventType eventType = wxEVT_NULL;
1961
1962 NMHDR *nmhdr = (NMHDR *)lParam;
1963
1964 // if your compiler is as broken as this, you should really change it: this
1965 // code is needed for normal operation! #ifdef below is only useful for
1966 // automatic rebuilds which are done with a very old compiler version
1967 #ifdef HDN_BEGINTRACKA
1968
1969 // check for messages from the header (in report view)
1970 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1971
1972 // is it a message from the header?
1973 if ( nmhdr->hwndFrom == hwndHdr )
1974 {
1975 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1976
1977 event.m_itemIndex = -1;
1978
1979 switch ( nmhdr->code )
1980 {
1981 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1982 // TRACK messages even to ANSI programs: on my system I get
1983 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1984 //
1985 // work around is to simply catch both versions and hope that it
1986 // works (why should this message exist in ANSI and Unicode is
1987 // beyond me as it doesn't deal with strings at all...)
1988 //
1989 // note that fr HDN_TRACK another possibility could be to use
1990 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1991 // something other than the item width changes so we'd have to
1992 // filter out the unwanted events then
1993 case HDN_BEGINTRACKA:
1994 case HDN_BEGINTRACKW:
1995 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1996 // fall through
1997
1998 case HDN_TRACKA:
1999 case HDN_TRACKW:
2000 if ( eventType == wxEVT_NULL )
2001 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
2002 // fall through
2003
2004 case HDN_ENDTRACKA:
2005 case HDN_ENDTRACKW:
2006 if ( eventType == wxEVT_NULL )
2007 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
2008
2009 event.m_item.m_width = nmHDR->pitem->cxy;
2010 event.m_col = nmHDR->iItem;
2011 break;
2012
2013 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2014 case GN_CONTEXTMENU:
2015 #endif //__WXWINCE__
2016 case NM_RCLICK:
2017 {
2018 POINT ptClick;
2019
2020 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
2021 event.m_col = wxMSWGetColumnClicked(nmhdr, &ptClick);
2022 event.m_pointDrag.x = ptClick.x;
2023 event.m_pointDrag.y = ptClick.y;
2024 }
2025 break;
2026
2027 case HDN_GETDISPINFOW:
2028 // letting Windows XP handle this message results in mysterious
2029 // crashes in comctl32.dll seemingly because of bad message
2030 // parameters
2031 //
2032 // I have no idea what is the real cause of the bug (which is,
2033 // just to make things interesting, impossible to reproduce
2034 // reliably) but ignoring all these messages does fix it and
2035 // doesn't seem to have any negative consequences
2036 return true;
2037
2038 default:
2039 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2040 }
2041 }
2042 else
2043 #endif // defined(HDN_BEGINTRACKA)
2044 if ( nmhdr->hwndFrom == GetHwnd() )
2045 {
2046 // almost all messages use NM_LISTVIEW
2047 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
2048
2049 const int iItem = nmLV->iItem;
2050
2051
2052 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
2053 // ignored for efficiency. It is done here because the internal data is in the
2054 // process of being deleted so we don't want to try and access it below.
2055 if ( m_ignoreChangeMessages &&
2056 ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
2057 (nmLV->hdr.code == LVN_ITEMCHANGING)) )
2058 {
2059 return true;
2060 }
2061
2062
2063 // If we have a valid item then check if there is a data value
2064 // associated with it and put it in the event.
2065 if ( iItem >= 0 && iItem < GetItemCount() )
2066 {
2067 wxListItemInternalData *internaldata =
2068 wxGetInternalData(GetHwnd(), iItem);
2069
2070 if ( internaldata )
2071 event.m_item.m_data = internaldata->lParam;
2072 }
2073
2074 bool processed = true;
2075 switch ( nmhdr->code )
2076 {
2077 case LVN_BEGINRDRAG:
2078 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
2079 // fall through
2080
2081 case LVN_BEGINDRAG:
2082 if ( eventType == wxEVT_NULL )
2083 {
2084 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
2085 }
2086
2087 event.m_itemIndex = iItem;
2088 event.m_pointDrag.x = nmLV->ptAction.x;
2089 event.m_pointDrag.y = nmLV->ptAction.y;
2090 break;
2091
2092 // NB: we have to handle both *A and *W versions here because some
2093 // versions of comctl32.dll send ANSI messages even to the
2094 // Unicode windows
2095 case LVN_BEGINLABELEDITA:
2096 case LVN_BEGINLABELEDITW:
2097 {
2098 wxLV_ITEM item;
2099 if ( nmhdr->code == LVN_BEGINLABELEDITA )
2100 {
2101 item.Init(((LV_DISPINFOA *)lParam)->item);
2102 }
2103 else // LVN_BEGINLABELEDITW
2104 {
2105 item.Init(((LV_DISPINFOW *)lParam)->item);
2106 }
2107
2108 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
2109 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
2110 event.m_itemIndex = event.m_item.m_itemId;
2111 }
2112 break;
2113
2114 case LVN_ENDLABELEDITA:
2115 case LVN_ENDLABELEDITW:
2116 {
2117 wxLV_ITEM item;
2118 if ( nmhdr->code == LVN_ENDLABELEDITA )
2119 {
2120 item.Init(((LV_DISPINFOA *)lParam)->item);
2121 }
2122 else // LVN_ENDLABELEDITW
2123 {
2124 item.Init(((LV_DISPINFOW *)lParam)->item);
2125 }
2126
2127 // was editing cancelled?
2128 const LV_ITEM& lvi = (LV_ITEM)item;
2129 if ( !lvi.pszText || lvi.iItem == -1 )
2130 {
2131 // EDIT control will be deleted by the list control
2132 // itself so prevent us from deleting it as well
2133 DeleteEditControl();
2134
2135 event.SetEditCanceled(true);
2136 }
2137
2138 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
2139 wxConvertFromMSWListItem(NULL, event.m_item, item);
2140 event.m_itemIndex = event.m_item.m_itemId;
2141 }
2142 break;
2143
2144 case LVN_COLUMNCLICK:
2145 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
2146 event.m_itemIndex = -1;
2147 event.m_col = nmLV->iSubItem;
2148 break;
2149
2150 case LVN_DELETEALLITEMS:
2151 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
2152 event.m_itemIndex = -1;
2153 break;
2154
2155 case LVN_DELETEITEM:
2156 if ( m_count == 0 )
2157 {
2158 // this should be prevented by the post-processing code
2159 // below, but "just in case"
2160 return false;
2161 }
2162
2163 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
2164 event.m_itemIndex = iItem;
2165 // delete the assoicated internal data
2166 wxDeleteInternalData(this, iItem);
2167 break;
2168
2169 case LVN_INSERTITEM:
2170 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
2171 event.m_itemIndex = iItem;
2172 break;
2173
2174 case LVN_ITEMCHANGED:
2175 // we translate this catch all message into more interesting
2176 // (and more easy to process) wxWidgets events
2177
2178 // first of all, we deal with the state change events only and
2179 // only for valid items (item == -1 for the virtual list
2180 // control)
2181 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
2182 {
2183 // temp vars for readability
2184 const UINT stOld = nmLV->uOldState;
2185 const UINT stNew = nmLV->uNewState;
2186
2187 event.m_item.SetId(iItem);
2188 event.m_item.SetMask(wxLIST_MASK_TEXT |
2189 wxLIST_MASK_IMAGE |
2190 wxLIST_MASK_DATA);
2191 GetItem(event.m_item);
2192
2193 // has the focus changed?
2194 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2195 {
2196 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2197 event.m_itemIndex = iItem;
2198 }
2199
2200 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2201 {
2202 if ( eventType != wxEVT_NULL )
2203 {
2204 // focus and selection have both changed: send the
2205 // focus event from here and the selection one
2206 // below
2207 event.SetEventType(eventType);
2208 (void)HandleWindowEvent(event);
2209 }
2210 else // no focus event to send
2211 {
2212 // then need to set m_itemIndex as it wasn't done
2213 // above
2214 event.m_itemIndex = iItem;
2215 }
2216
2217 eventType = stNew & LVIS_SELECTED
2218 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2219 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2220 }
2221 }
2222
2223 if ( eventType == wxEVT_NULL )
2224 {
2225 // not an interesting event for us
2226 return false;
2227 }
2228
2229 break;
2230
2231 case LVN_KEYDOWN:
2232 {
2233 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2234 WORD wVKey = info->wVKey;
2235
2236 // get the current selection
2237 long lItem = GetNextItem(-1,
2238 wxLIST_NEXT_ALL,
2239 wxLIST_STATE_SELECTED);
2240
2241 // <Enter> or <Space> activate the selected item if any (but
2242 // not with Shift and/or Ctrl as then they have a predefined
2243 // meaning for the list view)
2244 if ( lItem != -1 &&
2245 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
2246 !(wxIsShiftDown() || wxIsCtrlDown()) )
2247 {
2248 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2249 }
2250 else
2251 {
2252 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
2253
2254 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2255 // value which should be used as is
2256 int code = wxCharCodeMSWToWX(wVKey);
2257 event.m_code = code ? code : wVKey;
2258 }
2259
2260 event.m_itemIndex =
2261 event.m_item.m_itemId = lItem;
2262
2263 if ( lItem != -1 )
2264 {
2265 // fill the other fields too
2266 event.m_item.m_text = GetItemText(lItem);
2267 event.m_item.m_data = GetItemData(lItem);
2268 }
2269 }
2270 break;
2271
2272 case NM_DBLCLK:
2273 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2274 // anything else
2275 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2276 {
2277 return true;
2278 }
2279
2280 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2281 // if it happened on an item (and not on empty place)
2282 if ( iItem == -1 )
2283 {
2284 // not on item
2285 return false;
2286 }
2287
2288 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2289 event.m_itemIndex = iItem;
2290 event.m_item.m_text = GetItemText(iItem);
2291 event.m_item.m_data = GetItemData(iItem);
2292 break;
2293
2294 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2295 case GN_CONTEXTMENU:
2296 #endif //__WXWINCE__
2297 case NM_RCLICK:
2298 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2299 // don't do anything else
2300 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2301 {
2302 return true;
2303 }
2304
2305 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2306 LV_HITTESTINFO lvhti;
2307 wxZeroMemory(lvhti);
2308
2309 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2310 if(nmhdr->code == GN_CONTEXTMENU) {
2311 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2312 } else
2313 #endif //__WXWINCE__
2314 ::GetCursorPos(&(lvhti.pt));
2315 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
2316 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
2317 {
2318 if ( lvhti.flags & LVHT_ONITEM )
2319 {
2320 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2321 event.m_itemIndex = lvhti.iItem;
2322 event.m_pointDrag.x = lvhti.pt.x;
2323 event.m_pointDrag.y = lvhti.pt.y;
2324 }
2325 }
2326 break;
2327
2328 #ifdef NM_CUSTOMDRAW
2329 case NM_CUSTOMDRAW:
2330 *result = OnCustomDraw(lParam);
2331
2332 return *result != CDRF_DODEFAULT;
2333 #endif // _WIN32_IE >= 0x300
2334
2335 case LVN_ODCACHEHINT:
2336 {
2337 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
2338
2339 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
2340
2341 // we get some really stupid cache hints like ones for
2342 // items in range 0..0 for an empty control or, after
2343 // deleting an item, for items in invalid range -- filter
2344 // this garbage out
2345 if ( cacheHint->iFrom > cacheHint->iTo )
2346 return false;
2347
2348 event.m_oldItemIndex = cacheHint->iFrom;
2349
2350 const long iMax = GetItemCount();
2351 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2352 : iMax - 1;
2353 }
2354 break;
2355
2356 #ifdef HAVE_NMLVFINDITEM
2357 case LVN_ODFINDITEM:
2358 // this message is only used with the virtual list control but
2359 // even there we don't want to always use it: in a control with
2360 // sufficiently big number of items (defined as > 1000 here),
2361 // accidentally pressing a key could result in hanging an
2362 // application waiting while it performs linear search
2363 if ( IsVirtual() && GetItemCount() <= 1000 )
2364 {
2365 NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
2366
2367 // no match by default
2368 *result = -1;
2369
2370 // we only handle string-based searches here
2371 //
2372 // TODO: what about LVFI_PARTIAL, should we handle this?
2373 if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
2374 {
2375 return false;
2376 }
2377
2378 const wxChar * const searchstr = pFindInfo->lvfi.psz;
2379 const size_t len = wxStrlen(searchstr);
2380
2381 // this is the first item we should examine, search from it
2382 // wrapping if necessary
2383 const int startPos = pFindInfo->iStart;
2384 const int maxPos = GetItemCount();
2385 wxCHECK_MSG( startPos <= maxPos, false,
2386 _T("bad starting position in LVN_ODFINDITEM") );
2387
2388 int currentPos = startPos;
2389 do
2390 {
2391 // wrap to the beginning if necessary
2392 if ( currentPos == maxPos )
2393 {
2394 // somewhat surprizingly, LVFI_WRAP isn't set in
2395 // flags but we still should wrap
2396 currentPos = 0;
2397 }
2398
2399 // does this item begin with searchstr?
2400 if ( wxStrnicmp(searchstr,
2401 GetItemText(currentPos), len) == 0 )
2402 {
2403 *result = currentPos;
2404 break;
2405 }
2406 }
2407 while ( ++currentPos != startPos );
2408
2409 if ( *result == -1 )
2410 {
2411 // not found
2412 return false;
2413 }
2414
2415 SetItemState(*result,
2416 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
2417 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
2418 EnsureVisible(*result);
2419 return true;
2420 }
2421 else
2422 {
2423 processed = false;
2424 }
2425 break;
2426 #endif // HAVE_NMLVFINDITEM
2427
2428 case LVN_GETDISPINFO:
2429 if ( IsVirtual() )
2430 {
2431 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2432
2433 LV_ITEM& lvi = info->item;
2434 long item = lvi.iItem;
2435
2436 if ( lvi.mask & LVIF_TEXT )
2437 {
2438 wxString text = OnGetItemText(item, lvi.iSubItem);
2439 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
2440 }
2441
2442 // see comment at the end of wxListCtrl::GetColumn()
2443 #ifdef NM_CUSTOMDRAW
2444 if ( lvi.mask & LVIF_IMAGE )
2445 {
2446 lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem);
2447 }
2448 #endif // NM_CUSTOMDRAW
2449
2450 // even though we never use LVM_SETCALLBACKMASK, we still
2451 // can get messages with LVIF_STATE in lvi.mask under Vista
2452 if ( lvi.mask & LVIF_STATE )
2453 {
2454 // we don't have anything to return from here...
2455 lvi.stateMask = 0;
2456 }
2457
2458 return true;
2459 }
2460 // fall through
2461
2462 default:
2463 processed = false;
2464 }
2465
2466 if ( !processed )
2467 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2468 }
2469 else
2470 {
2471 // where did this one come from?
2472 return false;
2473 }
2474
2475 // process the event
2476 // -----------------
2477
2478 event.SetEventType(eventType);
2479
2480 bool processed = HandleWindowEvent(event);
2481
2482 // post processing
2483 // ---------------
2484 switch ( nmhdr->code )
2485 {
2486 case LVN_DELETEALLITEMS:
2487 // always return true to suppress all additional LVN_DELETEITEM
2488 // notifications - this makes deleting all items from a list ctrl
2489 // much faster
2490 *result = TRUE;
2491
2492 // also, we may free all user data now (couldn't do it before as
2493 // the user should have access to it in OnDeleteAllItems() handler)
2494 FreeAllInternalData();
2495
2496 // the control is empty now, synchronize the cached number of items
2497 // with the real one
2498 m_count = 0;
2499 return true;
2500
2501 case LVN_ENDLABELEDITA:
2502 case LVN_ENDLABELEDITW:
2503 // logic here is inverted compared to all the other messages
2504 *result = event.IsAllowed();
2505
2506 // EDIT control will be deleted by the list control itself so
2507 // prevent us from deleting it as well
2508 DeleteEditControl();
2509
2510 return true;
2511 }
2512
2513 if ( processed )
2514 *result = !event.IsAllowed();
2515
2516 return processed;
2517 }
2518
2519 // ----------------------------------------------------------------------------
2520 // custom draw stuff
2521 // ----------------------------------------------------------------------------
2522
2523 // see comment at the end of wxListCtrl::GetColumn()
2524 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2525
2526 static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd)
2527 {
2528 RECT rc;
2529 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rc, LVIR_BOUNDS);
2530
2531 RECT rcIcon;
2532 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rcIcon, LVIR_ICON);
2533
2534 // exclude the icon part, neither the selection background nor focus rect
2535 // should cover it
2536 rc.left = rcIcon.right;
2537
2538 return rc;
2539 }
2540
2541 static
2542 bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont, int colCount)
2543 {
2544 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2545
2546 HDC hdc = nmcd.hdc;
2547 HWND hwndList = nmcd.hdr.hwndFrom;
2548 const int col = pLVCD->iSubItem;
2549 const DWORD item = nmcd.dwItemSpec;
2550
2551 // the font must be valid, otherwise we wouldn't be painting the item at all
2552 SelectInHDC selFont(hdc, hfont);
2553
2554 // get the rectangle to paint
2555 RECT rc;
2556 ListView_GetSubItemRect(hwndList, item, col, LVIR_BOUNDS, &rc);
2557 if ( !col && colCount > 1 )
2558 {
2559 // broken ListView_GetSubItemRect() returns the entire item rect for
2560 // 0th subitem while we really need just the part for this column
2561 RECT rc2;
2562 ListView_GetSubItemRect(hwndList, item, 1, LVIR_BOUNDS, &rc2);
2563
2564 rc.right = rc2.left;
2565 rc.left += 4;
2566 }
2567 else // not first subitem
2568 {
2569 rc.left += 6;
2570 }
2571
2572 // get the image and text to draw
2573 wxChar text[512];
2574 LV_ITEM it;
2575 wxZeroMemory(it);
2576 it.mask = LVIF_TEXT | LVIF_IMAGE;
2577 it.iItem = item;
2578 it.iSubItem = col;
2579 it.pszText = text;
2580 it.cchTextMax = WXSIZEOF(text);
2581 ListView_GetItem(hwndList, &it);
2582
2583 HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL);
2584 if ( himl && ImageList_GetImageCount(himl) )
2585 {
2586 if ( it.iImage != -1 )
2587 {
2588 ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top,
2589 nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED
2590 : ILD_TRANSPARENT);
2591 }
2592
2593 // notice that even if this item doesn't have any image, the list
2594 // control still leaves space for the image in the first column if the
2595 // image list is not empty (presumably so that items with and without
2596 // images align?)
2597 if ( it.iImage != -1 || it.iSubItem == 0 )
2598 {
2599 int wImage, hImage;
2600 ImageList_GetIconSize(himl, &wImage, &hImage);
2601
2602 rc.left += wImage + 2;
2603 }
2604 }
2605
2606 ::SetBkMode(hdc, TRANSPARENT);
2607
2608 UINT fmt = DT_SINGLELINE |
2609 #ifndef __WXWINCE__
2610 DT_WORD_ELLIPSIS |
2611 #endif // __WXWINCE__
2612 DT_NOPREFIX |
2613 DT_VCENTER;
2614
2615 LV_COLUMN lvCol;
2616 wxZeroMemory(lvCol);
2617 lvCol.mask = LVCF_FMT;
2618 if ( ListView_GetColumn(hwndList, col, &lvCol) )
2619 {
2620 switch ( lvCol.fmt & LVCFMT_JUSTIFYMASK )
2621 {
2622 case LVCFMT_LEFT:
2623 fmt |= DT_LEFT;
2624 break;
2625
2626 case LVCFMT_CENTER:
2627 fmt |= DT_CENTER;
2628 break;
2629
2630 case LVCFMT_RIGHT:
2631 fmt |= DT_RIGHT;
2632 break;
2633 }
2634 }
2635 //else: failed to get alignment, assume it's DT_LEFT (default)
2636
2637 DrawText(hdc, text, -1, &rc, fmt);
2638
2639 return true;
2640 }
2641
2642 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd)
2643 {
2644 if ( nmcd.uItemState & CDIS_FOCUS )
2645 {
2646 RECT rc = GetCustomDrawnItemRect(nmcd);
2647
2648 // don't use the provided HDC, it's in some strange state by now
2649 ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc);
2650 }
2651 }
2652
2653 // pLVCD->clrText and clrTextBk should contain the colours to use
2654 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2655 {
2656 NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut
2657
2658 const HWND hwndList = nmcd.hdr.hwndFrom;
2659 const int item = nmcd.dwItemSpec;
2660
2661 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2662 // the item is not at all selected for some reason (comctl32 6), but we
2663 // also can't always trust ListView_GetItem() as it could return the old
2664 // item status if we're called just after the (de)selection, so remember
2665 // the last item to gain selection and also check for it here
2666 for ( int i = -1;; )
2667 {
2668 i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED);
2669 if ( i == -1 )
2670 {
2671 nmcd.uItemState &= ~CDIS_SELECTED;
2672 break;
2673 }
2674
2675 if ( i == item )
2676 {
2677 nmcd.uItemState |= CDIS_SELECTED;
2678 break;
2679 }
2680 }
2681
2682 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2683 if ( ::GetFocus() == hwndList &&
2684 ListView_GetNextItem(hwndList, (WPARAM)-1, LVNI_FOCUSED) == item )
2685 {
2686 nmcd.uItemState |= CDIS_FOCUS;
2687 }
2688 else
2689 {
2690 nmcd.uItemState &= ~CDIS_FOCUS;
2691 }
2692
2693 if ( nmcd.uItemState & CDIS_SELECTED )
2694 {
2695 int syscolFg, syscolBg;
2696 if ( ::GetFocus() == hwndList )
2697 {
2698 syscolFg = COLOR_HIGHLIGHTTEXT;
2699 syscolBg = COLOR_HIGHLIGHT;
2700 }
2701 else // selected but unfocused
2702 {
2703 syscolFg = COLOR_WINDOWTEXT;
2704 syscolBg = COLOR_BTNFACE;
2705
2706 // don't grey out the icon in this case neither
2707 nmcd.uItemState &= ~CDIS_SELECTED;
2708 }
2709
2710 pLVCD->clrText = ::GetSysColor(syscolFg);
2711 pLVCD->clrTextBk = ::GetSysColor(syscolBg);
2712 }
2713 //else: not selected, use normal colours from pLVCD
2714
2715 HDC hdc = nmcd.hdc;
2716 RECT rc = GetCustomDrawnItemRect(nmcd);
2717
2718 ::SetTextColor(hdc, pLVCD->clrText);
2719 ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk));
2720
2721 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2722 // problems so just draw everything except the focus rect from here instead
2723 const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList));
2724 for ( int col = 0; col < colCount; col++ )
2725 {
2726 pLVCD->iSubItem = col;
2727 HandleSubItemPrepaint(pLVCD, hfont, colCount);
2728 }
2729
2730 HandleItemPostpaint(nmcd);
2731 }
2732
2733 static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl,
2734 LPNMLVCUSTOMDRAW pLVCD,
2735 wxListItemAttr *attr)
2736 {
2737 if ( !attr )
2738 {
2739 // nothing to do for this item
2740 return CDRF_DODEFAULT;
2741 }
2742
2743
2744 // set the colours to use for text drawing
2745 pLVCD->clrText = attr->HasTextColour()
2746 ? wxColourToRGB(attr->GetTextColour())
2747 : wxColourToRGB(listctrl->GetTextColour());
2748 pLVCD->clrTextBk = attr->HasBackgroundColour()
2749 ? wxColourToRGB(attr->GetBackgroundColour())
2750 : wxColourToRGB(listctrl->GetBackgroundColour());
2751
2752 // select the font if non default one is specified
2753 if ( attr->HasFont() )
2754 {
2755 wxFont font = attr->GetFont();
2756 if ( font.GetEncoding() != wxFONTENCODING_SYSTEM )
2757 {
2758 // the standard control ignores the font encoding/charset, at least
2759 // with recent comctl32.dll versions (5 and 6, it uses to work with
2760 // 4.something) so we have to draw the item entirely ourselves in
2761 // this case
2762 HandleItemPaint(pLVCD, GetHfontOf(font));
2763 return CDRF_SKIPDEFAULT;
2764 }
2765
2766 ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font));
2767
2768 return CDRF_NEWFONT;
2769 }
2770
2771 return CDRF_DODEFAULT;
2772 }
2773
2774 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2775 {
2776 LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam;
2777 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2778 switch ( nmcd.dwDrawStage )
2779 {
2780 case CDDS_PREPAINT:
2781 // if we've got any items with non standard attributes,
2782 // notify us before painting each item
2783 //
2784 // for virtual controls, always suppose that we have attributes as
2785 // there is no way to check for this
2786 if ( IsVirtual() || m_hasAnyAttr )
2787 return CDRF_NOTIFYITEMDRAW;
2788 break;
2789
2790 case CDDS_ITEMPREPAINT:
2791 const int item = nmcd.dwItemSpec;
2792
2793 // we get this message with item == 0 for an empty control, we
2794 // must ignore it as calling OnGetItemAttr() would be wrong
2795 if ( item < 0 || item >= GetItemCount() )
2796 break;
2797
2798 return HandleItemPrepaint(this, pLVCD, DoGetItemAttr(item));
2799 }
2800
2801 return CDRF_DODEFAULT;
2802 }
2803
2804 #endif // NM_CUSTOMDRAW supported
2805
2806 // Necessary for drawing hrules and vrules, if specified
2807 void wxListCtrl::OnPaint(wxPaintEvent& event)
2808 {
2809 bool drawHRules = HasFlag(wxLC_HRULES);
2810 bool drawVRules = HasFlag(wxLC_VRULES);
2811
2812 if (!InReportView() || !drawHRules && !drawVRules)
2813 {
2814 event.Skip();
2815 return;
2816 }
2817
2818 wxPaintDC dc(this);
2819
2820 wxControl::OnPaint(event);
2821
2822 // Reset the device origin since it may have been set
2823 dc.SetDeviceOrigin(0, 0);
2824
2825 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT));
2826 dc.SetPen(pen);
2827 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2828
2829 wxSize clientSize = GetClientSize();
2830 wxRect itemRect;
2831
2832 int itemCount = GetItemCount();
2833 int i;
2834 if (drawHRules)
2835 {
2836 long top = GetTopItem();
2837 for (i = top; i < top + GetCountPerPage() + 1; i++)
2838 {
2839 if (GetItemRect(i, itemRect))
2840 {
2841 int cy = itemRect.GetTop();
2842 if (i != 0) // Don't draw the first one
2843 {
2844 dc.DrawLine(0, cy, clientSize.x, cy);
2845 }
2846 // Draw last line
2847 if (i == itemCount - 1)
2848 {
2849 cy = itemRect.GetBottom();
2850 dc.DrawLine(0, cy, clientSize.x, cy);
2851 }
2852 }
2853 }
2854 }
2855 i = itemCount - 1;
2856 if (drawVRules && (i > -1))
2857 {
2858 wxRect firstItemRect;
2859 GetItemRect(0, firstItemRect);
2860
2861 if (GetItemRect(i, itemRect))
2862 {
2863 // this is a fix for bug 673394: erase the pixels which we would
2864 // otherwise leave on the screen
2865 static const int gap = 2;
2866 dc.SetPen(*wxTRANSPARENT_PEN);
2867 dc.SetBrush(wxBrush(GetBackgroundColour()));
2868 dc.DrawRectangle(0, firstItemRect.GetY() - gap,
2869 clientSize.GetWidth(), gap);
2870
2871 dc.SetPen(pen);
2872 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2873
2874 int numCols = GetColumnCount();
2875 int* indexArray = new int[numCols];
2876 if ( !ListView_GetColumnOrderArray( GetHwnd(), numCols, indexArray) )
2877 {
2878 wxFAIL_MSG( _T("invalid column index array in OnPaint()") );
2879 }
2880
2881 int x = itemRect.GetX();
2882 for (int col = 0; col < numCols; col++)
2883 {
2884 int colWidth = GetColumnWidth(indexArray[col]);
2885 x += colWidth ;
2886 dc.DrawLine(x-1, firstItemRect.GetY() - gap,
2887 x-1, itemRect.GetBottom());
2888 }
2889
2890 delete indexArray;
2891 }
2892 }
2893 }
2894
2895 WXLRESULT
2896 wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2897 {
2898 switch ( nMsg )
2899 {
2900 #ifdef WM_PRINT
2901 case WM_PRINT:
2902 // we should bypass our own WM_PRINT handling as we don't handle
2903 // PRF_CHILDREN flag, so leave it to the native control itself
2904 return MSWDefWindowProc(nMsg, wParam, lParam);
2905 #endif // WM_PRINT
2906
2907 case WM_CONTEXTMENU:
2908 // because this message is propagated upwards the child-parent
2909 // chain, we get it for the right clicks on the header window but
2910 // this is confusing in wx as right clicking there already
2911 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2912 // so just ignore them
2913 if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) )
2914 return 0;
2915 //else: break
2916 }
2917
2918 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
2919 }
2920
2921 // ----------------------------------------------------------------------------
2922 // virtual list controls
2923 // ----------------------------------------------------------------------------
2924
2925 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2926 {
2927 // this is a pure virtual function, in fact - which is not really pure
2928 // because the controls which are not virtual don't need to implement it
2929 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2930
2931 return wxEmptyString;
2932 }
2933
2934 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2935 {
2936 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2937 -1,
2938 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2939 return -1;
2940 }
2941
2942 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2943 {
2944 if (!column)
2945 return OnGetItemImage(item);
2946
2947 return -1;
2948 }
2949
2950 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2951 {
2952 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2953 _T("invalid item index in OnGetItemAttr()") );
2954
2955 // no attributes by default
2956 return NULL;
2957 }
2958
2959 wxListItemAttr *wxListCtrl::DoGetItemAttr(long item) const
2960 {
2961 return IsVirtual() ? OnGetItemAttr(item)
2962 : wxGetInternalDataAttr(this, item);
2963 }
2964
2965 void wxListCtrl::SetItemCount(long count)
2966 {
2967 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2968
2969 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2970 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2971 {
2972 wxLogLastError(_T("ListView_SetItemCount"));
2973 }
2974 m_count = count;
2975 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2976 wxT("m_count should match ListView_GetItemCount"));
2977 }
2978
2979 void wxListCtrl::RefreshItem(long item)
2980 {
2981 RefreshItems(item, item);
2982 }
2983
2984 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2985 {
2986 ListView_RedrawItems(GetHwnd(), itemFrom, itemTo);
2987 }
2988
2989 // ----------------------------------------------------------------------------
2990 // internal data stuff
2991 // ----------------------------------------------------------------------------
2992
2993 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2994 {
2995 LV_ITEM it;
2996 it.mask = LVIF_PARAM;
2997 it.iItem = itemId;
2998
2999 if ( !ListView_GetItem(hwnd, &it) )
3000 return NULL;
3001
3002 return (wxListItemInternalData *) it.lParam;
3003 }
3004
3005 static
3006 wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
3007 {
3008 return wxGetInternalData(GetHwndOf(ctl), itemId);
3009 }
3010
3011 static
3012 wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId)
3013 {
3014 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
3015
3016 return data ? data->attr : NULL;
3017 }
3018
3019 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
3020 {
3021 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
3022 if (data)
3023 {
3024 LV_ITEM item;
3025 memset(&item, 0, sizeof(item));
3026 item.iItem = itemId;
3027 item.mask = LVIF_PARAM;
3028 item.lParam = (LPARAM) 0;
3029 ListView_SetItem((HWND)ctl->GetHWND(), &item);
3030 delete data;
3031 }
3032 }
3033
3034 // ----------------------------------------------------------------------------
3035 // wxWin <-> MSW items conversions
3036 // ----------------------------------------------------------------------------
3037
3038 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
3039 wxListItem& info,
3040 LV_ITEM& lvItem)
3041 {
3042 wxListItemInternalData *internaldata =
3043 (wxListItemInternalData *) lvItem.lParam;
3044
3045 if (internaldata)
3046 info.m_data = internaldata->lParam;
3047
3048 info.m_mask = 0;
3049 info.m_state = 0;
3050 info.m_stateMask = 0;
3051 info.m_itemId = lvItem.iItem;
3052
3053 long oldMask = lvItem.mask;
3054
3055 bool needText = false;
3056 if (hwndListCtrl != 0)
3057 {
3058 if ( lvItem.mask & LVIF_TEXT )
3059 needText = false;
3060 else
3061 needText = true;
3062
3063 if ( needText )
3064 {
3065 lvItem.pszText = new wxChar[513];
3066 lvItem.cchTextMax = 512;
3067 }
3068 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
3069 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
3070 }
3071
3072 if ( lvItem.mask & LVIF_STATE )
3073 {
3074 info.m_mask |= wxLIST_MASK_STATE;
3075
3076 if ( lvItem.stateMask & LVIS_CUT)
3077 {
3078 info.m_stateMask |= wxLIST_STATE_CUT;
3079 if ( lvItem.state & LVIS_CUT )
3080 info.m_state |= wxLIST_STATE_CUT;
3081 }
3082 if ( lvItem.stateMask & LVIS_DROPHILITED)
3083 {
3084 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
3085 if ( lvItem.state & LVIS_DROPHILITED )
3086 info.m_state |= wxLIST_STATE_DROPHILITED;
3087 }
3088 if ( lvItem.stateMask & LVIS_FOCUSED)
3089 {
3090 info.m_stateMask |= wxLIST_STATE_FOCUSED;
3091 if ( lvItem.state & LVIS_FOCUSED )
3092 info.m_state |= wxLIST_STATE_FOCUSED;
3093 }
3094 if ( lvItem.stateMask & LVIS_SELECTED)
3095 {
3096 info.m_stateMask |= wxLIST_STATE_SELECTED;
3097 if ( lvItem.state & LVIS_SELECTED )
3098 info.m_state |= wxLIST_STATE_SELECTED;
3099 }
3100 }
3101
3102 if ( lvItem.mask & LVIF_TEXT )
3103 {
3104 info.m_mask |= wxLIST_MASK_TEXT;
3105 info.m_text = lvItem.pszText;
3106 }
3107 if ( lvItem.mask & LVIF_IMAGE )
3108 {
3109 info.m_mask |= wxLIST_MASK_IMAGE;
3110 info.m_image = lvItem.iImage;
3111 }
3112 if ( lvItem.mask & LVIF_PARAM )
3113 info.m_mask |= wxLIST_MASK_DATA;
3114 if ( lvItem.mask & LVIF_DI_SETITEM )
3115 info.m_mask |= wxLIST_SET_ITEM;
3116 info.m_col = lvItem.iSubItem;
3117
3118 if (needText)
3119 {
3120 if (lvItem.pszText)
3121 delete[] lvItem.pszText;
3122 }
3123 lvItem.mask = oldMask;
3124 }
3125
3126 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
3127 {
3128 if (stateMask & wxLIST_STATE_CUT)
3129 {
3130 lvItem.stateMask |= LVIS_CUT;
3131 if (state & wxLIST_STATE_CUT)
3132 lvItem.state |= LVIS_CUT;
3133 }
3134 if (stateMask & wxLIST_STATE_DROPHILITED)
3135 {
3136 lvItem.stateMask |= LVIS_DROPHILITED;
3137 if (state & wxLIST_STATE_DROPHILITED)
3138 lvItem.state |= LVIS_DROPHILITED;
3139 }
3140 if (stateMask & wxLIST_STATE_FOCUSED)
3141 {
3142 lvItem.stateMask |= LVIS_FOCUSED;
3143 if (state & wxLIST_STATE_FOCUSED)
3144 lvItem.state |= LVIS_FOCUSED;
3145 }
3146 if (stateMask & wxLIST_STATE_SELECTED)
3147 {
3148 lvItem.stateMask |= LVIS_SELECTED;
3149 if (state & wxLIST_STATE_SELECTED)
3150 lvItem.state |= LVIS_SELECTED;
3151 }
3152 }
3153
3154 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
3155 const wxListItem& info,
3156 LV_ITEM& lvItem)
3157 {
3158 lvItem.iItem = (int) info.m_itemId;
3159
3160 lvItem.iImage = info.m_image;
3161 lvItem.stateMask = 0;
3162 lvItem.state = 0;
3163 lvItem.mask = 0;
3164 lvItem.iSubItem = info.m_col;
3165
3166 if (info.m_mask & wxLIST_MASK_STATE)
3167 {
3168 lvItem.mask |= LVIF_STATE;
3169
3170 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
3171 }
3172
3173 if (info.m_mask & wxLIST_MASK_TEXT)
3174 {
3175 lvItem.mask |= LVIF_TEXT;
3176 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
3177 {
3178 lvItem.pszText = LPSTR_TEXTCALLBACK;
3179 }
3180 else
3181 {
3182 // pszText is not const, hence the cast
3183 lvItem.pszText = (wxChar *)info.m_text.wx_str();
3184 if ( lvItem.pszText )
3185 lvItem.cchTextMax = info.m_text.length();
3186 else
3187 lvItem.cchTextMax = 0;
3188 }
3189 }
3190 if (info.m_mask & wxLIST_MASK_IMAGE)
3191 lvItem.mask |= LVIF_IMAGE;
3192 }
3193
3194 static void wxConvertToMSWListCol(HWND hwndList,
3195 int col,
3196 const wxListItem& item,
3197 LV_COLUMN& lvCol)
3198 {
3199 wxZeroMemory(lvCol);
3200
3201 if ( item.m_mask & wxLIST_MASK_TEXT )
3202 {
3203 lvCol.mask |= LVCF_TEXT;
3204 lvCol.pszText = (wxChar *)item.m_text.wx_str(); // cast is safe
3205 }
3206
3207 if ( item.m_mask & wxLIST_MASK_FORMAT )
3208 {
3209 lvCol.mask |= LVCF_FMT;
3210
3211 if ( item.m_format == wxLIST_FORMAT_LEFT )
3212 lvCol.fmt = LVCFMT_LEFT;
3213 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
3214 lvCol.fmt = LVCFMT_RIGHT;
3215 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
3216 lvCol.fmt = LVCFMT_CENTER;
3217 }
3218
3219 if ( item.m_mask & wxLIST_MASK_WIDTH )
3220 {
3221 lvCol.mask |= LVCF_WIDTH;
3222 if ( item.m_width == wxLIST_AUTOSIZE)
3223 lvCol.cx = LVSCW_AUTOSIZE;
3224 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3225 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
3226 else
3227 lvCol.cx = item.m_width;
3228 }
3229
3230 // see comment at the end of wxListCtrl::GetColumn()
3231 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3232 if ( item.m_mask & wxLIST_MASK_IMAGE )
3233 {
3234 if ( wxApp::GetComCtl32Version() >= 470 )
3235 {
3236 lvCol.mask |= LVCF_IMAGE;
3237
3238 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3239 // seem to be generally nicer than on the left and the generic
3240 // version only draws them on the right (we don't have a flag to
3241 // specify the image location anyhow)
3242 //
3243 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3244 // make any difference in my tests -- but maybe we should?
3245 if ( item.m_image != -1 )
3246 {
3247 // as we're going to overwrite the format field, get its
3248 // current value first -- unless we want to overwrite it anyhow
3249 if ( !(lvCol.mask & LVCF_FMT) )
3250 {
3251 LV_COLUMN lvColOld;
3252 wxZeroMemory(lvColOld);
3253 lvColOld.mask = LVCF_FMT;
3254 if ( ListView_GetColumn(hwndList, col, &lvColOld) )
3255 {
3256 lvCol.fmt = lvColOld.fmt;
3257 }
3258
3259 lvCol.mask |= LVCF_FMT;
3260 }
3261
3262 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
3263 }
3264
3265 lvCol.iImage = item.m_image;
3266 }
3267 //else: it doesn't support item images anyhow
3268 }
3269 #endif // _WIN32_IE >= 0x0300
3270 }
3271
3272 #endif // wxUSE_LISTCTRL