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