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