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