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