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