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