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