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