]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
Applied patch [ 1378522 ] fixed bitmap w/alpha->icon/cursor conversion
[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
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 | LVS_EX_SUBITEMIMAGES);
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 const wxListItemAttr& attrNew = *info.GetAttributes();
794
795 // don't overwrite the already set attributes if we have them
796 if ( data->attr )
797 data->attr->AssignFrom(attrNew);
798 else
799 data->attr = new wxListItemAttr(attrNew);
800 };
801 };
802
803
804 // we could be changing only the attribute in which case we don't need to
805 // call ListView_SetItem() at all
806 if ( item.mask )
807 {
808 item.cchTextMax = 0;
809 if ( !ListView_SetItem(GetHwnd(), &item) )
810 {
811 wxLogDebug(_T("ListView_SetItem() failed"));
812
813 return false;
814 }
815 }
816
817 // we need to update the item immediately to show the new image
818 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
819
820 // check whether it has any custom attributes
821 if ( info.HasAttributes() )
822 {
823 m_hasAnyAttr = true;
824
825 // if the colour has changed, we must redraw the item
826 updateNow = true;
827 }
828
829 if ( updateNow )
830 {
831 // we need this to make the change visible right now
832 RefreshItem(item.iItem);
833 }
834
835 return true;
836 }
837
838 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
839 {
840 wxListItem info;
841 info.m_text = label;
842 info.m_mask = wxLIST_MASK_TEXT;
843 info.m_itemId = index;
844 info.m_col = col;
845 if ( imageId > -1 )
846 {
847 info.m_image = imageId;
848 info.m_mask |= wxLIST_MASK_IMAGE;
849 }
850 return SetItem(info);
851 }
852
853
854 // Gets the item state
855 int wxListCtrl::GetItemState(long item, long stateMask) const
856 {
857 wxListItem info;
858
859 info.m_mask = wxLIST_MASK_STATE;
860 info.m_stateMask = stateMask;
861 info.m_itemId = item;
862
863 if (!GetItem(info))
864 return 0;
865
866 return info.m_state;
867 }
868
869 // Sets the item state
870 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
871 {
872 // NB: don't use SetItem() here as it doesn't work with the virtual list
873 // controls
874 LV_ITEM lvItem;
875 wxZeroMemory(lvItem);
876
877 wxConvertToMSWFlags(state, stateMask, lvItem);
878
879 // for the virtual list controls we need to refresh the previously focused
880 // item manually when changing focus without changing selection
881 // programmatically because otherwise it keeps its focus rectangle until
882 // next repaint (yet another comctl32 bug)
883 long focusOld;
884 if ( IsVirtual() &&
885 (stateMask & wxLIST_STATE_FOCUSED) &&
886 (state & wxLIST_STATE_FOCUSED) )
887 {
888 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
889 }
890 else
891 {
892 focusOld = -1;
893 }
894
895 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
896 (WPARAM)item, (LPARAM)&lvItem) )
897 {
898 wxLogLastError(_T("ListView_SetItemState"));
899
900 return false;
901 }
902
903 if ( focusOld != -1 )
904 {
905 // no need to refresh the item if it was previously selected, it would
906 // only result in annoying flicker
907 if ( !(GetItemState(focusOld,
908 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
909 {
910 RefreshItem(focusOld);
911 }
912 }
913
914 return true;
915 }
916
917 // Sets the item image
918 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
919 {
920 wxListItem info;
921
922 info.m_mask = wxLIST_MASK_IMAGE;
923 info.m_image = image;
924 info.m_itemId = item;
925
926 return SetItem(info);
927 }
928
929 // Gets the item text
930 wxString wxListCtrl::GetItemText(long item) const
931 {
932 wxListItem info;
933
934 info.m_mask = wxLIST_MASK_TEXT;
935 info.m_itemId = item;
936
937 if (!GetItem(info))
938 return wxEmptyString;
939 return info.m_text;
940 }
941
942 // Sets the item text
943 void wxListCtrl::SetItemText(long item, const wxString& str)
944 {
945 wxListItem info;
946
947 info.m_mask = wxLIST_MASK_TEXT;
948 info.m_itemId = item;
949 info.m_text = str;
950
951 SetItem(info);
952 }
953
954 // Gets the item data
955 wxUIntPtr wxListCtrl::GetItemData(long item) const
956 {
957 wxListItem info;
958
959 info.m_mask = wxLIST_MASK_DATA;
960 info.m_itemId = item;
961
962 if (!GetItem(info))
963 return 0;
964 return info.m_data;
965 }
966
967 // Sets the item data
968 bool wxListCtrl::SetItemData(long item, long data)
969 {
970 wxListItem info;
971
972 info.m_mask = wxLIST_MASK_DATA;
973 info.m_itemId = item;
974 info.m_data = data;
975
976 return SetItem(info);
977 }
978
979 wxRect wxListCtrl::GetViewRect() const
980 {
981 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
982 _T("wxListCtrl::GetViewRect() only works in icon mode") );
983
984 RECT rc;
985 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
986 {
987 wxLogDebug(_T("ListView_GetViewRect() failed."));
988
989 wxZeroMemory(rc);
990 }
991
992 wxRect rect;
993 wxCopyRECTToRect(rc, rect);
994
995 return rect;
996 }
997
998 // Gets the item rectangle
999 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
1000 {
1001 RECT rectWin;
1002
1003 int codeWin;
1004 if ( code == wxLIST_RECT_BOUNDS )
1005 codeWin = LVIR_BOUNDS;
1006 else if ( code == wxLIST_RECT_ICON )
1007 codeWin = LVIR_ICON;
1008 else if ( code == wxLIST_RECT_LABEL )
1009 codeWin = LVIR_LABEL;
1010 else
1011 {
1012 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1013
1014 codeWin = LVIR_BOUNDS;
1015 }
1016
1017 bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
1018
1019 rect.x = rectWin.left;
1020 rect.y = rectWin.top;
1021 rect.width = rectWin.right - rectWin.left;
1022 rect.height = rectWin.bottom - rectWin.top;
1023
1024 return success;
1025 }
1026
1027 // Gets the item position
1028 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1029 {
1030 POINT pt;
1031
1032 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
1033
1034 pos.x = pt.x; pos.y = pt.y;
1035 return success;
1036 }
1037
1038 // Sets the item position.
1039 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1040 {
1041 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
1042 }
1043
1044 // Gets the number of items in the list control
1045 int wxListCtrl::GetItemCount() const
1046 {
1047 return m_count;
1048 }
1049
1050 wxSize wxListCtrl::GetItemSpacing() const
1051 {
1052 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
1053
1054 return wxSize(LOWORD(spacing), HIWORD(spacing));
1055 }
1056
1057 int wxListCtrl::GetItemSpacing(bool isSmall) const
1058 {
1059 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
1060 }
1061
1062 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1063 {
1064 wxListItem info;
1065 info.m_itemId = item;
1066 info.SetTextColour( col );
1067 SetItem( info );
1068 }
1069
1070 wxColour wxListCtrl::GetItemTextColour( long item ) const
1071 {
1072 wxColour col;
1073 wxListItemInternalData *data = wxGetInternalData(this, item);
1074 if ( data && data->attr )
1075 col = data->attr->GetTextColour();
1076
1077 return col;
1078 }
1079
1080 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1081 {
1082 wxListItem info;
1083 info.m_itemId = item;
1084 info.SetBackgroundColour( col );
1085 SetItem( info );
1086 }
1087
1088 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1089 {
1090 wxColour col;
1091 wxListItemInternalData *data = wxGetInternalData(this, item);
1092 if ( data && data->attr )
1093 col = data->attr->GetBackgroundColour();
1094
1095 return col;
1096 }
1097
1098 void wxListCtrl::SetItemFont( long item, const wxFont &f )
1099 {
1100 wxListItem info;
1101 info.m_itemId = item;
1102 info.SetFont( f );
1103 SetItem( info );
1104 }
1105
1106 wxFont wxListCtrl::GetItemFont( long item ) const
1107 {
1108 wxFont f;
1109 wxListItemInternalData *data = wxGetInternalData(this, item);
1110 if ( data && data->attr )
1111 f = data->attr->GetFont();
1112
1113 return f;
1114 }
1115
1116 // Gets the number of selected items in the list control
1117 int wxListCtrl::GetSelectedItemCount() const
1118 {
1119 return ListView_GetSelectedCount(GetHwnd());
1120 }
1121
1122 // Gets the text colour of the listview
1123 wxColour wxListCtrl::GetTextColour() const
1124 {
1125 COLORREF ref = ListView_GetTextColor(GetHwnd());
1126 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1127 return col;
1128 }
1129
1130 // Sets the text colour of the listview
1131 void wxListCtrl::SetTextColour(const wxColour& col)
1132 {
1133 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
1134 }
1135
1136 // Gets the index of the topmost visible item when in
1137 // list or report view
1138 long wxListCtrl::GetTopItem() const
1139 {
1140 return (long) ListView_GetTopIndex(GetHwnd());
1141 }
1142
1143 // Searches for an item, starting from 'item'.
1144 // 'geometry' is one of
1145 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1146 // 'state' is a state bit flag, one or more of
1147 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1148 // item can be -1 to find the first item that matches the
1149 // specified flags.
1150 // Returns the item or -1 if unsuccessful.
1151 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1152 {
1153 long flags = 0;
1154
1155 if ( geom == wxLIST_NEXT_ABOVE )
1156 flags |= LVNI_ABOVE;
1157 if ( geom == wxLIST_NEXT_ALL )
1158 flags |= LVNI_ALL;
1159 if ( geom == wxLIST_NEXT_BELOW )
1160 flags |= LVNI_BELOW;
1161 if ( geom == wxLIST_NEXT_LEFT )
1162 flags |= LVNI_TOLEFT;
1163 if ( geom == wxLIST_NEXT_RIGHT )
1164 flags |= LVNI_TORIGHT;
1165
1166 if ( state & wxLIST_STATE_CUT )
1167 flags |= LVNI_CUT;
1168 if ( state & wxLIST_STATE_DROPHILITED )
1169 flags |= LVNI_DROPHILITED;
1170 if ( state & wxLIST_STATE_FOCUSED )
1171 flags |= LVNI_FOCUSED;
1172 if ( state & wxLIST_STATE_SELECTED )
1173 flags |= LVNI_SELECTED;
1174
1175 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1176 }
1177
1178
1179 wxImageList *wxListCtrl::GetImageList(int which) const
1180 {
1181 if ( which == wxIMAGE_LIST_NORMAL )
1182 {
1183 return m_imageListNormal;
1184 }
1185 else if ( which == wxIMAGE_LIST_SMALL )
1186 {
1187 return m_imageListSmall;
1188 }
1189 else if ( which == wxIMAGE_LIST_STATE )
1190 {
1191 return m_imageListState;
1192 }
1193 return NULL;
1194 }
1195
1196 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1197 {
1198 int flags = 0;
1199 if ( which == wxIMAGE_LIST_NORMAL )
1200 {
1201 flags = LVSIL_NORMAL;
1202 if (m_ownsImageListNormal) delete m_imageListNormal;
1203 m_imageListNormal = imageList;
1204 m_ownsImageListNormal = false;
1205 }
1206 else if ( which == wxIMAGE_LIST_SMALL )
1207 {
1208 flags = LVSIL_SMALL;
1209 if (m_ownsImageListSmall) delete m_imageListSmall;
1210 m_imageListSmall = imageList;
1211 m_ownsImageListSmall = false;
1212 }
1213 else if ( which == wxIMAGE_LIST_STATE )
1214 {
1215 flags = LVSIL_STATE;
1216 if (m_ownsImageListState) delete m_imageListState;
1217 m_imageListState = imageList;
1218 m_ownsImageListState = false;
1219 }
1220 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1221 }
1222
1223 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1224 {
1225 SetImageList(imageList, which);
1226 if ( which == wxIMAGE_LIST_NORMAL )
1227 m_ownsImageListNormal = true;
1228 else if ( which == wxIMAGE_LIST_SMALL )
1229 m_ownsImageListSmall = true;
1230 else if ( which == wxIMAGE_LIST_STATE )
1231 m_ownsImageListState = true;
1232 }
1233
1234 // ----------------------------------------------------------------------------
1235 // Operations
1236 // ----------------------------------------------------------------------------
1237
1238 // Arranges the items
1239 bool wxListCtrl::Arrange(int flag)
1240 {
1241 UINT code = 0;
1242 if ( flag == wxLIST_ALIGN_LEFT )
1243 code = LVA_ALIGNLEFT;
1244 else if ( flag == wxLIST_ALIGN_TOP )
1245 code = LVA_ALIGNTOP;
1246 else if ( flag == wxLIST_ALIGN_DEFAULT )
1247 code = LVA_DEFAULT;
1248 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1249 code = LVA_SNAPTOGRID;
1250
1251 return (ListView_Arrange(GetHwnd(), code) != 0);
1252 }
1253
1254 // Deletes an item
1255 bool wxListCtrl::DeleteItem(long item)
1256 {
1257 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1258 {
1259 wxLogLastError(_T("ListView_DeleteItem"));
1260 return false;
1261 }
1262
1263 m_count--;
1264 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1265 wxT("m_count should match ListView_GetItemCount"));
1266
1267 // the virtual list control doesn't refresh itself correctly, help it
1268 if ( IsVirtual() )
1269 {
1270 // we need to refresh all the lines below the one which was deleted
1271 wxRect rectItem;
1272 if ( item > 0 && GetItemCount() )
1273 {
1274 GetItemRect(item - 1, rectItem);
1275 }
1276 else
1277 {
1278 rectItem.y =
1279 rectItem.height = 0;
1280 }
1281
1282 wxRect rectWin = GetRect();
1283 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1284 rectWin.y = rectItem.GetBottom();
1285
1286 RefreshRect(rectWin);
1287 }
1288
1289 return true;
1290 }
1291
1292 // Deletes all items
1293 bool wxListCtrl::DeleteAllItems()
1294 {
1295 return ListView_DeleteAllItems(GetHwnd()) != 0;
1296 }
1297
1298 // Deletes all items
1299 bool wxListCtrl::DeleteAllColumns()
1300 {
1301 while ( m_colCount > 0 )
1302 {
1303 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1304 {
1305 wxLogLastError(wxT("ListView_DeleteColumn"));
1306
1307 return false;
1308 }
1309
1310 m_colCount--;
1311 }
1312
1313 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1314
1315 return true;
1316 }
1317
1318 // Deletes a column
1319 bool wxListCtrl::DeleteColumn(int col)
1320 {
1321 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1322
1323 if ( success && (m_colCount > 0) )
1324 m_colCount --;
1325 return success;
1326 }
1327
1328 // Clears items, and columns if there are any.
1329 void wxListCtrl::ClearAll()
1330 {
1331 DeleteAllItems();
1332 if ( m_colCount > 0 )
1333 DeleteAllColumns();
1334 }
1335
1336 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1337 {
1338 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1339
1340 // ListView_EditLabel requires that the list has focus.
1341 SetFocus();
1342
1343 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
1344 if ( !hWnd )
1345 {
1346 // failed to start editing
1347 return NULL;
1348 }
1349
1350 // [re]create the text control wrapping the HWND we got
1351 if ( m_textCtrl )
1352 {
1353 m_textCtrl->UnsubclassWin();
1354 m_textCtrl->SetHWND(0);
1355 delete m_textCtrl;
1356 }
1357
1358 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1359 m_textCtrl->SetHWND(hWnd);
1360 m_textCtrl->SubclassWin(hWnd);
1361 m_textCtrl->SetParent(this);
1362
1363 // we must disallow TABbing away from the control while the edit contol is
1364 // shown because this leaves it in some strange state (just try removing
1365 // this line and then pressing TAB while editing an item in listctrl
1366 // inside a panel)
1367 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1368
1369 return m_textCtrl;
1370 }
1371
1372 // End label editing, optionally cancelling the edit
1373 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel))
1374 {
1375 wxFAIL_MSG( _T("not implemented") );
1376
1377 return false;
1378 }
1379
1380 // Ensures this item is visible
1381 bool wxListCtrl::EnsureVisible(long item)
1382 {
1383 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE;
1384 }
1385
1386 // Find an item whose label matches this string, starting from the item after 'start'
1387 // or the beginning if 'start' is -1.
1388 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1389 {
1390 LV_FINDINFO findInfo;
1391
1392 findInfo.flags = LVFI_STRING;
1393 if ( partial )
1394 findInfo.flags |= LVFI_PARTIAL;
1395 findInfo.psz = str;
1396
1397 // ListView_FindItem() excludes the first item from search and to look
1398 // through all the items you need to start from -1 which is unnatural and
1399 // inconsistent with the generic version - so we adjust the index
1400 if (start != -1)
1401 start --;
1402 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1403 }
1404
1405 // Find an item whose data matches this data, starting from the item after 'start'
1406 // or the beginning if 'start' is -1.
1407 // NOTE : Lindsay Mathieson - 14-July-2002
1408 // No longer use ListView_FindItem as the data attribute is now stored
1409 // in a wxListItemInternalData structure refernced by the actual lParam
1410 long wxListCtrl::FindItem(long start, wxUIntPtr data)
1411 {
1412 long idx = start + 1;
1413 long count = GetItemCount();
1414
1415 while (idx < count)
1416 {
1417 if (GetItemData(idx) == data)
1418 return idx;
1419 idx++;
1420 };
1421
1422 return -1;
1423 }
1424
1425 // Find an item nearest this position in the specified direction, starting from
1426 // the item after 'start' or the beginning if 'start' is -1.
1427 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1428 {
1429 LV_FINDINFO findInfo;
1430
1431 findInfo.flags = LVFI_NEARESTXY;
1432 findInfo.pt.x = pt.x;
1433 findInfo.pt.y = pt.y;
1434 findInfo.vkDirection = VK_RIGHT;
1435
1436 if ( direction == wxLIST_FIND_UP )
1437 findInfo.vkDirection = VK_UP;
1438 else if ( direction == wxLIST_FIND_DOWN )
1439 findInfo.vkDirection = VK_DOWN;
1440 else if ( direction == wxLIST_FIND_LEFT )
1441 findInfo.vkDirection = VK_LEFT;
1442 else if ( direction == wxLIST_FIND_RIGHT )
1443 findInfo.vkDirection = VK_RIGHT;
1444
1445 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1446 }
1447
1448 // Determines which item (if any) is at the specified point,
1449 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1450 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1451 {
1452 LV_HITTESTINFO hitTestInfo;
1453 hitTestInfo.pt.x = (int) point.x;
1454 hitTestInfo.pt.y = (int) point.y;
1455
1456 ListView_HitTest(GetHwnd(), & hitTestInfo);
1457
1458 flags = 0;
1459
1460 if ( hitTestInfo.flags & LVHT_ABOVE )
1461 flags |= wxLIST_HITTEST_ABOVE;
1462 if ( hitTestInfo.flags & LVHT_BELOW )
1463 flags |= wxLIST_HITTEST_BELOW;
1464 if ( hitTestInfo.flags & LVHT_TOLEFT )
1465 flags |= wxLIST_HITTEST_TOLEFT;
1466 if ( hitTestInfo.flags & LVHT_TORIGHT )
1467 flags |= wxLIST_HITTEST_TORIGHT;
1468
1469 if ( hitTestInfo.flags & LVHT_NOWHERE )
1470 flags |= wxLIST_HITTEST_NOWHERE;
1471
1472 // note a bug or at least a very strange feature of comtl32.dll (tested
1473 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1474 // the right of the item label, ListView_HitTest() returns a combination of
1475 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1476 // the bits which don't make sense
1477 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1478 {
1479 flags |= wxLIST_HITTEST_ONITEMLABEL;
1480
1481 // do not translate LVHT_ONITEMICON here, as per above
1482 }
1483 else
1484 {
1485 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1486 flags |= wxLIST_HITTEST_ONITEMICON;
1487 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1488 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1489 }
1490
1491 return (long) hitTestInfo.iItem;
1492 }
1493
1494 // Inserts an item, returning the index of the new item if successful,
1495 // -1 otherwise.
1496 long wxListCtrl::InsertItem(const wxListItem& info)
1497 {
1498 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1499
1500 LV_ITEM item;
1501 wxConvertToMSWListItem(this, info, item);
1502 item.mask &= ~LVIF_PARAM;
1503
1504 // check wether we need to allocate our internal data
1505 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1506 if (needInternalData)
1507 {
1508 m_AnyInternalData = true;
1509 item.mask |= LVIF_PARAM;
1510
1511 // internal stucture that manages data
1512 wxListItemInternalData *data = new wxListItemInternalData();
1513 item.lParam = (LPARAM) data;
1514
1515 if (info.m_mask & wxLIST_MASK_DATA)
1516 data->lParam = info.m_data;
1517
1518 // check whether it has any custom attributes
1519 if ( info.HasAttributes() )
1520 {
1521 // take copy of attributes
1522 data->attr = new wxListItemAttr(*info.GetAttributes());
1523
1524 // and remember that we have some now...
1525 m_hasAnyAttr = true;
1526 }
1527 };
1528
1529 long rv = ListView_InsertItem(GetHwnd(), & item);
1530
1531 m_count++;
1532 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1533 wxT("m_count should match ListView_GetItemCount"));
1534
1535 return rv;
1536 }
1537
1538 long wxListCtrl::InsertItem(long index, const wxString& label)
1539 {
1540 wxListItem info;
1541 info.m_text = label;
1542 info.m_mask = wxLIST_MASK_TEXT;
1543 info.m_itemId = index;
1544 return InsertItem(info);
1545 }
1546
1547 // Inserts an image item
1548 long wxListCtrl::InsertItem(long index, int imageIndex)
1549 {
1550 wxListItem info;
1551 info.m_image = imageIndex;
1552 info.m_mask = wxLIST_MASK_IMAGE;
1553 info.m_itemId = index;
1554 return InsertItem(info);
1555 }
1556
1557 // Inserts an image/string item
1558 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1559 {
1560 wxListItem info;
1561 info.m_image = imageIndex;
1562 info.m_text = label;
1563 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1564 info.m_itemId = index;
1565 return InsertItem(info);
1566 }
1567
1568 // For list view mode (only), inserts a column.
1569 long wxListCtrl::InsertColumn(long col, const wxListItem& item)
1570 {
1571 LV_COLUMN lvCol;
1572 wxConvertToMSWListCol(col, item, lvCol);
1573
1574 if ( !(lvCol.mask & LVCF_WIDTH) )
1575 {
1576 // always give some width to the new column: this one is compatible
1577 // with the generic version
1578 lvCol.mask |= LVCF_WIDTH;
1579 lvCol.cx = 80;
1580 }
1581
1582 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1583 if ( n != -1 )
1584 {
1585 m_colCount++;
1586 }
1587 else // failed to insert?
1588 {
1589 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1590 lvCol.pszText);
1591 }
1592
1593 return n;
1594 }
1595
1596 long wxListCtrl::InsertColumn(long col,
1597 const wxString& heading,
1598 int format,
1599 int width)
1600 {
1601 wxListItem item;
1602 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1603 item.m_text = heading;
1604 if ( width > -1 )
1605 {
1606 item.m_mask |= wxLIST_MASK_WIDTH;
1607 item.m_width = width;
1608 }
1609 item.m_format = format;
1610
1611 return InsertColumn(col, item);
1612 }
1613
1614 // scroll the control by the given number of pixels (exception: in list view,
1615 // dx is interpreted as number of columns)
1616 bool wxListCtrl::ScrollList(int dx, int dy)
1617 {
1618 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1619 {
1620 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1621
1622 return false;
1623 }
1624
1625 return true;
1626 }
1627
1628 // Sort items.
1629
1630 // fn is a function which takes 3 long arguments: item1, item2, data.
1631 // item1 is the long data associated with a first item (NOT the index).
1632 // item2 is the long data associated with a second item (NOT the index).
1633 // data is the same value as passed to SortItems.
1634 // The return value is a negative number if the first item should precede the second
1635 // item, a positive number of the second item should precede the first,
1636 // or zero if the two items are equivalent.
1637
1638 // data is arbitrary data to be passed to the sort function.
1639
1640 // Internal structures for proxying the user compare function
1641 // so that we can pass it the *real* user data
1642
1643 // translate lParam data and call user func
1644 struct wxInternalDataSort
1645 {
1646 wxListCtrlCompare user_fn;
1647 long data;
1648 };
1649
1650 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1651 {
1652 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1653
1654 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1655 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1656
1657 long d1 = (data1 == NULL ? 0 : data1->lParam);
1658 long d2 = (data2 == NULL ? 0 : data2->lParam);
1659
1660 return internalData->user_fn(d1, d2, internalData->data);
1661
1662 }
1663
1664 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1665 {
1666 struct wxInternalDataSort internalData;
1667 internalData.user_fn = fn;
1668 internalData.data = data;
1669
1670 // WPARAM cast is needed for mingw/cygwin
1671 if ( !ListView_SortItems(GetHwnd(),
1672 wxInternalDataCompareFunc,
1673 (WPARAM) &internalData) )
1674 {
1675 wxLogDebug(_T("ListView_SortItems() failed"));
1676
1677 return false;
1678 }
1679
1680 return true;
1681 }
1682
1683
1684
1685 // ----------------------------------------------------------------------------
1686 // message processing
1687 // ----------------------------------------------------------------------------
1688
1689 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1690 {
1691 if (cmd == EN_UPDATE)
1692 {
1693 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1694 event.SetEventObject( this );
1695 ProcessCommand(event);
1696 return true;
1697 }
1698 else if (cmd == EN_KILLFOCUS)
1699 {
1700 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1701 event.SetEventObject( this );
1702 ProcessCommand(event);
1703 return true;
1704 }
1705 else
1706 return false;
1707 }
1708
1709 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1710 {
1711
1712 // prepare the event
1713 // -----------------
1714
1715 wxListEvent event(wxEVT_NULL, m_windowId);
1716 event.SetEventObject(this);
1717
1718 wxEventType eventType = wxEVT_NULL;
1719
1720 NMHDR *nmhdr = (NMHDR *)lParam;
1721
1722 // if your compiler is as broken as this, you should really change it: this
1723 // code is needed for normal operation! #ifdef below is only useful for
1724 // automatic rebuilds which are done with a very old compiler version
1725 #ifdef HDN_BEGINTRACKA
1726
1727 // check for messages from the header (in report view)
1728 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1729
1730 // is it a message from the header?
1731 if ( nmhdr->hwndFrom == hwndHdr )
1732 {
1733 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1734
1735 event.m_itemIndex = -1;
1736
1737 switch ( nmhdr->code )
1738 {
1739 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1740 // TRACK messages even to ANSI programs: on my system I get
1741 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1742 //
1743 // work around is to simply catch both versions and hope that it
1744 // works (why should this message exist in ANSI and Unicode is
1745 // beyond me as it doesn't deal with strings at all...)
1746 //
1747 // note that fr HDN_TRACK another possibility could be to use
1748 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1749 // something other than the item width changes so we'd have to
1750 // filter out the unwanted events then
1751 case HDN_BEGINTRACKA:
1752 case HDN_BEGINTRACKW:
1753 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1754 // fall through
1755
1756 case HDN_TRACKA:
1757 case HDN_TRACKW:
1758 if ( eventType == wxEVT_NULL )
1759 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1760 // fall through
1761
1762 case HDN_ENDTRACKA:
1763 case HDN_ENDTRACKW:
1764 if ( eventType == wxEVT_NULL )
1765 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1766
1767 event.m_item.m_width = nmHDR->pitem->cxy;
1768 event.m_col = nmHDR->iItem;
1769 break;
1770
1771 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1772 case GN_CONTEXTMENU:
1773 #endif //__WXWINCE__
1774 case NM_RCLICK:
1775 {
1776 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1777 event.m_col = -1;
1778
1779 // find the column clicked: we have to search for it
1780 // ourselves as the notification message doesn't provide
1781 // this info
1782
1783 // where did the click occur?
1784 POINT ptClick;
1785 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1786 if(nmhdr->code == GN_CONTEXTMENU) {
1787 ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1788 } else
1789 #endif //__WXWINCE__
1790 if ( !::GetCursorPos(&ptClick) )
1791 {
1792 wxLogLastError(_T("GetCursorPos"));
1793 }
1794
1795 if ( !::ScreenToClient(GetHwnd(), &ptClick) )
1796 {
1797 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1798 }
1799
1800 event.m_pointDrag.x = ptClick.x;
1801 event.m_pointDrag.y = ptClick.y;
1802
1803 int colCount = Header_GetItemCount(hwndHdr);
1804
1805 RECT rect;
1806 for ( int col = 0; col < colCount; col++ )
1807 {
1808 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1809 {
1810 if ( ::PtInRect(&rect, ptClick) )
1811 {
1812 event.m_col = col;
1813 break;
1814 }
1815 }
1816 }
1817 }
1818 break;
1819
1820 case HDN_GETDISPINFOW:
1821 // letting Windows XP handle this message results in mysterious
1822 // crashes in comctl32.dll seemingly because of bad message
1823 // parameters
1824 //
1825 // I have no idea what is the real cause of the bug (which is,
1826 // just to make things interesting, is impossible to reproduce
1827 // reliably) but ignoring all these messages does fix it and
1828 // doesn't seem to have any negative consequences
1829 return true;
1830
1831 default:
1832 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1833 }
1834 }
1835 else
1836 #endif // defined(HDN_BEGINTRACKA)
1837 if ( nmhdr->hwndFrom == GetHwnd() )
1838 {
1839 // almost all messages use NM_LISTVIEW
1840 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1841
1842 const int iItem = nmLV->iItem;
1843
1844
1845 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1846 // ignored for efficiency. It is done here because the internal data is in the
1847 // process of being deleted so we don't want to try and access it below.
1848 if ( m_ignoreChangeMessages &&
1849 ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
1850 (nmLV->hdr.code == LVN_ITEMCHANGING)) )
1851 {
1852 return true;
1853 }
1854
1855
1856 // If we have a valid item then check if there is a data value
1857 // associated with it and put it in the event.
1858 if ( iItem >= 0 && iItem < GetItemCount() )
1859 {
1860 wxListItemInternalData *internaldata =
1861 wxGetInternalData(GetHwnd(), iItem);
1862
1863 if ( internaldata )
1864 event.m_item.m_data = internaldata->lParam;
1865 }
1866
1867 bool processed = true;
1868 switch ( nmhdr->code )
1869 {
1870 case LVN_BEGINRDRAG:
1871 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1872 // fall through
1873
1874 case LVN_BEGINDRAG:
1875 if ( eventType == wxEVT_NULL )
1876 {
1877 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1878 }
1879
1880 event.m_itemIndex = iItem;
1881 event.m_pointDrag.x = nmLV->ptAction.x;
1882 event.m_pointDrag.y = nmLV->ptAction.y;
1883 break;
1884
1885 // NB: we have to handle both *A and *W versions here because some
1886 // versions of comctl32.dll send ANSI messages even to the
1887 // Unicode windows
1888 case LVN_BEGINLABELEDITA:
1889 case LVN_BEGINLABELEDITW:
1890 {
1891 wxLV_ITEM item;
1892 if ( nmhdr->code == LVN_BEGINLABELEDITA )
1893 {
1894 item.Init(((LV_DISPINFOA *)lParam)->item);
1895 }
1896 else // LVN_BEGINLABELEDITW
1897 {
1898 item.Init(((LV_DISPINFOW *)lParam)->item);
1899 }
1900
1901 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1902 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1903 event.m_itemIndex = event.m_item.m_itemId;
1904 }
1905 break;
1906
1907 case LVN_ENDLABELEDITA:
1908 case LVN_ENDLABELEDITW:
1909 {
1910 wxLV_ITEM item;
1911 if ( nmhdr->code == LVN_ENDLABELEDITA )
1912 {
1913 item.Init(((LV_DISPINFOA *)lParam)->item);
1914 }
1915 else // LVN_ENDLABELEDITW
1916 {
1917 item.Init(((LV_DISPINFOW *)lParam)->item);
1918 }
1919
1920 // was editing cancelled?
1921 const LV_ITEM& lvi = (LV_ITEM)item;
1922 if ( !lvi.pszText || lvi.iItem == -1 )
1923 {
1924 // don't keep a stale wxTextCtrl around
1925 if ( m_textCtrl )
1926 {
1927 // EDIT control will be deleted by the list control itself so
1928 // prevent us from deleting it as well
1929 m_textCtrl->UnsubclassWin();
1930 m_textCtrl->SetHWND(0);
1931 delete m_textCtrl;
1932 m_textCtrl = NULL;
1933 }
1934
1935 event.SetEditCanceled(true);
1936 }
1937
1938 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1939 wxConvertFromMSWListItem(NULL, event.m_item, item);
1940 event.m_itemIndex = event.m_item.m_itemId;
1941 }
1942 break;
1943
1944 case LVN_COLUMNCLICK:
1945 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1946 event.m_itemIndex = -1;
1947 event.m_col = nmLV->iSubItem;
1948 break;
1949
1950 case LVN_DELETEALLITEMS:
1951 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1952 event.m_itemIndex = -1;
1953 break;
1954
1955 case LVN_DELETEITEM:
1956 if ( m_count == 0 )
1957 {
1958 // this should be prevented by the post-processing code
1959 // below, but "just in case"
1960 return false;
1961 }
1962
1963 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1964 event.m_itemIndex = iItem;
1965 // delete the assoicated internal data
1966 wxDeleteInternalData(this, iItem);
1967 break;
1968
1969 #if WXWIN_COMPATIBILITY_2_4
1970 case LVN_SETDISPINFO:
1971 {
1972 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1973 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1974 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
1975 }
1976 break;
1977 #endif
1978
1979 case LVN_INSERTITEM:
1980 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1981 event.m_itemIndex = iItem;
1982 break;
1983
1984 case LVN_ITEMCHANGED:
1985 // we translate this catch all message into more interesting
1986 // (and more easy to process) wxWidgets events
1987
1988 // first of all, we deal with the state change events only and
1989 // only for valid items (item == -1 for the virtual list
1990 // control)
1991 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
1992 {
1993 // temp vars for readability
1994 const UINT stOld = nmLV->uOldState;
1995 const UINT stNew = nmLV->uNewState;
1996
1997 event.m_item.SetId(iItem);
1998 event.m_item.SetMask(wxLIST_MASK_TEXT |
1999 wxLIST_MASK_IMAGE |
2000 wxLIST_MASK_DATA);
2001 GetItem(event.m_item);
2002
2003 // has the focus changed?
2004 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2005 {
2006 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2007 event.m_itemIndex = iItem;
2008 }
2009
2010 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2011 {
2012 if ( eventType != wxEVT_NULL )
2013 {
2014 // focus and selection have both changed: send the
2015 // focus event from here and the selection one
2016 // below
2017 event.SetEventType(eventType);
2018 (void)GetEventHandler()->ProcessEvent(event);
2019 }
2020 else // no focus event to send
2021 {
2022 // then need to set m_itemIndex as it wasn't done
2023 // above
2024 event.m_itemIndex = iItem;
2025 }
2026
2027 eventType = stNew & LVIS_SELECTED
2028 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2029 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2030 }
2031 }
2032
2033 if ( eventType == wxEVT_NULL )
2034 {
2035 // not an interesting event for us
2036 return false;
2037 }
2038
2039 break;
2040
2041 case LVN_KEYDOWN:
2042 {
2043 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2044 WORD wVKey = info->wVKey;
2045
2046 // get the current selection
2047 long lItem = GetNextItem(-1,
2048 wxLIST_NEXT_ALL,
2049 wxLIST_STATE_SELECTED);
2050
2051 // <Enter> or <Space> activate the selected item if any (but
2052 // not with Shift and/or Ctrl as then they have a predefined
2053 // meaning for the list view)
2054 if ( lItem != -1 &&
2055 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
2056 !(wxIsShiftDown() || wxIsCtrlDown()) )
2057 {
2058 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2059 }
2060 else
2061 {
2062 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
2063
2064 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2065 // value which should be used as is
2066 int code = wxCharCodeMSWToWX(wVKey);
2067 event.m_code = code ? code : wVKey;
2068 }
2069
2070 event.m_itemIndex =
2071 event.m_item.m_itemId = lItem;
2072
2073 if ( lItem != -1 )
2074 {
2075 // fill the other fields too
2076 event.m_item.m_text = GetItemText(lItem);
2077 event.m_item.m_data = GetItemData(lItem);
2078 }
2079 }
2080 break;
2081
2082 case NM_DBLCLK:
2083 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2084 // anything else
2085 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2086 {
2087 return true;
2088 }
2089
2090 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2091 // if it happened on an item (and not on empty place)
2092 if ( iItem == -1 )
2093 {
2094 // not on item
2095 return false;
2096 }
2097
2098 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2099 event.m_itemIndex = iItem;
2100 event.m_item.m_text = GetItemText(iItem);
2101 event.m_item.m_data = GetItemData(iItem);
2102 break;
2103
2104 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2105 case GN_CONTEXTMENU:
2106 #endif //__WXWINCE__
2107 case NM_RCLICK:
2108 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2109 // don't do anything else
2110 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2111 {
2112 return true;
2113 }
2114
2115 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2116 LV_HITTESTINFO lvhti;
2117 wxZeroMemory(lvhti);
2118
2119 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2120 if(nmhdr->code == GN_CONTEXTMENU) {
2121 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2122 } else
2123 #endif //__WXWINCE__
2124 ::GetCursorPos(&(lvhti.pt));
2125 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
2126 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
2127 {
2128 if ( lvhti.flags & LVHT_ONITEM )
2129 {
2130 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2131 event.m_itemIndex = lvhti.iItem;
2132 event.m_pointDrag.x = lvhti.pt.x;
2133 event.m_pointDrag.y = lvhti.pt.y;
2134 }
2135 }
2136 break;
2137
2138 #ifdef NM_CUSTOMDRAW
2139 case NM_CUSTOMDRAW:
2140 *result = OnCustomDraw(lParam);
2141
2142 return true;
2143 #endif // _WIN32_IE >= 0x300
2144
2145 case LVN_ODCACHEHINT:
2146 {
2147 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
2148
2149 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
2150
2151 // we get some really stupid cache hints like ones for
2152 // items in range 0..0 for an empty control or, after
2153 // deleting an item, for items in invalid range -- filter
2154 // this garbage out
2155 if ( cacheHint->iFrom > cacheHint->iTo )
2156 return false;
2157
2158 event.m_oldItemIndex = cacheHint->iFrom;
2159
2160 const long iMax = GetItemCount();
2161 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2162 : iMax - 1;
2163 }
2164 break;
2165
2166 #ifdef HAVE_NMLVFINDITEM
2167 case LVN_ODFINDITEM:
2168 // this message is only used with the virtual list control but
2169 // even there we don't want to always use it: in a control with
2170 // sufficiently big number of items (defined as > 1000 here),
2171 // accidentally pressing a key could result in hanging an
2172 // application waiting while it performs linear search
2173 if ( IsVirtual() && GetItemCount() <= 1000 )
2174 {
2175 NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
2176
2177 // no match by default
2178 *result = -1;
2179
2180 // we only handle string-based searches here
2181 //
2182 // TODO: what about LVFI_PARTIAL, should we handle this?
2183 if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
2184 {
2185 return false;
2186 }
2187
2188 const wxChar * const searchstr = pFindInfo->lvfi.psz;
2189 const size_t len = wxStrlen(searchstr);
2190
2191 // this is the first item we should examine, search from it
2192 // wrapping if necessary
2193 const int startPos = pFindInfo->iStart;
2194 const int maxPos = GetItemCount();
2195 wxCHECK_MSG( startPos <= maxPos, false,
2196 _T("bad starting position in LVN_ODFINDITEM") );
2197
2198 int currentPos = startPos;
2199 do
2200 {
2201 // wrap to the beginning if necessary
2202 if ( currentPos == maxPos )
2203 {
2204 // somewhat surprizingly, LVFI_WRAP isn't set in
2205 // flags but we still should wrap
2206 currentPos = 0;
2207 }
2208
2209 // does this item begin with searchstr?
2210 if ( wxStrnicmp(searchstr,
2211 GetItemText(currentPos), len) == 0 )
2212 {
2213 *result = currentPos;
2214 break;
2215 }
2216 }
2217 while ( ++currentPos != startPos );
2218
2219 if ( *result == -1 )
2220 {
2221 // not found
2222 return false;
2223 }
2224
2225 SetItemState(*result,
2226 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
2227 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
2228 EnsureVisible(*result);
2229 return true;
2230 }
2231 else
2232 {
2233 processed = false;
2234 }
2235 break;
2236 #endif // HAVE_NMLVFINDITEM
2237
2238 case LVN_GETDISPINFO:
2239 if ( IsVirtual() )
2240 {
2241 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2242
2243 LV_ITEM& lvi = info->item;
2244 long item = lvi.iItem;
2245
2246 if ( lvi.mask & LVIF_TEXT )
2247 {
2248 wxString text = OnGetItemText(item, lvi.iSubItem);
2249 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
2250 }
2251
2252 // see comment at the end of wxListCtrl::GetColumn()
2253 #ifdef NM_CUSTOMDRAW
2254 if ( lvi.mask & LVIF_IMAGE )
2255 {
2256 lvi.iImage = OnGetItemImage(item);
2257 }
2258 #endif // NM_CUSTOMDRAW
2259
2260 // a little dose of healthy paranoia: as we never use
2261 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2262 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
2263 _T("we don't support state callbacks yet!") );
2264
2265 return true;
2266 }
2267 // fall through
2268
2269 default:
2270 processed = false;
2271 }
2272
2273 if ( !processed )
2274 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2275 }
2276 else
2277 {
2278 // where did this one come from?
2279 return false;
2280 }
2281
2282 // process the event
2283 // -----------------
2284
2285 event.SetEventType(eventType);
2286
2287 bool processed = GetEventHandler()->ProcessEvent(event);
2288
2289 // post processing
2290 // ---------------
2291 switch ( nmhdr->code )
2292 {
2293 case LVN_DELETEALLITEMS:
2294 // always return true to suppress all additional LVN_DELETEITEM
2295 // notifications - this makes deleting all items from a list ctrl
2296 // much faster
2297 *result = TRUE;
2298
2299 // also, we may free all user data now (couldn't do it before as
2300 // the user should have access to it in OnDeleteAllItems() handler)
2301 FreeAllInternalData();
2302
2303 // the control is empty now, synchronize the cached number of items
2304 // with the real one
2305 m_count = 0;
2306 return true;
2307
2308 case LVN_ENDLABELEDITA:
2309 case LVN_ENDLABELEDITW:
2310 // logic here is inverted compared to all the other messages
2311 *result = event.IsAllowed();
2312
2313 // don't keep a stale wxTextCtrl around
2314 if ( m_textCtrl )
2315 {
2316 // EDIT control will be deleted by the list control itself so
2317 // prevent us from deleting it as well
2318 m_textCtrl->UnsubclassWin();
2319 m_textCtrl->SetHWND(0);
2320 delete m_textCtrl;
2321 m_textCtrl = NULL;
2322 }
2323
2324 return true;
2325 }
2326
2327 if ( processed )
2328 *result = !event.IsAllowed();
2329
2330 return processed;
2331 }
2332
2333 // see comment at the end of wxListCtrl::GetColumn()
2334 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2335
2336 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2337 {
2338 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
2339 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
2340 switch ( nmcd.dwDrawStage )
2341 {
2342 case CDDS_PREPAINT:
2343 // if we've got any items with non standard attributes,
2344 // notify us before painting each item
2345 //
2346 // for virtual controls, always suppose that we have attributes as
2347 // there is no way to check for this
2348 return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2349 : CDRF_DODEFAULT;
2350
2351 case CDDS_ITEMPREPAINT:
2352 {
2353 size_t item = (size_t)nmcd.dwItemSpec;
2354 if ( item >= (size_t)GetItemCount() )
2355 {
2356 // we get this message with item == 0 for an empty control,
2357 // we must ignore it as calling OnGetItemAttr() would be
2358 // wrong
2359 return CDRF_DODEFAULT;
2360 }
2361
2362 wxListItemAttr *attr =
2363 IsVirtual() ? OnGetItemAttr(item)
2364 : wxGetInternalDataAttr(this, item);
2365
2366 if ( !attr )
2367 {
2368 // nothing to do for this item
2369 return CDRF_DODEFAULT;
2370 }
2371
2372 HFONT hFont;
2373 wxColour colText, colBack;
2374 if ( attr->HasFont() )
2375 {
2376 wxFont font = attr->GetFont();
2377 hFont = (HFONT)font.GetResourceHandle();
2378 }
2379 else
2380 {
2381 hFont = 0;
2382 }
2383
2384 if ( attr->HasTextColour() )
2385 {
2386 colText = attr->GetTextColour();
2387 }
2388 else
2389 {
2390 colText = GetTextColour();
2391 }
2392
2393 if ( attr->HasBackgroundColour() )
2394 {
2395 colBack = attr->GetBackgroundColour();
2396 }
2397 else
2398 {
2399 colBack = GetBackgroundColour();
2400 }
2401
2402 lplvcd->clrText = wxColourToRGB(colText);
2403 lplvcd->clrTextBk = wxColourToRGB(colBack);
2404
2405 // note that if we wanted to set colours for
2406 // individual columns (subitems), we would have
2407 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2408 if ( hFont )
2409 {
2410 ::SelectObject(nmcd.hdc, hFont);
2411
2412 return CDRF_NEWFONT;
2413 }
2414 }
2415 // fall through to return CDRF_DODEFAULT
2416
2417 default:
2418 return CDRF_DODEFAULT;
2419 }
2420 }
2421
2422 #endif // NM_CUSTOMDRAW supported
2423
2424 // Necessary for drawing hrules and vrules, if specified
2425 void wxListCtrl::OnPaint(wxPaintEvent& event)
2426 {
2427 wxPaintDC dc(this);
2428
2429 wxControl::OnPaint(event);
2430
2431 // Reset the device origin since it may have been set
2432 dc.SetDeviceOrigin(0, 0);
2433
2434 bool drawHRules = HasFlag(wxLC_HRULES);
2435 bool drawVRules = HasFlag(wxLC_VRULES);
2436
2437 if (!InReportView() || !drawHRules && !drawVRules)
2438 return;
2439
2440 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2441 dc.SetPen(pen);
2442 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2443
2444 wxSize clientSize = GetClientSize();
2445 wxRect itemRect;
2446
2447 int itemCount = GetItemCount();
2448 int i;
2449 if (drawHRules)
2450 {
2451 long top = GetTopItem();
2452 for (i = top; i < top + GetCountPerPage() + 1; i++)
2453 {
2454 if (GetItemRect(i, itemRect))
2455 {
2456 int cy = itemRect.GetTop();
2457 if (i != 0) // Don't draw the first one
2458 {
2459 dc.DrawLine(0, cy, clientSize.x, cy);
2460 }
2461 // Draw last line
2462 if (i == itemCount - 1)
2463 {
2464 cy = itemRect.GetBottom();
2465 dc.DrawLine(0, cy, clientSize.x, cy);
2466 }
2467 }
2468 }
2469 }
2470 i = itemCount - 1;
2471 if (drawVRules && (i > -1))
2472 {
2473 wxRect firstItemRect;
2474 GetItemRect(0, firstItemRect);
2475
2476 if (GetItemRect(i, itemRect))
2477 {
2478 int col;
2479 int x = itemRect.GetX();
2480 for (col = 0; col < GetColumnCount(); col++)
2481 {
2482 int colWidth = GetColumnWidth(col);
2483 x += colWidth ;
2484 dc.DrawLine(x-1, firstItemRect.GetY() - 2, x-1, itemRect.GetBottom());
2485 }
2486 }
2487 }
2488 }
2489
2490 WXLRESULT
2491 wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2492 {
2493 #ifdef WM_PRINT
2494 if ( nMsg == WM_PRINT )
2495 {
2496 // we should bypass our own WM_PRINT handling as we don't handle
2497 // PRF_CHILDREN flag, so leave it to the native control itself
2498 return MSWDefWindowProc(nMsg, wParam, lParam);
2499 }
2500 #endif // WM_PRINT
2501
2502 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
2503 }
2504
2505 // ----------------------------------------------------------------------------
2506 // virtual list controls
2507 // ----------------------------------------------------------------------------
2508
2509 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2510 {
2511 // this is a pure virtual function, in fact - which is not really pure
2512 // because the controls which are not virtual don't need to implement it
2513 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2514
2515 return wxEmptyString;
2516 }
2517
2518 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2519 {
2520 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2521 -1,
2522 wxT("List control has an image list, OnGetItemImage should be overridden."));
2523 return -1;
2524 }
2525
2526 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2527 {
2528 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2529 _T("invalid item index in OnGetItemAttr()") );
2530
2531 // no attributes by default
2532 return NULL;
2533 }
2534
2535 void wxListCtrl::SetItemCount(long count)
2536 {
2537 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2538
2539 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2540 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2541 {
2542 wxLogLastError(_T("ListView_SetItemCount"));
2543 }
2544 m_count = count;
2545 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2546 wxT("m_count should match ListView_GetItemCount"));
2547 }
2548
2549 void wxListCtrl::RefreshItem(long item)
2550 {
2551 // strangely enough, ListView_Update() results in much more flicker here
2552 // than a dumb Refresh() -- why?
2553 #if 0
2554 if ( !ListView_Update(GetHwnd(), item) )
2555 {
2556 wxLogLastError(_T("ListView_Update"));
2557 }
2558 #else // 1
2559 wxRect rect;
2560 GetItemRect(item, rect);
2561 RefreshRect(rect);
2562 #endif // 0/1
2563 }
2564
2565 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2566 {
2567 wxRect rect1, rect2;
2568 GetItemRect(itemFrom, rect1);
2569 GetItemRect(itemTo, rect2);
2570
2571 wxRect rect = rect1;
2572 rect.height = rect2.GetBottom() - rect1.GetTop();
2573
2574 RefreshRect(rect);
2575 }
2576
2577 // ----------------------------------------------------------------------------
2578 // internal data stuff
2579 // ----------------------------------------------------------------------------
2580
2581 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2582 {
2583 LV_ITEM it;
2584 it.mask = LVIF_PARAM;
2585 it.iItem = itemId;
2586
2587 if ( !ListView_GetItem(hwnd, &it) )
2588 return NULL;
2589
2590 return (wxListItemInternalData *) it.lParam;
2591 }
2592
2593 static
2594 wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
2595 {
2596 return wxGetInternalData(GetHwndOf(ctl), itemId);
2597 }
2598
2599 static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId)
2600 {
2601 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2602
2603 return data ? data->attr : NULL;
2604 }
2605
2606 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2607 {
2608 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2609 if (data)
2610 {
2611 LV_ITEM item;
2612 memset(&item, 0, sizeof(item));
2613 item.iItem = itemId;
2614 item.mask = LVIF_PARAM;
2615 item.lParam = (LPARAM) 0;
2616 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2617 delete data;
2618 }
2619 }
2620
2621 // ----------------------------------------------------------------------------
2622 // wxWin <-> MSW items conversions
2623 // ----------------------------------------------------------------------------
2624
2625 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2626 wxListItem& info,
2627 LV_ITEM& lvItem)
2628 {
2629 wxListItemInternalData *internaldata =
2630 (wxListItemInternalData *) lvItem.lParam;
2631
2632 if (internaldata)
2633 info.m_data = internaldata->lParam;
2634
2635 info.m_mask = 0;
2636 info.m_state = 0;
2637 info.m_stateMask = 0;
2638 info.m_itemId = lvItem.iItem;
2639
2640 long oldMask = lvItem.mask;
2641
2642 bool needText = false;
2643 if (hwndListCtrl != 0)
2644 {
2645 if ( lvItem.mask & LVIF_TEXT )
2646 needText = false;
2647 else
2648 needText = true;
2649
2650 if ( needText )
2651 {
2652 lvItem.pszText = new wxChar[513];
2653 lvItem.cchTextMax = 512;
2654 }
2655 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2656 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2657 }
2658
2659 if ( lvItem.mask & LVIF_STATE )
2660 {
2661 info.m_mask |= wxLIST_MASK_STATE;
2662
2663 if ( lvItem.stateMask & LVIS_CUT)
2664 {
2665 info.m_stateMask |= wxLIST_STATE_CUT;
2666 if ( lvItem.state & LVIS_CUT )
2667 info.m_state |= wxLIST_STATE_CUT;
2668 }
2669 if ( lvItem.stateMask & LVIS_DROPHILITED)
2670 {
2671 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2672 if ( lvItem.state & LVIS_DROPHILITED )
2673 info.m_state |= wxLIST_STATE_DROPHILITED;
2674 }
2675 if ( lvItem.stateMask & LVIS_FOCUSED)
2676 {
2677 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2678 if ( lvItem.state & LVIS_FOCUSED )
2679 info.m_state |= wxLIST_STATE_FOCUSED;
2680 }
2681 if ( lvItem.stateMask & LVIS_SELECTED)
2682 {
2683 info.m_stateMask |= wxLIST_STATE_SELECTED;
2684 if ( lvItem.state & LVIS_SELECTED )
2685 info.m_state |= wxLIST_STATE_SELECTED;
2686 }
2687 }
2688
2689 if ( lvItem.mask & LVIF_TEXT )
2690 {
2691 info.m_mask |= wxLIST_MASK_TEXT;
2692 info.m_text = lvItem.pszText;
2693 }
2694 if ( lvItem.mask & LVIF_IMAGE )
2695 {
2696 info.m_mask |= wxLIST_MASK_IMAGE;
2697 info.m_image = lvItem.iImage;
2698 }
2699 if ( lvItem.mask & LVIF_PARAM )
2700 info.m_mask |= wxLIST_MASK_DATA;
2701 if ( lvItem.mask & LVIF_DI_SETITEM )
2702 info.m_mask |= wxLIST_SET_ITEM;
2703 info.m_col = lvItem.iSubItem;
2704
2705 if (needText)
2706 {
2707 if (lvItem.pszText)
2708 delete[] lvItem.pszText;
2709 }
2710 lvItem.mask = oldMask;
2711 }
2712
2713 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2714 {
2715 if (stateMask & wxLIST_STATE_CUT)
2716 {
2717 lvItem.stateMask |= LVIS_CUT;
2718 if (state & wxLIST_STATE_CUT)
2719 lvItem.state |= LVIS_CUT;
2720 }
2721 if (stateMask & wxLIST_STATE_DROPHILITED)
2722 {
2723 lvItem.stateMask |= LVIS_DROPHILITED;
2724 if (state & wxLIST_STATE_DROPHILITED)
2725 lvItem.state |= LVIS_DROPHILITED;
2726 }
2727 if (stateMask & wxLIST_STATE_FOCUSED)
2728 {
2729 lvItem.stateMask |= LVIS_FOCUSED;
2730 if (state & wxLIST_STATE_FOCUSED)
2731 lvItem.state |= LVIS_FOCUSED;
2732 }
2733 if (stateMask & wxLIST_STATE_SELECTED)
2734 {
2735 lvItem.stateMask |= LVIS_SELECTED;
2736 if (state & wxLIST_STATE_SELECTED)
2737 lvItem.state |= LVIS_SELECTED;
2738 }
2739 }
2740
2741 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2742 const wxListItem& info,
2743 LV_ITEM& lvItem)
2744 {
2745 lvItem.iItem = (int) info.m_itemId;
2746
2747 lvItem.iImage = info.m_image;
2748 lvItem.stateMask = 0;
2749 lvItem.state = 0;
2750 lvItem.mask = 0;
2751 lvItem.iSubItem = info.m_col;
2752
2753 if (info.m_mask & wxLIST_MASK_STATE)
2754 {
2755 lvItem.mask |= LVIF_STATE;
2756
2757 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
2758 }
2759
2760 if (info.m_mask & wxLIST_MASK_TEXT)
2761 {
2762 lvItem.mask |= LVIF_TEXT;
2763 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
2764 {
2765 lvItem.pszText = LPSTR_TEXTCALLBACK;
2766 }
2767 else
2768 {
2769 // pszText is not const, hence the cast
2770 lvItem.pszText = (wxChar *)info.m_text.c_str();
2771 if ( lvItem.pszText )
2772 lvItem.cchTextMax = info.m_text.length();
2773 else
2774 lvItem.cchTextMax = 0;
2775 }
2776 }
2777 if (info.m_mask & wxLIST_MASK_IMAGE)
2778 lvItem.mask |= LVIF_IMAGE;
2779 }
2780
2781 static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
2782 LV_COLUMN& lvCol)
2783 {
2784 wxZeroMemory(lvCol);
2785
2786 if ( item.m_mask & wxLIST_MASK_TEXT )
2787 {
2788 lvCol.mask |= LVCF_TEXT;
2789 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
2790 }
2791
2792 if ( item.m_mask & wxLIST_MASK_FORMAT )
2793 {
2794 lvCol.mask |= LVCF_FMT;
2795
2796 if ( item.m_format == wxLIST_FORMAT_LEFT )
2797 lvCol.fmt = LVCFMT_LEFT;
2798 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
2799 lvCol.fmt = LVCFMT_RIGHT;
2800 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
2801 lvCol.fmt = LVCFMT_CENTER;
2802 }
2803
2804 if ( item.m_mask & wxLIST_MASK_WIDTH )
2805 {
2806 lvCol.mask |= LVCF_WIDTH;
2807 if ( item.m_width == wxLIST_AUTOSIZE)
2808 lvCol.cx = LVSCW_AUTOSIZE;
2809 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2810 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
2811 else
2812 lvCol.cx = item.m_width;
2813 }
2814
2815 // see comment at the end of wxListCtrl::GetColumn()
2816 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2817 if ( item.m_mask & wxLIST_MASK_IMAGE )
2818 {
2819 if ( wxTheApp->GetComCtl32Version() >= 470 )
2820 {
2821 lvCol.mask |= LVCF_IMAGE | LVCF_FMT;
2822
2823 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2824 // seem to be generally nicer than on the left and the generic
2825 // version only draws them on the right (we don't have a flag to
2826 // specify the image location anyhow)
2827 //
2828 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2829 // make any difference in my tests -- but maybe we should?
2830 if ( item.m_image != -1 )
2831 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
2832
2833 lvCol.iImage = item.m_image;
2834 }
2835 //else: it doesn't support item images anyhow
2836 }
2837 #endif // _WIN32_IE >= 0x0300
2838 }
2839
2840 #endif // wxUSE_LISTCTRL