]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
dumb fix for wxDC::Clear() crash
[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 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
914 {
915 wxListItem info;
916 info.m_itemId = item;
917 info.SetTextColour( col );
918 SetItem( info );
919 }
920
921 wxColour wxListCtrl::GetItemTextColour( long item ) const
922 {
923 wxListItem info;
924 info.m_itemId = item;
925 GetItem( info );
926 return info.GetTextColour();
927 }
928
929 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
930 {
931 wxListItem info;
932 info.m_itemId = item;
933 info.SetBackgroundColour( col );
934 SetItem( info );
935 }
936
937 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
938 {
939 wxListItem info;
940 info.m_itemId = item;
941 GetItem( info );
942 return info.GetBackgroundColour();
943 }
944
945 // Gets the number of selected items in the list control
946 int wxListCtrl::GetSelectedItemCount() const
947 {
948 return ListView_GetSelectedCount(GetHwnd());
949 }
950
951 // Gets the text colour of the listview
952 wxColour wxListCtrl::GetTextColour() const
953 {
954 COLORREF ref = ListView_GetTextColor(GetHwnd());
955 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
956 return col;
957 }
958
959 // Sets the text colour of the listview
960 void wxListCtrl::SetTextColour(const wxColour& col)
961 {
962 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
963 }
964
965 // Gets the index of the topmost visible item when in
966 // list or report view
967 long wxListCtrl::GetTopItem() const
968 {
969 return (long) ListView_GetTopIndex(GetHwnd());
970 }
971
972 // Searches for an item, starting from 'item'.
973 // 'geometry' is one of
974 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
975 // 'state' is a state bit flag, one or more of
976 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
977 // item can be -1 to find the first item that matches the
978 // specified flags.
979 // Returns the item or -1 if unsuccessful.
980 long wxListCtrl::GetNextItem(long item, int geom, int state) const
981 {
982 long flags = 0;
983
984 if ( geom == wxLIST_NEXT_ABOVE )
985 flags |= LVNI_ABOVE;
986 if ( geom == wxLIST_NEXT_ALL )
987 flags |= LVNI_ALL;
988 if ( geom == wxLIST_NEXT_BELOW )
989 flags |= LVNI_BELOW;
990 if ( geom == wxLIST_NEXT_LEFT )
991 flags |= LVNI_TOLEFT;
992 if ( geom == wxLIST_NEXT_RIGHT )
993 flags |= LVNI_TORIGHT;
994
995 if ( state & wxLIST_STATE_CUT )
996 flags |= LVNI_CUT;
997 if ( state & wxLIST_STATE_DROPHILITED )
998 flags |= LVNI_DROPHILITED;
999 if ( state & wxLIST_STATE_FOCUSED )
1000 flags |= LVNI_FOCUSED;
1001 if ( state & wxLIST_STATE_SELECTED )
1002 flags |= LVNI_SELECTED;
1003
1004 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1005 }
1006
1007
1008 wxImageList *wxListCtrl::GetImageList(int which) const
1009 {
1010 if ( which == wxIMAGE_LIST_NORMAL )
1011 {
1012 return m_imageListNormal;
1013 }
1014 else if ( which == wxIMAGE_LIST_SMALL )
1015 {
1016 return m_imageListSmall;
1017 }
1018 else if ( which == wxIMAGE_LIST_STATE )
1019 {
1020 return m_imageListState;
1021 }
1022 return NULL;
1023 }
1024
1025 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1026 {
1027 int flags = 0;
1028 if ( which == wxIMAGE_LIST_NORMAL )
1029 {
1030 flags = LVSIL_NORMAL;
1031 if (m_ownsImageListNormal) delete m_imageListNormal;
1032 m_imageListNormal = imageList;
1033 m_ownsImageListNormal = FALSE;
1034 }
1035 else if ( which == wxIMAGE_LIST_SMALL )
1036 {
1037 flags = LVSIL_SMALL;
1038 if (m_ownsImageListSmall) delete m_imageListSmall;
1039 m_imageListSmall = imageList;
1040 m_ownsImageListSmall = FALSE;
1041 }
1042 else if ( which == wxIMAGE_LIST_STATE )
1043 {
1044 flags = LVSIL_STATE;
1045 if (m_ownsImageListState) delete m_imageListState;
1046 m_imageListState = imageList;
1047 m_ownsImageListState = FALSE;
1048 }
1049 ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1050 }
1051
1052 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1053 {
1054 SetImageList(imageList, which);
1055 if ( which == wxIMAGE_LIST_NORMAL )
1056 m_ownsImageListNormal = TRUE;
1057 else if ( which == wxIMAGE_LIST_SMALL )
1058 m_ownsImageListSmall = TRUE;
1059 else if ( which == wxIMAGE_LIST_STATE )
1060 m_ownsImageListState = TRUE;
1061 }
1062
1063 // ----------------------------------------------------------------------------
1064 // Operations
1065 // ----------------------------------------------------------------------------
1066
1067 // Arranges the items
1068 bool wxListCtrl::Arrange(int flag)
1069 {
1070 UINT code = 0;
1071 if ( flag == wxLIST_ALIGN_LEFT )
1072 code = LVA_ALIGNLEFT;
1073 else if ( flag == wxLIST_ALIGN_TOP )
1074 code = LVA_ALIGNTOP;
1075 else if ( flag == wxLIST_ALIGN_DEFAULT )
1076 code = LVA_DEFAULT;
1077 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1078 code = LVA_SNAPTOGRID;
1079
1080 return (ListView_Arrange(GetHwnd(), code) != 0);
1081 }
1082
1083 // Deletes an item
1084 bool wxListCtrl::DeleteItem(long item)
1085 {
1086 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1087 {
1088 wxLogLastError(_T("ListView_DeleteItem"));
1089 return FALSE;
1090 }
1091
1092 // the virtual list control doesn't refresh itself correctly, help it
1093 if ( IsVirtual() )
1094 {
1095 // we need to refresh all the lines below the one which was deleted
1096 wxRect rectItem;
1097 if ( item > 0 && GetItemCount() )
1098 {
1099 GetItemRect(item - 1, rectItem);
1100 }
1101 else
1102 {
1103 rectItem.y =
1104 rectItem.height = 0;
1105 }
1106
1107 wxRect rectWin = GetRect();
1108 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1109 rectWin.y = rectItem.GetBottom();
1110
1111 RefreshRect(rectWin);
1112 }
1113
1114 return TRUE;
1115 }
1116
1117 // Deletes all items
1118 bool wxListCtrl::DeleteAllItems()
1119 {
1120 return ListView_DeleteAllItems(GetHwnd()) != 0;
1121 }
1122
1123 // Deletes all items
1124 bool wxListCtrl::DeleteAllColumns()
1125 {
1126 while ( m_colCount > 0 )
1127 {
1128 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1129 {
1130 wxLogLastError(wxT("ListView_DeleteColumn"));
1131
1132 return FALSE;
1133 }
1134
1135 m_colCount--;
1136 }
1137
1138 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1139
1140 return TRUE;
1141 }
1142
1143 // Deletes a column
1144 bool wxListCtrl::DeleteColumn(int col)
1145 {
1146 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1147
1148 if ( success && (m_colCount > 0) )
1149 m_colCount --;
1150 return success;
1151 }
1152
1153 // Clears items, and columns if there are any.
1154 void wxListCtrl::ClearAll()
1155 {
1156 DeleteAllItems();
1157 if ( m_colCount > 0 )
1158 DeleteAllColumns();
1159 }
1160
1161 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1162 {
1163 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1164
1165 // VS: ListView_EditLabel requires that the list has focus.
1166 SetFocus();
1167 HWND hWnd = (HWND) ListView_EditLabel(GetHwnd(), item);
1168
1169 if (m_textCtrl)
1170 {
1171 m_textCtrl->SetHWND(0);
1172 m_textCtrl->UnsubclassWin();
1173 delete m_textCtrl;
1174 m_textCtrl = NULL;
1175 }
1176
1177 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
1178 m_textCtrl->SetHWND((WXHWND) hWnd);
1179 m_textCtrl->SubclassWin((WXHWND) hWnd);
1180
1181 return m_textCtrl;
1182 }
1183
1184 // End label editing, optionally cancelling the edit
1185 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel))
1186 {
1187 wxFAIL_MSG( _T("not implemented") );
1188
1189 return FALSE;
1190 }
1191
1192 // Ensures this item is visible
1193 bool wxListCtrl::EnsureVisible(long item)
1194 {
1195 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0;
1196 }
1197
1198 // Find an item whose label matches this string, starting from the item after 'start'
1199 // or the beginning if 'start' is -1.
1200 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1201 {
1202 LV_FINDINFO findInfo;
1203
1204 findInfo.flags = LVFI_STRING;
1205 if ( partial )
1206 findInfo.flags |= LVFI_PARTIAL;
1207 findInfo.psz = str;
1208
1209 // ListView_FindItem() excludes the first item from search and to look
1210 // through all the items you need to start from -1 which is unnatural and
1211 // inconsistent with the generic version - so we adjust the index
1212 if (start != -1)
1213 start --;
1214 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1215 }
1216
1217 // Find an item whose data matches this data, starting from the item after 'start'
1218 // or the beginning if 'start' is -1.
1219 long wxListCtrl::FindItem(long start, long data)
1220 {
1221 LV_FINDINFO findInfo;
1222
1223 findInfo.flags = LVFI_PARAM;
1224 findInfo.lParam = data;
1225
1226 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1227 }
1228
1229 // Find an item nearest this position in the specified direction, starting from
1230 // the item after 'start' or the beginning if 'start' is -1.
1231 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1232 {
1233 LV_FINDINFO findInfo;
1234
1235 findInfo.flags = LVFI_NEARESTXY;
1236 findInfo.pt.x = pt.x;
1237 findInfo.pt.y = pt.y;
1238 findInfo.vkDirection = VK_RIGHT;
1239
1240 if ( direction == wxLIST_FIND_UP )
1241 findInfo.vkDirection = VK_UP;
1242 else if ( direction == wxLIST_FIND_DOWN )
1243 findInfo.vkDirection = VK_DOWN;
1244 else if ( direction == wxLIST_FIND_LEFT )
1245 findInfo.vkDirection = VK_LEFT;
1246 else if ( direction == wxLIST_FIND_RIGHT )
1247 findInfo.vkDirection = VK_RIGHT;
1248
1249 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1250 }
1251
1252 // Determines which item (if any) is at the specified point,
1253 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1254 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1255 {
1256 LV_HITTESTINFO hitTestInfo;
1257 hitTestInfo.pt.x = (int) point.x;
1258 hitTestInfo.pt.y = (int) point.y;
1259
1260 ListView_HitTest(GetHwnd(), & hitTestInfo);
1261
1262 flags = 0;
1263 if ( hitTestInfo.flags & LVHT_ABOVE )
1264 flags |= wxLIST_HITTEST_ABOVE;
1265 if ( hitTestInfo.flags & LVHT_BELOW )
1266 flags |= wxLIST_HITTEST_BELOW;
1267 if ( hitTestInfo.flags & LVHT_NOWHERE )
1268 flags |= wxLIST_HITTEST_NOWHERE;
1269 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1270 flags |= wxLIST_HITTEST_ONITEMICON;
1271 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1272 flags |= wxLIST_HITTEST_ONITEMLABEL;
1273 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1274 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1275 if ( hitTestInfo.flags & LVHT_TOLEFT )
1276 flags |= wxLIST_HITTEST_TOLEFT;
1277 if ( hitTestInfo.flags & LVHT_TORIGHT )
1278 flags |= wxLIST_HITTEST_TORIGHT;
1279
1280 return (long) hitTestInfo.iItem;
1281 }
1282
1283 // Inserts an item, returning the index of the new item if successful,
1284 // -1 otherwise.
1285 long wxListCtrl::InsertItem(wxListItem& info)
1286 {
1287 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1288
1289 LV_ITEM item;
1290 wxConvertToMSWListItem(this, info, item);
1291
1292 // check whether it has any custom attributes
1293 if ( info.HasAttributes() )
1294 {
1295
1296 wxListItemAttr *attr;
1297 attr = (wxListItemAttr*) m_attrs.Get(item.iItem);
1298
1299 if (attr == NULL)
1300
1301 m_attrs.Put(item.iItem, (wxObject *)new wxListItemAttr(*info.GetAttributes()));
1302
1303 else *attr = *info.GetAttributes();
1304
1305 m_hasAnyAttr = TRUE;
1306 }
1307
1308 return (long) ListView_InsertItem(GetHwnd(), & item);
1309 }
1310
1311 long wxListCtrl::InsertItem(long index, const wxString& label)
1312 {
1313 wxListItem info;
1314 info.m_text = label;
1315 info.m_mask = wxLIST_MASK_TEXT;
1316 info.m_itemId = index;
1317 return InsertItem(info);
1318 }
1319
1320 // Inserts an image item
1321 long wxListCtrl::InsertItem(long index, int imageIndex)
1322 {
1323 wxListItem info;
1324 info.m_image = imageIndex;
1325 info.m_mask = wxLIST_MASK_IMAGE;
1326 info.m_itemId = index;
1327 return InsertItem(info);
1328 }
1329
1330 // Inserts an image/string item
1331 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1332 {
1333 wxListItem info;
1334 info.m_image = imageIndex;
1335 info.m_text = label;
1336 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1337 info.m_itemId = index;
1338 return InsertItem(info);
1339 }
1340
1341 // For list view mode (only), inserts a column.
1342 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1343 {
1344 LV_COLUMN lvCol;
1345 wxConvertToMSWListCol(col, item, lvCol);
1346
1347 if ( !(lvCol.mask & LVCF_WIDTH) )
1348 {
1349 // always give some width to the new column: this one is compatible
1350 // with the generic version
1351 lvCol.mask |= LVCF_WIDTH;
1352 lvCol.cx = 80;
1353 }
1354
1355 // when we insert a column which can contain an image, we must specify this
1356 // flag right now as doing it later in SetColumn() has no effect
1357 //
1358 // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
1359 // way to dynamically set/clear the bitmap as the column without a bitmap
1360 // on the left looks ugly (there is a hole)
1361 //
1362 // unfortunately with my version of comctl32.dll (5.80), the left column
1363 // image is always on the left and it seems that it's a "feature" - I
1364 // didn't find any way to work around it in any case
1365 if ( lvCol.mask & LVCF_IMAGE )
1366 {
1367 lvCol.mask |= LVCF_FMT;
1368 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT;
1369 }
1370
1371 bool success = ListView_InsertColumn(GetHwnd(), col, &lvCol) != -1;
1372 if ( success )
1373 {
1374 m_colCount++;
1375 }
1376 else
1377 {
1378 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1379 lvCol.pszText);
1380 }
1381
1382 return success;
1383 }
1384
1385 long wxListCtrl::InsertColumn(long col,
1386 const wxString& heading,
1387 int format,
1388 int width)
1389 {
1390 wxListItem item;
1391 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1392 item.m_text = heading;
1393 if ( width > -1 )
1394 {
1395 item.m_mask |= wxLIST_MASK_WIDTH;
1396 item.m_width = width;
1397 }
1398 item.m_format = format;
1399
1400 return InsertColumn(col, item);
1401 }
1402
1403 // Scrolls the list control. If in icon, small icon or report view mode,
1404 // x specifies the number of pixels to scroll. If in list view mode, x
1405 // specifies the number of columns to scroll.
1406 // If in icon, small icon or list view mode, y specifies the number of pixels
1407 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1408 bool wxListCtrl::ScrollList(int dx, int dy)
1409 {
1410 return (ListView_Scroll(GetHwnd(), dx, dy) != 0);
1411 }
1412
1413 // Sort items.
1414
1415 // fn is a function which takes 3 long arguments: item1, item2, data.
1416 // item1 is the long data associated with a first item (NOT the index).
1417 // item2 is the long data associated with a second item (NOT the index).
1418 // data is the same value as passed to SortItems.
1419 // The return value is a negative number if the first item should precede the second
1420 // item, a positive number of the second item should precede the first,
1421 // or zero if the two items are equivalent.
1422
1423 // data is arbitrary data to be passed to the sort function.
1424
1425 // FIXME: this is horrible and MT-unsafe and everything else but I don't have
1426 // time for anything better right now (VZ)
1427 static long gs_sortData = 0;
1428 static wxListCtrl *gs_sortCtrl = NULL;
1429 static wxListCtrlCompare gs_sortFunction = NULL;
1430
1431 int wxCMPFUNC_CONV wxListCtrlCompareFn(const void *arg1, const void *arg2)
1432 {
1433 int n1 = *(const int *)arg1,
1434 n2 = *(const int *)arg2;
1435
1436 return gs_sortFunction(gs_sortCtrl->GetItemData(n1),
1437 gs_sortCtrl->GetItemData(n2),
1438 gs_sortData);
1439 }
1440
1441 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1442 {
1443 // sort the attributes too
1444 if ( m_hasAnyAttr )
1445 {
1446 int n,
1447 count = GetItemCount();
1448 int *aItems = new int[count];
1449 for ( n = 0; n < count; n++ )
1450 {
1451 aItems[n] = n;
1452 }
1453
1454 gs_sortData = data;
1455 gs_sortCtrl = this;
1456 gs_sortFunction = fn;
1457
1458 qsort(aItems, count, sizeof(int), wxListCtrlCompareFn);
1459
1460 gs_sortData = 0;
1461 gs_sortCtrl = NULL;
1462 gs_sortFunction = NULL;
1463
1464 wxHashTable attrsNew(wxKEY_INTEGER, 1000);
1465 for ( n = 0; n < count; n++ )
1466 {
1467 wxObject *attr = m_attrs.Delete(aItems[n]);
1468 if ( attr )
1469 {
1470 attrsNew.Put(n, attr);
1471 }
1472 }
1473
1474 m_attrs.Destroy();
1475 m_attrs = attrsNew;
1476
1477 delete [] aItems;
1478 }
1479
1480 if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE)fn, data) )
1481 {
1482 wxLogDebug(_T("ListView_SortItems() failed"));
1483
1484 return FALSE;
1485 }
1486
1487 return TRUE;
1488 }
1489
1490
1491
1492 // ----------------------------------------------------------------------------
1493 // message processing
1494 // ----------------------------------------------------------------------------
1495
1496 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1497 {
1498 if (cmd == EN_UPDATE)
1499 {
1500 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1501 event.SetEventObject( this );
1502 ProcessCommand(event);
1503 return TRUE;
1504 }
1505 else if (cmd == EN_KILLFOCUS)
1506 {
1507 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1508 event.SetEventObject( this );
1509 ProcessCommand(event);
1510 return TRUE;
1511 }
1512 else
1513 return FALSE;
1514 }
1515
1516 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1517 {
1518 // prepare the event
1519 // -----------------
1520
1521 wxListEvent event(wxEVT_NULL, m_windowId);
1522 event.SetEventObject(this);
1523
1524 wxEventType eventType = wxEVT_NULL;
1525
1526 NMHDR *nmhdr = (NMHDR *)lParam;
1527
1528 // if your compiler is as broken as this, you should really change it: this
1529 // code is needed for normal operation! #ifdef below is only useful for
1530 // automatic rebuilds which are done with a very old compiler version
1531 #ifdef HDN_BEGINTRACKA
1532
1533 // check for messages from the header (in report view)
1534 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1535
1536 // is it a message from the header?
1537 if ( nmhdr->hwndFrom == hwndHdr )
1538 {
1539 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1540
1541 event.m_itemIndex = -1;
1542
1543 switch ( nmhdr->code )
1544 {
1545 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1546 // TRACK messages even to ANSI programs: on my system I get
1547 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1548 //
1549 // work around is to simply catch both versions and hope that it
1550 // works (why should this message exist in ANSI and Unicode is
1551 // beyond me as it doesn't deal with strings at all...)
1552 case HDN_BEGINTRACKA:
1553 case HDN_BEGINTRACKW:
1554 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1555 // fall through
1556
1557 case HDN_TRACKA:
1558 case HDN_TRACKW:
1559 if ( eventType == wxEVT_NULL )
1560 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1561 // fall through
1562
1563 case HDN_ENDTRACKA:
1564 case HDN_ENDTRACKW:
1565 if ( eventType == wxEVT_NULL )
1566 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1567 event.m_col = nmHDR->iItem;
1568 break;
1569
1570 case NM_RCLICK:
1571 {
1572 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1573 event.m_col = -1;
1574
1575 // find the column clicked: we have to search for it
1576 // ourselves as the notification message doesn't provide
1577 // this info
1578
1579 // where did the click occur?
1580 POINT ptClick;
1581 if ( !::GetCursorPos(&ptClick) )
1582 {
1583 wxLogLastError(_T("GetCursorPos"));
1584 }
1585
1586 if ( !::ScreenToClient(hwndHdr, &ptClick) )
1587 {
1588 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1589 }
1590
1591 event.m_pointDrag.x = ptClick.x;
1592 event.m_pointDrag.y = ptClick.y;
1593
1594 int colCount = Header_GetItemCount(hwndHdr);
1595
1596 RECT rect;
1597 for ( int col = 0; col < colCount; col++ )
1598 {
1599 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1600 {
1601 if ( ::PtInRect(&rect, ptClick) )
1602 {
1603 event.m_col = col;
1604 break;
1605 }
1606 }
1607 }
1608 }
1609 break;
1610
1611 default:
1612 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1613 }
1614 }
1615 else
1616 #endif // defined(HDN_BEGINTRACKA)
1617 if ( nmhdr->hwndFrom == GetHwnd() )
1618 {
1619 // almost all messages use NM_LISTVIEW
1620 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1621
1622 // this is true for almost all events
1623 event.m_item.m_data = nmLV->lParam;
1624
1625 switch ( nmhdr->code )
1626 {
1627 case LVN_BEGINRDRAG:
1628 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1629 // fall through
1630
1631 case LVN_BEGINDRAG:
1632 if ( eventType == wxEVT_NULL )
1633 {
1634 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1635 }
1636
1637 event.m_itemIndex = nmLV->iItem;
1638 event.m_pointDrag.x = nmLV->ptAction.x;
1639 event.m_pointDrag.y = nmLV->ptAction.y;
1640 break;
1641
1642 // NB: we have to handle both *A and *W versions here because some
1643 // versions of comctl32.dll send ANSI message to an Unicode app
1644 case LVN_BEGINLABELEDITA:
1645 {
1646 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1647 wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item);
1648 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1649 event.m_itemIndex = event.m_item.m_itemId;
1650 }
1651 break;
1652 case LVN_BEGINLABELEDITW:
1653 {
1654 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1655 wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item);
1656 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1657 event.m_itemIndex = event.m_item.m_itemId;
1658 }
1659 break;
1660
1661 case LVN_ENDLABELEDITA:
1662 {
1663 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1664 wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item);
1665 wxConvertFromMSWListItem(NULL, event.m_item, item);
1666 if ( ((LV_ITEM)item).pszText == NULL ||
1667 ((LV_ITEM)item).iItem == -1 )
1668 return FALSE;
1669
1670 event.m_itemIndex = event.m_item.m_itemId;
1671 }
1672 break;
1673 case LVN_ENDLABELEDITW:
1674 {
1675 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1676 wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item);
1677 wxConvertFromMSWListItem(NULL, event.m_item, item);
1678 if ( ((LV_ITEM)item).pszText == NULL ||
1679 ((LV_ITEM)item).iItem == -1 )
1680 return FALSE;
1681
1682 event.m_itemIndex = event.m_item.m_itemId;
1683 }
1684 break;
1685
1686 case LVN_COLUMNCLICK:
1687 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1688 event.m_itemIndex = -1;
1689 event.m_col = nmLV->iSubItem;
1690 break;
1691
1692 case LVN_DELETEALLITEMS:
1693 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1694 event.m_itemIndex = -1;
1695
1696 FreeAllAttrs();
1697
1698 break;
1699
1700 case LVN_DELETEITEM:
1701 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1702 event.m_itemIndex = nmLV->iItem;
1703
1704 if ( m_hasAnyAttr )
1705 {
1706 delete (wxListItemAttr *)m_attrs.Delete(nmLV->iItem);
1707 }
1708 break;
1709
1710 case LVN_SETDISPINFO:
1711 {
1712 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1713 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1714 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
1715 }
1716 break;
1717
1718 case LVN_INSERTITEM:
1719 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1720 event.m_itemIndex = nmLV->iItem;
1721 break;
1722
1723 case LVN_ITEMCHANGED:
1724 // we translate this catch all message into more interesting
1725 // (and more easy to process) wxWindows events
1726
1727 // first of all, we deal with the state change events only
1728 if ( nmLV->uChanged & LVIF_STATE )
1729 {
1730 // temp vars for readability
1731 const UINT stOld = nmLV->uOldState;
1732 const UINT stNew = nmLV->uNewState;
1733
1734 // has the focus changed?
1735 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
1736 {
1737 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
1738 event.m_itemIndex = nmLV->iItem;
1739 }
1740
1741 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
1742 {
1743 if ( eventType != wxEVT_NULL )
1744 {
1745 // focus and selection have both changed: send the
1746 // focus event from here and the selection one
1747 // below
1748 event.SetEventType(eventType);
1749 (void)GetEventHandler()->ProcessEvent(event);
1750 }
1751 else // no focus event to send
1752 {
1753 // then need to set m_itemIndex as it wasn't done
1754 // above
1755 event.m_itemIndex = nmLV->iItem;
1756 }
1757
1758 eventType = stNew & LVIS_SELECTED
1759 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1760 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1761 }
1762 }
1763
1764 if ( eventType == wxEVT_NULL )
1765 {
1766 // not an interesting event for us
1767 return FALSE;
1768 }
1769
1770 break;
1771
1772 case LVN_KEYDOWN:
1773 {
1774 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1775 WORD wVKey = info->wVKey;
1776
1777 // get the current selection
1778 long lItem = GetNextItem(-1,
1779 wxLIST_NEXT_ALL,
1780 wxLIST_STATE_SELECTED);
1781
1782 // <Enter> or <Space> activate the selected item if any (but
1783 // not with Shift and/or Ctrl as then they have a predefined
1784 // meaning for the list view)
1785 if ( lItem != -1 &&
1786 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
1787 !(wxIsShiftDown() || wxIsCtrlDown()) )
1788 {
1789 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1790 }
1791 else
1792 {
1793 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
1794
1795 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
1796 // value which should be used as is
1797 int code = wxCharCodeMSWToWX(wVKey);
1798 event.m_code = code ? code : wVKey;
1799 }
1800
1801 event.m_itemIndex =
1802 event.m_item.m_itemId = lItem;
1803
1804 if ( lItem != -1 )
1805 {
1806 // fill the other fields too
1807 event.m_item.m_text = GetItemText(lItem);
1808 event.m_item.m_data = GetItemData(lItem);
1809 }
1810 }
1811 break;
1812
1813 case NM_DBLCLK:
1814 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1815 // anything else
1816 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1817 {
1818 return TRUE;
1819 }
1820
1821 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1822 // if it happened on an item (and not on empty place)
1823 if ( nmLV->iItem == -1 )
1824 {
1825 // not on item
1826 return FALSE;
1827 }
1828
1829 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1830 event.m_itemIndex = nmLV->iItem;
1831 event.m_item.m_text = GetItemText(nmLV->iItem);
1832 event.m_item.m_data = GetItemData(nmLV->iItem);
1833 break;
1834
1835 case NM_RCLICK:
1836 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1837 // don't do anything else
1838 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1839 {
1840 return TRUE;
1841 }
1842
1843 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1844 LV_HITTESTINFO lvhti;
1845 wxZeroMemory(lvhti);
1846
1847 ::GetCursorPos(&(lvhti.pt));
1848 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
1849 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
1850 {
1851 if ( lvhti.flags & LVHT_ONITEM )
1852 {
1853 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
1854 event.m_itemIndex = lvhti.iItem;
1855 event.m_pointDrag.x = lvhti.pt.x;
1856 event.m_pointDrag.y = lvhti.pt.y;
1857 }
1858 }
1859 break;
1860
1861 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1862 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
1863 case NM_CUSTOMDRAW:
1864 *result = OnCustomDraw(lParam);
1865
1866 return TRUE;
1867 #endif // _WIN32_IE >= 0x300
1868
1869 case LVN_ODCACHEHINT:
1870 {
1871 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
1872
1873 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
1874
1875 // we get some really stupid cache hints like ones for items in
1876 // range 0..0 for an empty control or, after deleting an item,
1877 // for items in invalid range - filter this garbage out
1878 if ( cacheHint->iFrom < cacheHint->iTo )
1879 {
1880 event.m_oldItemIndex = cacheHint->iFrom;
1881
1882 long iMax = GetItemCount();
1883 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
1884 : iMax - 1;
1885 }
1886 else
1887 {
1888 return FALSE;
1889 }
1890 }
1891 break;
1892
1893 case LVN_GETDISPINFO:
1894 if ( IsVirtual() )
1895 {
1896 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1897
1898 LV_ITEM& lvi = info->item;
1899 long item = lvi.iItem;
1900
1901 if ( lvi.mask & LVIF_TEXT )
1902 {
1903 wxString text = OnGetItemText(item, lvi.iSubItem);
1904 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
1905 }
1906
1907 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
1908 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
1909 if ( lvi.mask & LVIF_IMAGE )
1910 {
1911 lvi.iImage = OnGetItemImage(item);
1912 }
1913 #endif
1914
1915 // a little dose of healthy paranoia: as we never use
1916 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1917 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
1918 _T("we don't support state callbacks yet!") );
1919
1920 return TRUE;
1921 }
1922 // fall through
1923
1924 default:
1925 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1926 }
1927 }
1928 else
1929 {
1930 // where did this one come from?
1931 return FALSE;
1932 }
1933
1934 // process the event
1935 // -----------------
1936
1937 event.SetEventType(eventType);
1938
1939 if ( !GetEventHandler()->ProcessEvent(event) )
1940 return FALSE;
1941
1942 // post processing
1943 // ---------------
1944
1945 switch ( nmhdr->code )
1946 {
1947 case LVN_DELETEALLITEMS:
1948 // always return TRUE to suppress all additional LVN_DELETEITEM
1949 // notifications - this makes deleting all items from a list ctrl
1950 // much faster
1951 *result = TRUE;
1952
1953 return TRUE;
1954
1955 case LVN_ENDLABELEDITA:
1956 case LVN_ENDLABELEDITW:
1957 // logic here is inversed compared to all the other messages
1958 *result = event.IsAllowed();
1959
1960 return TRUE;
1961 }
1962
1963 *result = !event.IsAllowed();
1964
1965 return TRUE;
1966 }
1967
1968 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1969
1970 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
1971 {
1972 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
1973 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
1974 switch ( nmcd.dwDrawStage )
1975 {
1976 case CDDS_PREPAINT:
1977 // if we've got any items with non standard attributes,
1978 // notify us before painting each item
1979 //
1980 // for virtual controls, always suppose that we have attributes as
1981 // there is no way to check for this
1982 return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
1983 : CDRF_DODEFAULT;
1984
1985 case CDDS_ITEMPREPAINT:
1986 {
1987 size_t item = (size_t)nmcd.dwItemSpec;
1988 if ( item >= (size_t)GetItemCount() )
1989 {
1990 // we get this message with item == 0 for an empty control,
1991 // we must ignore it as calling OnGetItemAttr() would be
1992 // wrong
1993 return CDRF_DODEFAULT;
1994 }
1995
1996 wxListItemAttr *attr =
1997 IsVirtual() ? OnGetItemAttr(item)
1998 : (wxListItemAttr *)m_attrs.Get(item);
1999
2000 if ( !attr )
2001 {
2002 // nothing to do for this item
2003 return CDRF_DODEFAULT;
2004 }
2005
2006 HFONT hFont;
2007 wxColour colText, colBack;
2008 if ( attr->HasFont() )
2009 {
2010 wxFont font = attr->GetFont();
2011 hFont = (HFONT)font.GetResourceHandle();
2012 }
2013 else
2014 {
2015 hFont = 0;
2016 }
2017
2018 if ( attr->HasTextColour() )
2019 {
2020 colText = attr->GetTextColour();
2021 }
2022 else
2023 {
2024 colText = GetTextColour();
2025 }
2026
2027 if ( attr->HasBackgroundColour() )
2028 {
2029 colBack = attr->GetBackgroundColour();
2030 }
2031 else
2032 {
2033 colBack = GetBackgroundColour();
2034 }
2035
2036 lplvcd->clrText = wxColourToRGB(colText);
2037 lplvcd->clrTextBk = wxColourToRGB(colBack);
2038
2039 // note that if we wanted to set colours for
2040 // individual columns (subitems), we would have
2041 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2042 if ( hFont )
2043 {
2044 ::SelectObject(nmcd.hdc, hFont);
2045
2046 return CDRF_NEWFONT;
2047 }
2048 }
2049 // fall through to return CDRF_DODEFAULT
2050
2051 default:
2052 return CDRF_DODEFAULT;
2053 }
2054 }
2055
2056 #endif // NM_CUSTOMDRAW supported
2057
2058 // Necessary for drawing hrules and vrules, if specified
2059 void wxListCtrl::OnPaint(wxPaintEvent& event)
2060 {
2061 wxPaintDC dc(this);
2062
2063 wxControl::OnPaint(event);
2064
2065 // Reset the device origin since it may have been set
2066 dc.SetDeviceOrigin(0, 0);
2067
2068 bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0);
2069 bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0);
2070
2071 if (!drawHRules && !drawVRules)
2072 return;
2073 if ((GetWindowStyle() & wxLC_REPORT) == 0)
2074 return;
2075
2076 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2077 dc.SetPen(pen);
2078 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2079
2080 wxSize clientSize = GetClientSize();
2081 wxRect itemRect;
2082 int cy=0;
2083
2084 int itemCount = GetItemCount();
2085 int i;
2086 if (drawHRules)
2087 {
2088 long top = GetTopItem();
2089 for (i = top; i < top + GetCountPerPage() + 1; i++)
2090 {
2091 if (GetItemRect(i, itemRect))
2092 {
2093 cy = itemRect.GetTop();
2094 if (i != 0) // Don't draw the first one
2095 {
2096 dc.DrawLine(0, cy, clientSize.x, cy);
2097 }
2098 // Draw last line
2099 if (i == itemCount - 1)
2100 {
2101 cy = itemRect.GetBottom();
2102 dc.DrawLine(0, cy, clientSize.x, cy);
2103 }
2104 }
2105 }
2106 }
2107 i = itemCount - 1;
2108 if (drawVRules && (i > -1))
2109 {
2110 wxRect firstItemRect;
2111 GetItemRect(0, firstItemRect);
2112
2113 if (GetItemRect(i, itemRect))
2114 {
2115 int col;
2116 int x = itemRect.GetX();
2117 for (col = 0; col < GetColumnCount(); col++)
2118 {
2119 int colWidth = GetColumnWidth(col);
2120 x += colWidth ;
2121 dc.DrawLine(x, firstItemRect.GetY() - 2, x, itemRect.GetBottom());
2122 }
2123 }
2124 }
2125 }
2126
2127 // ----------------------------------------------------------------------------
2128 // virtual list controls
2129 // ----------------------------------------------------------------------------
2130
2131 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2132 {
2133 // this is a pure virtual function, in fact - which is not really pure
2134 // because the controls which are not virtual don't need to implement it
2135 wxFAIL_MSG( _T("not supposed to be called") );
2136
2137 return wxEmptyString;
2138 }
2139
2140 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2141 {
2142 // same as above
2143 wxFAIL_MSG( _T("not supposed to be called") );
2144
2145 return -1;
2146 }
2147
2148 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2149 {
2150 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2151 _T("invalid item index in OnGetItemAttr()") );
2152
2153 // no attributes by default
2154 return NULL;
2155 }
2156
2157 void wxListCtrl::SetItemCount(long count)
2158 {
2159 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2160
2161 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, 0) )
2162 {
2163 wxLogLastError(_T("ListView_SetItemCount"));
2164 }
2165 }
2166
2167 void wxListCtrl::RefreshItem(long item)
2168 {
2169 // strangely enough, ListView_Update() results in much more flicker here
2170 // than a dumb Refresh() -- why?
2171 #if 0
2172 if ( !ListView_Update(GetHwnd(), item) )
2173 {
2174 wxLogLastError(_T("ListView_Update"));
2175 }
2176 #else // 1
2177 wxRect rect;
2178 GetItemRect(item, rect);
2179 RefreshRect(rect);
2180 #endif // 0/1
2181 }
2182
2183 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2184 {
2185 wxRect rect1, rect2;
2186 GetItemRect(itemFrom, rect1);
2187 GetItemRect(itemTo, rect2);
2188
2189 wxRect rect = rect1;
2190 rect.height = rect2.GetBottom() - rect1.GetTop();
2191
2192 RefreshRect(rect);
2193 }
2194
2195 // ----------------------------------------------------------------------------
2196 // wxListItem
2197 // ----------------------------------------------------------------------------
2198
2199 // List item structure
2200 wxListItem::wxListItem()
2201 {
2202 m_mask = 0;
2203 m_itemId = 0;
2204 m_col = 0;
2205 m_state = 0;
2206 m_stateMask = 0;
2207 m_image = 0;
2208 m_data = 0;
2209
2210 m_format = wxLIST_FORMAT_CENTRE;
2211 m_width = 0;
2212
2213 m_attr = NULL;
2214 }
2215
2216 void wxListItem::Clear()
2217 {
2218 m_mask = 0;
2219 m_itemId = 0;
2220 m_col = 0;
2221 m_state = 0;
2222 m_stateMask = 0;
2223 m_image = 0;
2224 m_data = 0;
2225 m_format = wxLIST_FORMAT_CENTRE;
2226 m_width = 0;
2227 m_text = wxEmptyString;
2228
2229 if (m_attr) delete m_attr;
2230 m_attr = NULL;
2231 }
2232
2233 void wxListItem::ClearAttributes()
2234 {
2235 if (m_attr) delete m_attr;
2236 m_attr = NULL;
2237 }
2238
2239 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2240 wxListItem& info,
2241 LV_ITEM& lvItem)
2242 {
2243 info.m_data = lvItem.lParam;
2244 info.m_mask = 0;
2245 info.m_state = 0;
2246 info.m_stateMask = 0;
2247 info.m_itemId = lvItem.iItem;
2248
2249 long oldMask = lvItem.mask;
2250
2251 bool needText = FALSE;
2252 if (hwndListCtrl != 0)
2253 {
2254 if ( lvItem.mask & LVIF_TEXT )
2255 needText = FALSE;
2256 else
2257 needText = TRUE;
2258
2259 if ( needText )
2260 {
2261 lvItem.pszText = new wxChar[513];
2262 lvItem.cchTextMax = 512;
2263 }
2264 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2265 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2266 }
2267
2268 if ( lvItem.mask & LVIF_STATE )
2269 {
2270 info.m_mask |= wxLIST_MASK_STATE;
2271
2272 if ( lvItem.stateMask & LVIS_CUT)
2273 {
2274 info.m_stateMask |= wxLIST_STATE_CUT;
2275 if ( lvItem.state & LVIS_CUT )
2276 info.m_state |= wxLIST_STATE_CUT;
2277 }
2278 if ( lvItem.stateMask & LVIS_DROPHILITED)
2279 {
2280 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2281 if ( lvItem.state & LVIS_DROPHILITED )
2282 info.m_state |= wxLIST_STATE_DROPHILITED;
2283 }
2284 if ( lvItem.stateMask & LVIS_FOCUSED)
2285 {
2286 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2287 if ( lvItem.state & LVIS_FOCUSED )
2288 info.m_state |= wxLIST_STATE_FOCUSED;
2289 }
2290 if ( lvItem.stateMask & LVIS_SELECTED)
2291 {
2292 info.m_stateMask |= wxLIST_STATE_SELECTED;
2293 if ( lvItem.state & LVIS_SELECTED )
2294 info.m_state |= wxLIST_STATE_SELECTED;
2295 }
2296 }
2297
2298 if ( lvItem.mask & LVIF_TEXT )
2299 {
2300 info.m_mask |= wxLIST_MASK_TEXT;
2301 info.m_text = lvItem.pszText;
2302 }
2303 if ( lvItem.mask & LVIF_IMAGE )
2304 {
2305 info.m_mask |= wxLIST_MASK_IMAGE;
2306 info.m_image = lvItem.iImage;
2307 }
2308 if ( lvItem.mask & LVIF_PARAM )
2309 info.m_mask |= wxLIST_MASK_DATA;
2310 if ( lvItem.mask & LVIF_DI_SETITEM )
2311 info.m_mask |= wxLIST_SET_ITEM;
2312 info.m_col = lvItem.iSubItem;
2313
2314 if (needText)
2315 {
2316 if (lvItem.pszText)
2317 delete[] lvItem.pszText;
2318 }
2319 lvItem.mask = oldMask;
2320 }
2321
2322 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2323 {
2324 if (stateMask & wxLIST_STATE_CUT)
2325 {
2326 lvItem.stateMask |= LVIS_CUT;
2327 if (state & wxLIST_STATE_CUT)
2328 lvItem.state |= LVIS_CUT;
2329 }
2330 if (stateMask & wxLIST_STATE_DROPHILITED)
2331 {
2332 lvItem.stateMask |= LVIS_DROPHILITED;
2333 if (state & wxLIST_STATE_DROPHILITED)
2334 lvItem.state |= LVIS_DROPHILITED;
2335 }
2336 if (stateMask & wxLIST_STATE_FOCUSED)
2337 {
2338 lvItem.stateMask |= LVIS_FOCUSED;
2339 if (state & wxLIST_STATE_FOCUSED)
2340 lvItem.state |= LVIS_FOCUSED;
2341 }
2342 if (stateMask & wxLIST_STATE_SELECTED)
2343 {
2344 lvItem.stateMask |= LVIS_SELECTED;
2345 if (state & wxLIST_STATE_SELECTED)
2346 lvItem.state |= LVIS_SELECTED;
2347 }
2348 }
2349
2350 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2351 const wxListItem& info,
2352 LV_ITEM& lvItem)
2353 {
2354 lvItem.iItem = (int) info.m_itemId;
2355
2356 lvItem.iImage = info.m_image;
2357 lvItem.lParam = info.m_data;
2358 lvItem.stateMask = 0;
2359 lvItem.state = 0;
2360 lvItem.mask = 0;
2361 lvItem.iSubItem = info.m_col;
2362
2363 if (info.m_mask & wxLIST_MASK_STATE)
2364 {
2365 lvItem.mask |= LVIF_STATE;
2366
2367 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
2368 }
2369
2370 if (info.m_mask & wxLIST_MASK_TEXT)
2371 {
2372 lvItem.mask |= LVIF_TEXT;
2373 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
2374 {
2375 lvItem.pszText = LPSTR_TEXTCALLBACK;
2376 }
2377 else
2378 {
2379 // pszText is not const, hence the cast
2380 lvItem.pszText = (wxChar *)info.m_text.c_str();
2381 if ( lvItem.pszText )
2382 lvItem.cchTextMax = info.m_text.Length();
2383 else
2384 lvItem.cchTextMax = 0;
2385 }
2386 }
2387 if (info.m_mask & wxLIST_MASK_IMAGE)
2388 lvItem.mask |= LVIF_IMAGE;
2389 if (info.m_mask & wxLIST_MASK_DATA)
2390 lvItem.mask |= LVIF_PARAM;
2391 }
2392
2393 static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
2394 LV_COLUMN& lvCol)
2395 {
2396 wxZeroMemory(lvCol);
2397
2398 if ( item.m_mask & wxLIST_MASK_TEXT )
2399 {
2400 lvCol.mask |= LVCF_TEXT;
2401 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
2402 }
2403
2404 if ( item.m_mask & wxLIST_MASK_FORMAT )
2405 {
2406 lvCol.mask |= LVCF_FMT;
2407
2408 if ( item.m_format == wxLIST_FORMAT_LEFT )
2409 lvCol.fmt = LVCFMT_LEFT;
2410 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
2411 lvCol.fmt = LVCFMT_RIGHT;
2412 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
2413 lvCol.fmt = LVCFMT_CENTER;
2414 }
2415
2416 if ( item.m_mask & wxLIST_MASK_WIDTH )
2417 {
2418 lvCol.mask |= LVCF_WIDTH;
2419 if ( item.m_width == wxLIST_AUTOSIZE)
2420 lvCol.cx = LVSCW_AUTOSIZE;
2421 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2422 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
2423 else
2424 lvCol.cx = item.m_width;
2425 }
2426
2427 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
2428 && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) )
2429 if ( item.m_mask & wxLIST_MASK_IMAGE )
2430 {
2431 if ( wxTheApp->GetComCtl32Version() >= 470 )
2432 {
2433 lvCol.mask |= LVCF_IMAGE;
2434 lvCol.iImage = item.m_image;
2435 }
2436 //else: it doesn't support item images anyhow
2437 }
2438 #endif
2439 }
2440
2441 #endif // wxUSE_LISTCTRL
2442
2443 // vi:sts=4:sw=4:et