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