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