]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
corrections for buffers
[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 // and remember that we have some now...
1570 m_hasAnyAttr = TRUE;
1571 }
1572 };
1573
1574 long rv = ListView_InsertItem(GetHwnd(), & item);
1575
1576 m_count++;
1577 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1578 wxT("m_count should match ListView_GetItemCount"));
1579
1580 return rv;
1581 }
1582
1583 long wxListCtrl::InsertItem(long index, const wxString& label)
1584 {
1585 wxListItem info;
1586 info.m_text = label;
1587 info.m_mask = wxLIST_MASK_TEXT;
1588 info.m_itemId = index;
1589 return InsertItem(info);
1590 }
1591
1592 // Inserts an image item
1593 long wxListCtrl::InsertItem(long index, int imageIndex)
1594 {
1595 wxListItem info;
1596 info.m_image = imageIndex;
1597 info.m_mask = wxLIST_MASK_IMAGE;
1598 info.m_itemId = index;
1599 return InsertItem(info);
1600 }
1601
1602 // Inserts an image/string item
1603 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1604 {
1605 wxListItem info;
1606 info.m_image = imageIndex;
1607 info.m_text = label;
1608 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1609 info.m_itemId = index;
1610 return InsertItem(info);
1611 }
1612
1613 // For list view mode (only), inserts a column.
1614 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1615 {
1616 LV_COLUMN lvCol;
1617 wxConvertToMSWListCol(col, item, lvCol);
1618
1619 if ( !(lvCol.mask & LVCF_WIDTH) )
1620 {
1621 // always give some width to the new column: this one is compatible
1622 // with the generic version
1623 lvCol.mask |= LVCF_WIDTH;
1624 lvCol.cx = 80;
1625 }
1626
1627 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1628 if ( n != -1 )
1629 {
1630 m_colCount++;
1631 }
1632 else // failed to insert?
1633 {
1634 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1635 lvCol.pszText);
1636 }
1637
1638 return n;
1639 }
1640
1641 long wxListCtrl::InsertColumn(long col,
1642 const wxString& heading,
1643 int format,
1644 int width)
1645 {
1646 wxListItem item;
1647 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1648 item.m_text = heading;
1649 if ( width > -1 )
1650 {
1651 item.m_mask |= wxLIST_MASK_WIDTH;
1652 item.m_width = width;
1653 }
1654 item.m_format = format;
1655
1656 return InsertColumn(col, item);
1657 }
1658
1659 // scroll the control by the given number of pixels (exception: in list view,
1660 // dx is interpreted as number of columns)
1661 bool wxListCtrl::ScrollList(int dx, int dy)
1662 {
1663 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1664 {
1665 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1666
1667 return FALSE;
1668 }
1669
1670 return TRUE;
1671 }
1672
1673 // Sort items.
1674
1675 // fn is a function which takes 3 long arguments: item1, item2, data.
1676 // item1 is the long data associated with a first item (NOT the index).
1677 // item2 is the long data associated with a second item (NOT the index).
1678 // data is the same value as passed to SortItems.
1679 // The return value is a negative number if the first item should precede the second
1680 // item, a positive number of the second item should precede the first,
1681 // or zero if the two items are equivalent.
1682
1683 // data is arbitrary data to be passed to the sort function.
1684
1685 // Internal structures for proxying the user compare function
1686 // so that we can pass it the *real* user data
1687
1688 // translate lParam data and call user func
1689 struct wxInternalDataSort
1690 {
1691 wxListCtrlCompare user_fn;
1692 long data;
1693 };
1694
1695 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1696 {
1697 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1698
1699 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1700 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1701
1702 long d1 = (data1 == NULL ? 0 : data1->lParam);
1703 long d2 = (data2 == NULL ? 0 : data2->lParam);
1704
1705 return internalData->user_fn(d1, d2, internalData->data);
1706
1707 };
1708
1709 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1710 {
1711 struct wxInternalDataSort internalData;
1712 internalData.user_fn = fn;
1713 internalData.data = data;
1714
1715 // WPARAM cast is needed for mingw/cygwin
1716 if ( !ListView_SortItems(GetHwnd(),
1717 wxInternalDataCompareFunc,
1718 (WPARAM) &internalData) )
1719 {
1720 wxLogDebug(_T("ListView_SortItems() failed"));
1721
1722 return FALSE;
1723 }
1724
1725 return TRUE;
1726 }
1727
1728
1729
1730 // ----------------------------------------------------------------------------
1731 // message processing
1732 // ----------------------------------------------------------------------------
1733
1734 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1735 {
1736 if (cmd == EN_UPDATE)
1737 {
1738 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1739 event.SetEventObject( this );
1740 ProcessCommand(event);
1741 return TRUE;
1742 }
1743 else if (cmd == EN_KILLFOCUS)
1744 {
1745 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1746 event.SetEventObject( this );
1747 ProcessCommand(event);
1748 return TRUE;
1749 }
1750 else
1751 return FALSE;
1752 }
1753
1754 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1755 {
1756
1757 // prepare the event
1758 // -----------------
1759
1760 wxListEvent event(wxEVT_NULL, m_windowId);
1761 event.SetEventObject(this);
1762
1763 wxEventType eventType = wxEVT_NULL;
1764
1765 NMHDR *nmhdr = (NMHDR *)lParam;
1766
1767 // if your compiler is as broken as this, you should really change it: this
1768 // code is needed for normal operation! #ifdef below is only useful for
1769 // automatic rebuilds which are done with a very old compiler version
1770 #ifdef HDN_BEGINTRACKA
1771
1772 // check for messages from the header (in report view)
1773 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1774
1775 // is it a message from the header?
1776 if ( nmhdr->hwndFrom == hwndHdr )
1777 {
1778 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1779
1780 event.m_itemIndex = -1;
1781
1782 switch ( nmhdr->code )
1783 {
1784 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1785 // TRACK messages even to ANSI programs: on my system I get
1786 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1787 //
1788 // work around is to simply catch both versions and hope that it
1789 // works (why should this message exist in ANSI and Unicode is
1790 // beyond me as it doesn't deal with strings at all...)
1791 //
1792 // note that fr HDN_TRACK another possibility could be to use
1793 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1794 // something other than the item width changes so we'd have to
1795 // filter out the unwanted events then
1796 case HDN_BEGINTRACKA:
1797 case HDN_BEGINTRACKW:
1798 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1799 // fall through
1800
1801 case HDN_TRACKA:
1802 case HDN_TRACKW:
1803 if ( eventType == wxEVT_NULL )
1804 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1805 // fall through
1806
1807 case HDN_ENDTRACKA:
1808 case HDN_ENDTRACKW:
1809 if ( eventType == wxEVT_NULL )
1810 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1811
1812 event.m_item.m_width = nmHDR->pitem->cxy;
1813 event.m_col = nmHDR->iItem;
1814 break;
1815
1816 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1817 case GN_CONTEXTMENU:
1818 #endif //__WXWINCE__
1819 case NM_RCLICK:
1820 {
1821 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1822 event.m_col = -1;
1823
1824 // find the column clicked: we have to search for it
1825 // ourselves as the notification message doesn't provide
1826 // this info
1827
1828 // where did the click occur?
1829 POINT ptClick;
1830 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1831 if(nmhdr->code == GN_CONTEXTMENU) {
1832 ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1833 } else
1834 #endif //__WXWINCE__
1835 if ( !::GetCursorPos(&ptClick) )
1836 {
1837 wxLogLastError(_T("GetCursorPos"));
1838 }
1839
1840 if ( !::ScreenToClient(hwndHdr, &ptClick) )
1841 {
1842 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1843 }
1844
1845 event.m_pointDrag.x = ptClick.x;
1846 event.m_pointDrag.y = ptClick.y;
1847
1848 int colCount = Header_GetItemCount(hwndHdr);
1849
1850 RECT rect;
1851 for ( int col = 0; col < colCount; col++ )
1852 {
1853 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1854 {
1855 if ( ::PtInRect(&rect, ptClick) )
1856 {
1857 event.m_col = col;
1858 break;
1859 }
1860 }
1861 }
1862 }
1863 break;
1864
1865 case HDN_GETDISPINFOW:
1866 {
1867 LPNMHDDISPINFOW info = (LPNMHDDISPINFOW) lParam;
1868 // This is a fix for a strange bug under XP.
1869 // Normally, info->iItem is a valid index, but
1870 // sometimes this is a silly (large) number
1871 // and when we return FALSE via wxControl::MSWOnNotify
1872 // to indicate that it hasn't yet been processed,
1873 // there's a GPF in Windows.
1874 // By returning TRUE here, we avoid further processing
1875 // of this strange message.
1876 if ( info->iItem >= GetColumnCount() )
1877 return TRUE;
1878 }
1879 // fall through
1880
1881 default:
1882 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1883 }
1884 }
1885 else
1886 #endif // defined(HDN_BEGINTRACKA)
1887 if ( nmhdr->hwndFrom == GetHwnd() )
1888 {
1889 // almost all messages use NM_LISTVIEW
1890 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1891
1892 const int iItem = nmLV->iItem;
1893
1894
1895 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1896 // ignored for efficiency. It is done here because the internal data is in the
1897 // process of being deleted so we don't want to try and access it below.
1898 if ( m_ignoreChangeMessages &&
1899 ( (nmLV->hdr.code == LVN_ITEMCHANGED) || (nmLV->hdr.code == LVN_ITEMCHANGING)))
1900 {
1901 return TRUE;
1902 }
1903
1904
1905 // If we have a valid item then check if there is a data value
1906 // associated with it and put it in the event.
1907 if ( iItem >= 0 && iItem < GetItemCount() )
1908 {
1909 wxListItemInternalData *internaldata =
1910 wxGetInternalData(GetHwnd(), iItem);
1911
1912 if ( internaldata )
1913 event.m_item.m_data = internaldata->lParam;
1914 }
1915
1916
1917 switch ( nmhdr->code )
1918 {
1919 case LVN_BEGINRDRAG:
1920 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1921 // fall through
1922
1923 case LVN_BEGINDRAG:
1924 if ( eventType == wxEVT_NULL )
1925 {
1926 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1927 }
1928
1929 event.m_itemIndex = iItem;
1930 event.m_pointDrag.x = nmLV->ptAction.x;
1931 event.m_pointDrag.y = nmLV->ptAction.y;
1932 break;
1933
1934 // NB: we have to handle both *A and *W versions here because some
1935 // versions of comctl32.dll send ANSI messages even to the
1936 // Unicode windows
1937 case LVN_BEGINLABELEDITA:
1938 case LVN_BEGINLABELEDITW:
1939 {
1940 wxLV_ITEM item;
1941 if ( nmhdr->code == LVN_BEGINLABELEDITA )
1942 {
1943 item.Init(((LV_DISPINFOA *)lParam)->item);
1944 }
1945 else // LVN_BEGINLABELEDITW
1946 {
1947 item.Init(((LV_DISPINFOW *)lParam)->item);
1948 }
1949
1950 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1951 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1952 event.m_itemIndex = event.m_item.m_itemId;
1953 }
1954 break;
1955
1956 case LVN_ENDLABELEDITA:
1957 case LVN_ENDLABELEDITW:
1958 {
1959 wxLV_ITEM item;
1960 if ( nmhdr->code == LVN_ENDLABELEDITA )
1961 {
1962 item.Init(((LV_DISPINFOA *)lParam)->item);
1963 }
1964 else // LVN_ENDLABELEDITW
1965 {
1966 item.Init(((LV_DISPINFOW *)lParam)->item);
1967 }
1968
1969 // was editing cancelled?
1970 const LV_ITEM& lvi = (LV_ITEM)item;
1971 if ( !lvi.pszText || lvi.iItem == -1 )
1972 {
1973 // don't keep a stale wxTextCtrl around
1974 if ( m_textCtrl )
1975 {
1976 // EDIT control will be deleted by the list control itself so
1977 // prevent us from deleting it as well
1978 m_textCtrl->UnsubclassWin();
1979 m_textCtrl->SetHWND(0);
1980 delete m_textCtrl;
1981 m_textCtrl = NULL;
1982 }
1983
1984 event.SetEditCanceled(true);
1985 }
1986
1987 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1988 wxConvertFromMSWListItem(NULL, event.m_item, item);
1989 event.m_itemIndex = event.m_item.m_itemId;
1990 }
1991 break;
1992
1993 case LVN_COLUMNCLICK:
1994 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1995 event.m_itemIndex = -1;
1996 event.m_col = nmLV->iSubItem;
1997 break;
1998
1999 case LVN_DELETEALLITEMS:
2000 m_count = 0;
2001 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
2002 event.m_itemIndex = -1;
2003 break;
2004
2005 case LVN_DELETEITEM:
2006 if (m_count == 0)
2007 // this should be prevented by the post-processing code below,
2008 // but "just in case"
2009 return FALSE;
2010
2011 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
2012 event.m_itemIndex = iItem;
2013 // delete the assoicated internal data
2014 wxDeleteInternalData(this, iItem);
2015 break;
2016
2017 case LVN_SETDISPINFO:
2018 {
2019 eventType = wxEVT_COMMAND_LIST_SET_INFO;
2020 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2021 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
2022 }
2023 break;
2024
2025 case LVN_INSERTITEM:
2026 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
2027 event.m_itemIndex = iItem;
2028 break;
2029
2030 case LVN_ITEMCHANGED:
2031 // we translate this catch all message into more interesting
2032 // (and more easy to process) wxWindows events
2033
2034 // first of all, we deal with the state change events only and
2035 // only for valid items (item == -1 for the virtual list
2036 // control)
2037 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
2038 {
2039 // temp vars for readability
2040 const UINT stOld = nmLV->uOldState;
2041 const UINT stNew = nmLV->uNewState;
2042
2043 event.m_item.SetId(iItem);
2044 event.m_item.SetMask(wxLIST_MASK_TEXT |
2045 wxLIST_MASK_IMAGE |
2046 wxLIST_MASK_DATA);
2047 GetItem(event.m_item);
2048
2049 // has the focus changed?
2050 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2051 {
2052 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2053 event.m_itemIndex = iItem;
2054 }
2055
2056 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2057 {
2058 if ( eventType != wxEVT_NULL )
2059 {
2060 // focus and selection have both changed: send the
2061 // focus event from here and the selection one
2062 // below
2063 event.SetEventType(eventType);
2064 (void)GetEventHandler()->ProcessEvent(event);
2065 }
2066 else // no focus event to send
2067 {
2068 // then need to set m_itemIndex as it wasn't done
2069 // above
2070 event.m_itemIndex = iItem;
2071 }
2072
2073 eventType = stNew & LVIS_SELECTED
2074 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2075 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2076 }
2077 }
2078
2079 if ( eventType == wxEVT_NULL )
2080 {
2081 // not an interesting event for us
2082 return FALSE;
2083 }
2084
2085 break;
2086
2087 case LVN_KEYDOWN:
2088 {
2089 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2090 WORD wVKey = info->wVKey;
2091
2092 // get the current selection
2093 long lItem = GetNextItem(-1,
2094 wxLIST_NEXT_ALL,
2095 wxLIST_STATE_SELECTED);
2096
2097 // <Enter> or <Space> activate the selected item if any (but
2098 // not with Shift and/or Ctrl as then they have a predefined
2099 // meaning for the list view)
2100 if ( lItem != -1 &&
2101 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
2102 !(wxIsShiftDown() || wxIsCtrlDown()) )
2103 {
2104 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2105 }
2106 else
2107 {
2108 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
2109
2110 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2111 // value which should be used as is
2112 int code = wxCharCodeMSWToWX(wVKey);
2113 event.m_code = code ? code : wVKey;
2114 }
2115
2116 event.m_itemIndex =
2117 event.m_item.m_itemId = lItem;
2118
2119 if ( lItem != -1 )
2120 {
2121 // fill the other fields too
2122 event.m_item.m_text = GetItemText(lItem);
2123 event.m_item.m_data = GetItemData(lItem);
2124 }
2125 }
2126 break;
2127
2128 case NM_DBLCLK:
2129 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2130 // anything else
2131 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2132 {
2133 return TRUE;
2134 }
2135
2136 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2137 // if it happened on an item (and not on empty place)
2138 if ( iItem == -1 )
2139 {
2140 // not on item
2141 return FALSE;
2142 }
2143
2144 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2145 event.m_itemIndex = iItem;
2146 event.m_item.m_text = GetItemText(iItem);
2147 event.m_item.m_data = GetItemData(iItem);
2148 break;
2149
2150 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2151 case GN_CONTEXTMENU:
2152 #endif //__WXWINCE__
2153 case NM_RCLICK:
2154 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2155 // don't do anything else
2156 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2157 {
2158 return TRUE;
2159 }
2160
2161 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2162 LV_HITTESTINFO lvhti;
2163 wxZeroMemory(lvhti);
2164
2165 #if defined(__WXWINCE__) && _WIN32_WCE < 400
2166 if(nmhdr->code == GN_CONTEXTMENU) {
2167 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2168 } else
2169 #endif //__WXWINCE__
2170 ::GetCursorPos(&(lvhti.pt));
2171 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
2172 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
2173 {
2174 if ( lvhti.flags & LVHT_ONITEM )
2175 {
2176 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2177 event.m_itemIndex = lvhti.iItem;
2178 event.m_pointDrag.x = lvhti.pt.x;
2179 event.m_pointDrag.y = lvhti.pt.y;
2180 }
2181 }
2182 break;
2183
2184 #ifdef NM_CUSTOMDRAW
2185 case NM_CUSTOMDRAW:
2186 *result = OnCustomDraw(lParam);
2187
2188 return TRUE;
2189 #endif // _WIN32_IE >= 0x300
2190
2191 case LVN_ODCACHEHINT:
2192 {
2193 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
2194
2195 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
2196
2197 // we get some really stupid cache hints like ones for
2198 // items in range 0..0 for an empty control or, after
2199 // deleting an item, for items in invalid range -- filter
2200 // this garbage out
2201 if ( cacheHint->iFrom > cacheHint->iTo )
2202 return FALSE;
2203
2204 event.m_oldItemIndex = cacheHint->iFrom;
2205
2206 const long iMax = GetItemCount();
2207 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2208 : iMax - 1;
2209 }
2210 break;
2211
2212 case LVN_GETDISPINFO:
2213 if ( IsVirtual() )
2214 {
2215 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2216
2217 LV_ITEM& lvi = info->item;
2218 long item = lvi.iItem;
2219
2220 if ( lvi.mask & LVIF_TEXT )
2221 {
2222 wxString text = OnGetItemText(item, lvi.iSubItem);
2223 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
2224 }
2225
2226 // see comment at the end of wxListCtrl::GetColumn()
2227 #ifdef NM_CUSTOMDRAW
2228 if ( lvi.mask & LVIF_IMAGE )
2229 {
2230 lvi.iImage = OnGetItemImage(item);
2231 }
2232 #endif // NM_CUSTOMDRAW
2233
2234 // a little dose of healthy paranoia: as we never use
2235 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2236 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
2237 _T("we don't support state callbacks yet!") );
2238
2239 return TRUE;
2240 }
2241 // fall through
2242
2243 default:
2244 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2245 }
2246 }
2247 else
2248 {
2249 // where did this one come from?
2250 return FALSE;
2251 }
2252
2253 // process the event
2254 // -----------------
2255
2256 event.SetEventType(eventType);
2257
2258 bool processed = GetEventHandler()->ProcessEvent(event);
2259
2260 // post processing
2261 // ---------------
2262 switch ( nmhdr->code )
2263 {
2264 case LVN_DELETEALLITEMS:
2265 // always return TRUE to suppress all additional LVN_DELETEITEM
2266 // notifications - this makes deleting all items from a list ctrl
2267 // much faster
2268 *result = TRUE;
2269 return TRUE;
2270
2271 case LVN_ENDLABELEDITA:
2272 case LVN_ENDLABELEDITW:
2273 // logic here is inversed compared to all the other messages
2274 *result = event.IsAllowed();
2275
2276 // don't keep a stale wxTextCtrl around
2277 if ( m_textCtrl )
2278 {
2279 // EDIT control will be deleted by the list control itself so
2280 // prevent us from deleting it as well
2281 m_textCtrl->UnsubclassWin();
2282 m_textCtrl->SetHWND(0);
2283 delete m_textCtrl;
2284 m_textCtrl = NULL;
2285 }
2286
2287 return TRUE;
2288 }
2289
2290 if ( processed )
2291 *result = !event.IsAllowed();
2292
2293 return processed;
2294 }
2295
2296 // see comment at the end of wxListCtrl::GetColumn()
2297 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2298
2299 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2300 {
2301 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
2302 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
2303 switch ( nmcd.dwDrawStage )
2304 {
2305 case CDDS_PREPAINT:
2306 // if we've got any items with non standard attributes,
2307 // notify us before painting each item
2308 //
2309 // for virtual controls, always suppose that we have attributes as
2310 // there is no way to check for this
2311 return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2312 : CDRF_DODEFAULT;
2313
2314 case CDDS_ITEMPREPAINT:
2315 {
2316 size_t item = (size_t)nmcd.dwItemSpec;
2317 if ( item >= (size_t)GetItemCount() )
2318 {
2319 // we get this message with item == 0 for an empty control,
2320 // we must ignore it as calling OnGetItemAttr() would be
2321 // wrong
2322 return CDRF_DODEFAULT;
2323 }
2324
2325 wxListItemAttr *attr =
2326 IsVirtual() ? OnGetItemAttr(item)
2327 : wxGetInternalDataAttr(this, item);
2328
2329 if ( !attr )
2330 {
2331 // nothing to do for this item
2332 return CDRF_DODEFAULT;
2333 }
2334
2335 HFONT hFont;
2336 wxColour colText, colBack;
2337 if ( attr->HasFont() )
2338 {
2339 wxFont font = attr->GetFont();
2340 hFont = (HFONT)font.GetResourceHandle();
2341 }
2342 else
2343 {
2344 hFont = 0;
2345 }
2346
2347 if ( attr->HasTextColour() )
2348 {
2349 colText = attr->GetTextColour();
2350 }
2351 else
2352 {
2353 colText = GetTextColour();
2354 }
2355
2356 if ( attr->HasBackgroundColour() )
2357 {
2358 colBack = attr->GetBackgroundColour();
2359 }
2360 else
2361 {
2362 colBack = GetBackgroundColour();
2363 }
2364
2365 lplvcd->clrText = wxColourToRGB(colText);
2366 lplvcd->clrTextBk = wxColourToRGB(colBack);
2367
2368 // note that if we wanted to set colours for
2369 // individual columns (subitems), we would have
2370 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2371 if ( hFont )
2372 {
2373 ::SelectObject(nmcd.hdc, hFont);
2374
2375 return CDRF_NEWFONT;
2376 }
2377 }
2378 // fall through to return CDRF_DODEFAULT
2379
2380 default:
2381 return CDRF_DODEFAULT;
2382 }
2383 }
2384
2385 #endif // NM_CUSTOMDRAW supported
2386
2387 // Necessary for drawing hrules and vrules, if specified
2388 void wxListCtrl::OnPaint(wxPaintEvent& event)
2389 {
2390 wxPaintDC dc(this);
2391
2392 wxControl::OnPaint(event);
2393
2394 // Reset the device origin since it may have been set
2395 dc.SetDeviceOrigin(0, 0);
2396
2397 bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0);
2398 bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0);
2399
2400 if (!drawHRules && !drawVRules)
2401 return;
2402 if ((GetWindowStyle() & wxLC_REPORT) == 0)
2403 return;
2404
2405 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2406 dc.SetPen(pen);
2407 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2408
2409 wxSize clientSize = GetClientSize();
2410 wxRect itemRect;
2411 int cy=0;
2412
2413 int itemCount = GetItemCount();
2414 int i;
2415 if (drawHRules)
2416 {
2417 long top = GetTopItem();
2418 for (i = top; i < top + GetCountPerPage() + 1; i++)
2419 {
2420 if (GetItemRect(i, itemRect))
2421 {
2422 cy = itemRect.GetTop();
2423 if (i != 0) // Don't draw the first one
2424 {
2425 dc.DrawLine(0, cy, clientSize.x, cy);
2426 }
2427 // Draw last line
2428 if (i == itemCount - 1)
2429 {
2430 cy = itemRect.GetBottom();
2431 dc.DrawLine(0, cy, clientSize.x, cy);
2432 }
2433 }
2434 }
2435 }
2436 i = itemCount - 1;
2437 if (drawVRules && (i > -1))
2438 {
2439 wxRect firstItemRect;
2440 GetItemRect(0, firstItemRect);
2441
2442 if (GetItemRect(i, itemRect))
2443 {
2444 int col;
2445 int x = itemRect.GetX();
2446 for (col = 0; col < GetColumnCount(); col++)
2447 {
2448 int colWidth = GetColumnWidth(col);
2449 x += colWidth ;
2450 dc.DrawLine(x-1, firstItemRect.GetY() - 2, x-1, itemRect.GetBottom());
2451 }
2452 }
2453 }
2454 }
2455
2456 // ----------------------------------------------------------------------------
2457 // virtual list controls
2458 // ----------------------------------------------------------------------------
2459
2460 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2461 {
2462 // this is a pure virtual function, in fact - which is not really pure
2463 // because the controls which are not virtual don't need to implement it
2464 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2465
2466 return wxEmptyString;
2467 }
2468
2469 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2470 {
2471 // same as above
2472 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2473
2474 return -1;
2475 }
2476
2477 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2478 {
2479 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2480 _T("invalid item index in OnGetItemAttr()") );
2481
2482 // no attributes by default
2483 return NULL;
2484 }
2485
2486 void wxListCtrl::SetItemCount(long count)
2487 {
2488 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2489
2490 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2491 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2492 {
2493 wxLogLastError(_T("ListView_SetItemCount"));
2494 }
2495 m_count = count;
2496 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2497 wxT("m_count should match ListView_GetItemCount"));
2498 }
2499
2500 void wxListCtrl::RefreshItem(long item)
2501 {
2502 // strangely enough, ListView_Update() results in much more flicker here
2503 // than a dumb Refresh() -- why?
2504 #if 0
2505 if ( !ListView_Update(GetHwnd(), item) )
2506 {
2507 wxLogLastError(_T("ListView_Update"));
2508 }
2509 #else // 1
2510 wxRect rect;
2511 GetItemRect(item, rect);
2512 RefreshRect(rect);
2513 #endif // 0/1
2514 }
2515
2516 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2517 {
2518 wxRect rect1, rect2;
2519 GetItemRect(itemFrom, rect1);
2520 GetItemRect(itemTo, rect2);
2521
2522 wxRect rect = rect1;
2523 rect.height = rect2.GetBottom() - rect1.GetTop();
2524
2525 RefreshRect(rect);
2526 }
2527
2528 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2529 {
2530 LV_ITEM it;
2531 it.mask = LVIF_PARAM;
2532 it.iItem = itemId;
2533
2534 bool success = ListView_GetItem(hwnd, &it) != 0;
2535 if (success)
2536 return (wxListItemInternalData *) it.lParam;
2537 else
2538 return NULL;
2539 };
2540
2541 static wxListItemInternalData *wxGetInternalData(wxListCtrl *ctl, long itemId)
2542 {
2543 return wxGetInternalData((HWND) ctl->GetHWND(), itemId);
2544 };
2545
2546 static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId)
2547 {
2548 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2549 if (data)
2550 return data->attr;
2551 else
2552 return NULL;
2553 };
2554
2555 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2556 {
2557 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2558 if (data)
2559 {
2560 LV_ITEM item;
2561 memset(&item, 0, sizeof(item));
2562 item.iItem = itemId;
2563 item.mask = LVIF_PARAM;
2564 item.lParam = (LPARAM) 0;
2565 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2566 delete data;
2567 }
2568 }
2569
2570 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2571 wxListItem& info,
2572 LV_ITEM& lvItem)
2573 {
2574 wxListItemInternalData *internaldata =
2575 (wxListItemInternalData *) lvItem.lParam;
2576
2577 if (internaldata)
2578 info.m_data = internaldata->lParam;
2579
2580 info.m_mask = 0;
2581 info.m_state = 0;
2582 info.m_stateMask = 0;
2583 info.m_itemId = lvItem.iItem;
2584
2585 long oldMask = lvItem.mask;
2586
2587 bool needText = FALSE;
2588 if (hwndListCtrl != 0)
2589 {
2590 if ( lvItem.mask & LVIF_TEXT )
2591 needText = FALSE;
2592 else
2593 needText = TRUE;
2594
2595 if ( needText )
2596 {
2597 lvItem.pszText = new wxChar[513];
2598 lvItem.cchTextMax = 512;
2599 }
2600 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2601 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2602 }
2603
2604 if ( lvItem.mask & LVIF_STATE )
2605 {
2606 info.m_mask |= wxLIST_MASK_STATE;
2607
2608 if ( lvItem.stateMask & LVIS_CUT)
2609 {
2610 info.m_stateMask |= wxLIST_STATE_CUT;
2611 if ( lvItem.state & LVIS_CUT )
2612 info.m_state |= wxLIST_STATE_CUT;
2613 }
2614 if ( lvItem.stateMask & LVIS_DROPHILITED)
2615 {
2616 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2617 if ( lvItem.state & LVIS_DROPHILITED )
2618 info.m_state |= wxLIST_STATE_DROPHILITED;
2619 }
2620 if ( lvItem.stateMask & LVIS_FOCUSED)
2621 {
2622 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2623 if ( lvItem.state & LVIS_FOCUSED )
2624 info.m_state |= wxLIST_STATE_FOCUSED;
2625 }
2626 if ( lvItem.stateMask & LVIS_SELECTED)
2627 {
2628 info.m_stateMask |= wxLIST_STATE_SELECTED;
2629 if ( lvItem.state & LVIS_SELECTED )
2630 info.m_state |= wxLIST_STATE_SELECTED;
2631 }
2632 }
2633
2634 if ( lvItem.mask & LVIF_TEXT )
2635 {
2636 info.m_mask |= wxLIST_MASK_TEXT;
2637 info.m_text = lvItem.pszText;
2638 }
2639 if ( lvItem.mask & LVIF_IMAGE )
2640 {
2641 info.m_mask |= wxLIST_MASK_IMAGE;
2642 info.m_image = lvItem.iImage;
2643 }
2644 if ( lvItem.mask & LVIF_PARAM )
2645 info.m_mask |= wxLIST_MASK_DATA;
2646 if ( lvItem.mask & LVIF_DI_SETITEM )
2647 info.m_mask |= wxLIST_SET_ITEM;
2648 info.m_col = lvItem.iSubItem;
2649
2650 if (needText)
2651 {
2652 if (lvItem.pszText)
2653 delete[] lvItem.pszText;
2654 }
2655 lvItem.mask = oldMask;
2656 }
2657
2658 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2659 {
2660 if (stateMask & wxLIST_STATE_CUT)
2661 {
2662 lvItem.stateMask |= LVIS_CUT;
2663 if (state & wxLIST_STATE_CUT)
2664 lvItem.state |= LVIS_CUT;
2665 }
2666 if (stateMask & wxLIST_STATE_DROPHILITED)
2667 {
2668 lvItem.stateMask |= LVIS_DROPHILITED;
2669 if (state & wxLIST_STATE_DROPHILITED)
2670 lvItem.state |= LVIS_DROPHILITED;
2671 }
2672 if (stateMask & wxLIST_STATE_FOCUSED)
2673 {
2674 lvItem.stateMask |= LVIS_FOCUSED;
2675 if (state & wxLIST_STATE_FOCUSED)
2676 lvItem.state |= LVIS_FOCUSED;
2677 }
2678 if (stateMask & wxLIST_STATE_SELECTED)
2679 {
2680 lvItem.stateMask |= LVIS_SELECTED;
2681 if (state & wxLIST_STATE_SELECTED)
2682 lvItem.state |= LVIS_SELECTED;
2683 }
2684 }
2685
2686 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2687 const wxListItem& info,
2688 LV_ITEM& lvItem)
2689 {
2690 lvItem.iItem = (int) info.m_itemId;
2691
2692 lvItem.iImage = info.m_image;
2693 lvItem.stateMask = 0;
2694 lvItem.state = 0;
2695 lvItem.mask = 0;
2696 lvItem.iSubItem = info.m_col;
2697
2698 if (info.m_mask & wxLIST_MASK_STATE)
2699 {
2700 lvItem.mask |= LVIF_STATE;
2701
2702 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
2703 }
2704
2705 if (info.m_mask & wxLIST_MASK_TEXT)
2706 {
2707 lvItem.mask |= LVIF_TEXT;
2708 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
2709 {
2710 lvItem.pszText = LPSTR_TEXTCALLBACK;
2711 }
2712 else
2713 {
2714 // pszText is not const, hence the cast
2715 lvItem.pszText = (wxChar *)info.m_text.c_str();
2716 if ( lvItem.pszText )
2717 lvItem.cchTextMax = info.m_text.Length();
2718 else
2719 lvItem.cchTextMax = 0;
2720 }
2721 }
2722 if (info.m_mask & wxLIST_MASK_IMAGE)
2723 lvItem.mask |= LVIF_IMAGE;
2724 }
2725
2726 static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
2727 LV_COLUMN& lvCol)
2728 {
2729 wxZeroMemory(lvCol);
2730
2731 if ( item.m_mask & wxLIST_MASK_TEXT )
2732 {
2733 lvCol.mask |= LVCF_TEXT;
2734 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
2735 }
2736
2737 if ( item.m_mask & wxLIST_MASK_FORMAT )
2738 {
2739 lvCol.mask |= LVCF_FMT;
2740
2741 if ( item.m_format == wxLIST_FORMAT_LEFT )
2742 lvCol.fmt = LVCFMT_LEFT;
2743 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
2744 lvCol.fmt = LVCFMT_RIGHT;
2745 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
2746 lvCol.fmt = LVCFMT_CENTER;
2747 }
2748
2749 if ( item.m_mask & wxLIST_MASK_WIDTH )
2750 {
2751 lvCol.mask |= LVCF_WIDTH;
2752 if ( item.m_width == wxLIST_AUTOSIZE)
2753 lvCol.cx = LVSCW_AUTOSIZE;
2754 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2755 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
2756 else
2757 lvCol.cx = item.m_width;
2758 }
2759
2760 // see comment at the end of wxListCtrl::GetColumn()
2761 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2762 if ( item.m_mask & wxLIST_MASK_IMAGE )
2763 {
2764 if ( wxTheApp->GetComCtl32Version() >= 470 )
2765 {
2766 lvCol.mask |= LVCF_IMAGE | LVCF_FMT;
2767
2768 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2769 // seem to be generally nicer than on the left and the generic
2770 // version only draws them on the right (we don't have a flag to
2771 // specify the image location anyhow)
2772 //
2773 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2774 // make any difference in my tests -- but maybe we should?
2775 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
2776
2777 lvCol.iImage = item.m_image;
2778 }
2779 //else: it doesn't support item images anyhow
2780 }
2781 #endif // _WIN32_IE >= 0x0300
2782 }
2783
2784 #endif // wxUSE_LISTCTRL
2785