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