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