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