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