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