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