]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
workarounded false memory leak report
[wxWidgets.git] / src / msw / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __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 #ifdef __GNUWIN32_OLD__
49 #include "wx/msw/gnuwin32/extra.h"
50 #else
51 #include <commctrl.h>
52 #endif
53
54 #ifndef LVHT_ONITEM
55 #define LVHT_ONITEM \
56 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
57 #endif
58
59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
60 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
61 #endif
62
63 #ifndef LVS_EX_FULLROWSELECT
64 #define LVS_EX_FULLROWSELECT 0x00000020
65 #endif
66
67 // ----------------------------------------------------------------------------
68 // private functions
69 // ----------------------------------------------------------------------------
70
71 static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem);
72 static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem, HWND getFullInfo = 0);
73
74 // ----------------------------------------------------------------------------
75 // macros
76 // ----------------------------------------------------------------------------
77
78 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
79 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
80
81 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
82 EVT_PAINT(wxListCtrl::OnPaint)
83 END_EVENT_TABLE()
84
85 // ============================================================================
86 // implementation
87 // ============================================================================
88
89 // ----------------------------------------------------------------------------
90 // wxListEvent
91 // ----------------------------------------------------------------------------
92
93 void wxListEvent::CopyObject(wxObject& object_dest) const
94 {
95 wxListEvent *obj = (wxListEvent *)&object_dest;
96
97 wxNotifyEvent::CopyObject(object_dest);
98
99 obj->m_code = m_code;
100 obj->m_itemIndex = m_itemIndex;
101 obj->m_oldItemIndex = m_oldItemIndex;
102 obj->m_col = m_col;
103 obj->m_cancelled = m_cancelled;
104 obj->m_pointDrag = m_pointDrag;
105 obj->m_item.m_mask = m_item.m_mask;
106 obj->m_item.m_itemId = m_item.m_itemId;
107 obj->m_item.m_col = m_item.m_col;
108 obj->m_item.m_state = m_item.m_state;
109 obj->m_item.m_stateMask = m_item.m_stateMask;
110 obj->m_item.m_text = m_item.m_text;
111 obj->m_item.m_image = m_item.m_image;
112 obj->m_item.m_data = m_item.m_data;
113 obj->m_item.m_format = m_item.m_format;
114 obj->m_item.m_width = m_item.m_width;
115
116 if ( m_item.HasAttributes() )
117 {
118 obj->m_item.SetTextColour(m_item.GetTextColour());
119 obj->m_item.SetBackgroundColour(m_item.GetBackgroundColour());
120 obj->m_item.SetFont(m_item.GetFont());
121 }
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxListCtrl construction
126 // ----------------------------------------------------------------------------
127
128 void wxListCtrl::Init()
129 {
130 m_imageListNormal = NULL;
131 m_imageListSmall = NULL;
132 m_imageListState = NULL;
133 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE;
134 m_baseStyle = 0;
135 m_colCount = 0;
136 m_textCtrl = NULL;
137 m_hasAnyAttr = FALSE;
138 }
139
140 bool wxListCtrl::Create(wxWindow *parent,
141 wxWindowID id,
142 const wxPoint& pos,
143 const wxSize& size,
144 long style,
145 const wxValidator& validator,
146 const wxString& name)
147 {
148 #if wxUSE_VALIDATORS
149 SetValidator(validator);
150 #endif // wxUSE_VALIDATORS
151
152 SetName(name);
153
154 int x = pos.x;
155 int y = pos.y;
156 int width = size.x;
157 int height = size.y;
158
159 m_windowStyle = style;
160
161 SetParent(parent);
162
163 if (width <= 0)
164 width = 100;
165 if (height <= 0)
166 height = 30;
167 if (x < 0)
168 x = 0;
169 if (y < 0)
170 y = 0;
171
172 m_windowId = (id == -1) ? NewControlId() : id;
173
174 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
175 LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
176 if ( wxStyleHasBorder(m_windowStyle) )
177 wstyle |= WS_BORDER;
178 m_baseStyle = wstyle;
179
180 if ( !DoCreateControl(x, y, width, height) )
181 return FALSE;
182
183 if (parent)
184 parent->AddChild(this);
185
186 return TRUE;
187 }
188
189 bool wxListCtrl::DoCreateControl(int x, int y, int w, int h)
190 {
191 DWORD wstyle = m_baseStyle;
192
193 bool want3D;
194 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
195
196 // Even with extended styles, need to combine with WS_BORDER
197 // for them to look right.
198 if ( want3D )
199 wstyle |= WS_BORDER;
200
201 long oldStyle = 0; // Dummy
202 wstyle |= ConvertToMSWStyle(oldStyle, m_windowStyle);
203
204 // Create the ListView control.
205 m_hWnd = (WXHWND)CreateWindowEx(exStyle,
206 WC_LISTVIEW,
207 wxT(""),
208 wstyle,
209 x, y, w, h,
210 GetWinHwnd(GetParent()),
211 (HMENU)m_windowId,
212 wxGetInstance(),
213 NULL);
214
215 if ( !m_hWnd )
216 {
217 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
218
219 return FALSE;
220 }
221
222 // for comctl32.dll v 4.70+ we want to have this attribute because it's
223 // prettier (and also because wxGTK does it like this)
224 if ( (wstyle & LVS_REPORT) && wxTheApp->GetComCtl32Version() >= 470 )
225 {
226 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
227 0, LVS_EX_FULLROWSELECT);
228 }
229
230 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
231 SetForegroundColour(GetParent()->GetForegroundColour());
232
233 SubclassWin(m_hWnd);
234
235 return TRUE;
236 }
237
238 void wxListCtrl::UpdateStyle()
239 {
240 if ( GetHWND() )
241 {
242 // The new window view style
243 long dummy;
244 DWORD dwStyleNew = ConvertToMSWStyle(dummy, m_windowStyle);
245 dwStyleNew |= m_baseStyle;
246
247 // Get the current window style.
248 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
249
250 // Only set the window style if the view bits have changed.
251 if ( dwStyleOld != dwStyleNew )
252 {
253 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
254 }
255 }
256 }
257
258 void wxListCtrl::FreeAllAttrs(bool dontRecreate)
259 {
260 if ( m_hasAnyAttr )
261 {
262 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
263 {
264 delete (wxListItemAttr *)node->Data();
265 }
266
267 m_attrs.Destroy();
268 if ( !dontRecreate )
269 {
270 m_attrs.Create(wxKEY_INTEGER, 1000); // just as def ctor
271 }
272
273 m_hasAnyAttr = FALSE;
274 }
275 }
276
277 wxListCtrl::~wxListCtrl()
278 {
279 FreeAllAttrs(TRUE /* no need to recreate hash any more */);
280
281 if ( m_textCtrl )
282 {
283 m_textCtrl->UnsubclassWin();
284 m_textCtrl->SetHWND(0);
285 delete m_textCtrl;
286 m_textCtrl = NULL;
287 }
288
289 if (m_ownsImageListNormal) delete m_imageListNormal;
290 if (m_ownsImageListSmall) delete m_imageListSmall;
291 if (m_ownsImageListState) delete m_imageListState;
292 }
293
294 // ----------------------------------------------------------------------------
295 // set/get/change style
296 // ----------------------------------------------------------------------------
297
298 // Add or remove a single window style
299 void wxListCtrl::SetSingleStyle(long style, bool add)
300 {
301 long flag = GetWindowStyleFlag();
302
303 // Get rid of conflicting styles
304 if ( add )
305 {
306 if ( style & wxLC_MASK_TYPE)
307 flag = flag & ~wxLC_MASK_TYPE;
308 if ( style & wxLC_MASK_ALIGN )
309 flag = flag & ~wxLC_MASK_ALIGN;
310 if ( style & wxLC_MASK_SORT )
311 flag = flag & ~wxLC_MASK_SORT;
312 }
313
314 if ( flag & style )
315 {
316 if ( !add )
317 flag -= style;
318 }
319 else
320 {
321 if ( add )
322 {
323 flag |= style;
324 }
325 }
326
327 m_windowStyle = flag;
328
329 UpdateStyle();
330 }
331
332 // Set the whole window style
333 void wxListCtrl::SetWindowStyleFlag(long flag)
334 {
335 m_windowStyle = flag;
336
337 UpdateStyle();
338 }
339
340 // Can be just a single style, or a bitlist
341 long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const
342 {
343 long wstyle = 0;
344 if ( style & wxLC_ICON )
345 {
346 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
347 oldStyle -= LVS_SMALLICON;
348 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
349 oldStyle -= LVS_REPORT;
350 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
351 oldStyle -= LVS_LIST;
352 wstyle |= LVS_ICON;
353 }
354
355 if ( style & wxLC_SMALL_ICON )
356 {
357 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
358 oldStyle -= LVS_ICON;
359 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
360 oldStyle -= LVS_REPORT;
361 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
362 oldStyle -= LVS_LIST;
363 wstyle |= LVS_SMALLICON;
364 }
365
366 if ( style & wxLC_LIST )
367 {
368 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
369 oldStyle -= LVS_ICON;
370 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
371 oldStyle -= LVS_REPORT;
372 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
373 oldStyle -= LVS_SMALLICON;
374 wstyle |= LVS_LIST;
375 }
376
377 if ( style & wxLC_REPORT )
378 {
379 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
380 oldStyle -= LVS_ICON;
381 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
382 oldStyle -= LVS_LIST;
383 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
384 oldStyle -= LVS_SMALLICON;
385
386 wstyle |= LVS_REPORT;
387 }
388
389 if ( style & wxLC_ALIGN_LEFT )
390 {
391 if ( oldStyle & LVS_ALIGNTOP )
392 oldStyle -= LVS_ALIGNTOP;
393 wstyle |= LVS_ALIGNLEFT;
394 }
395
396 if ( style & wxLC_ALIGN_TOP )
397 {
398 if ( oldStyle & LVS_ALIGNLEFT )
399 oldStyle -= LVS_ALIGNLEFT;
400 wstyle |= LVS_ALIGNTOP;
401 }
402
403 if ( style & wxLC_AUTOARRANGE )
404 wstyle |= LVS_AUTOARRANGE;
405
406 // Apparently, no such style (documentation wrong?)
407 /*
408 if ( style & wxLC_BUTTON )
409 wstyle |= LVS_BUTTON;
410 */
411
412 if ( style & wxLC_NO_SORT_HEADER )
413 wstyle |= LVS_NOSORTHEADER;
414
415 if ( style & wxLC_NO_HEADER )
416 wstyle |= LVS_NOCOLUMNHEADER;
417
418 if ( style & wxLC_EDIT_LABELS )
419 wstyle |= LVS_EDITLABELS;
420
421 if ( style & wxLC_SINGLE_SEL )
422 wstyle |= LVS_SINGLESEL;
423
424 if ( style & wxLC_SORT_ASCENDING )
425 {
426 if ( oldStyle & LVS_SORTDESCENDING )
427 oldStyle -= LVS_SORTDESCENDING;
428 wstyle |= LVS_SORTASCENDING;
429 }
430
431 if ( style & wxLC_SORT_DESCENDING )
432 {
433 if ( oldStyle & LVS_SORTASCENDING )
434 oldStyle -= LVS_SORTASCENDING;
435 wstyle |= LVS_SORTDESCENDING;
436 }
437
438 return wstyle;
439 }
440
441 // ----------------------------------------------------------------------------
442 // accessors
443 // ----------------------------------------------------------------------------
444
445 // Sets the foreground, i.e. text, colour
446 bool wxListCtrl::SetForegroundColour(const wxColour& col)
447 {
448 if ( !wxWindow::SetForegroundColour(col) )
449 return FALSE;
450
451 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
452
453 return TRUE;
454 }
455
456 // Sets the background colour
457 bool wxListCtrl::SetBackgroundColour(const wxColour& col)
458 {
459 if ( !wxWindow::SetBackgroundColour(col) )
460 return FALSE;
461
462 // we set the same colour for both the "empty" background and the items
463 // background
464 COLORREF color = wxColourToRGB(col);
465 ListView_SetBkColor(GetHwnd(), color);
466 ListView_SetTextBkColor(GetHwnd(), color);
467
468 return TRUE;
469 }
470
471 // Gets information about this column
472 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
473 {
474 LV_COLUMN lvCol;
475 lvCol.mask = 0;
476 lvCol.fmt = 0;
477 lvCol.pszText = NULL;
478
479 if ( item.m_mask & wxLIST_MASK_TEXT )
480 {
481 lvCol.mask |= LVCF_TEXT;
482 lvCol.pszText = new wxChar[513];
483 lvCol.cchTextMax = 512;
484 }
485
486 bool success = (ListView_GetColumn(GetHwnd(), col, & lvCol) != 0);
487
488 // item.m_subItem = lvCol.iSubItem;
489 item.m_width = lvCol.cx;
490
491 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
492 {
493 item.m_text = lvCol.pszText;
494 delete[] lvCol.pszText;
495 }
496
497 if ( item.m_mask & wxLIST_MASK_FORMAT )
498 {
499 if (lvCol.fmt == LVCFMT_LEFT)
500 item.m_format = wxLIST_FORMAT_LEFT;
501 else if (lvCol.fmt == LVCFMT_RIGHT)
502 item.m_format = wxLIST_FORMAT_RIGHT;
503 else if (lvCol.fmt == LVCFMT_CENTER)
504 item.m_format = wxLIST_FORMAT_CENTRE;
505 }
506
507 return success;
508 }
509
510 // Sets information about this column
511 bool wxListCtrl::SetColumn(int col, wxListItem& item)
512 {
513 LV_COLUMN lvCol;
514 lvCol.mask = 0;
515 lvCol.fmt = 0;
516 lvCol.pszText = NULL;
517
518 if ( item.m_mask & wxLIST_MASK_TEXT )
519 {
520 lvCol.mask |= LVCF_TEXT;
521 lvCol.pszText = WXSTRINGCAST item.m_text;
522 lvCol.cchTextMax = 0; // Ignored
523 }
524 if ( item.m_mask & wxLIST_MASK_FORMAT )
525 {
526 lvCol.mask |= LVCF_FMT;
527
528 if ( item.m_format == wxLIST_FORMAT_LEFT )
529 lvCol.fmt = LVCFMT_LEFT;
530 if ( item.m_format == wxLIST_FORMAT_RIGHT )
531 lvCol.fmt = LVCFMT_RIGHT;
532 if ( item.m_format == wxLIST_FORMAT_CENTRE )
533 lvCol.fmt = LVCFMT_CENTER;
534 }
535
536 if ( item.m_mask & wxLIST_MASK_WIDTH )
537 {
538 lvCol.mask |= LVCF_WIDTH;
539 lvCol.cx = item.m_width;
540
541 if ( lvCol.cx == wxLIST_AUTOSIZE)
542 lvCol.cx = LVSCW_AUTOSIZE;
543 else if ( lvCol.cx == wxLIST_AUTOSIZE_USEHEADER)
544 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
545 }
546 lvCol.mask |= LVCF_SUBITEM;
547 lvCol.iSubItem = col;
548 return (ListView_SetColumn(GetHwnd(), col, & lvCol) != 0);
549 }
550
551 // Gets the column width
552 int wxListCtrl::GetColumnWidth(int col) const
553 {
554 return ListView_GetColumnWidth(GetHwnd(), col);
555 }
556
557 // Sets the column width
558 bool wxListCtrl::SetColumnWidth(int col, int width)
559 {
560 int col2 = col;
561 if ( m_windowStyle & wxLC_LIST )
562 col2 = -1;
563
564 int width2 = width;
565 if ( width2 == wxLIST_AUTOSIZE)
566 width2 = LVSCW_AUTOSIZE;
567 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
568 width2 = LVSCW_AUTOSIZE_USEHEADER;
569
570 return (ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0);
571 }
572
573 // Gets the number of items that can fit vertically in the
574 // visible area of the list control (list or report view)
575 // or the total number of items in the list control (icon
576 // or small icon view)
577 int wxListCtrl::GetCountPerPage() const
578 {
579 return ListView_GetCountPerPage(GetHwnd());
580 }
581
582 // Gets the edit control for editing labels.
583 wxTextCtrl* wxListCtrl::GetEditControl() const
584 {
585 return m_textCtrl;
586 }
587
588 // Gets information about the item
589 bool wxListCtrl::GetItem(wxListItem& info) const
590 {
591 LV_ITEM lvItem;
592 wxZeroMemory(lvItem);
593
594 lvItem.iItem = info.m_itemId;
595 lvItem.iSubItem = info.m_col;
596
597 if ( info.m_mask & wxLIST_MASK_TEXT )
598 {
599 lvItem.mask |= LVIF_TEXT;
600 lvItem.pszText = new wxChar[513];
601 lvItem.cchTextMax = 512;
602 }
603 else
604 {
605 lvItem.pszText = NULL;
606 }
607
608 if (info.m_mask & wxLIST_MASK_DATA)
609 lvItem.mask |= LVIF_PARAM;
610
611 if (info.m_mask & wxLIST_MASK_IMAGE)
612 lvItem.mask |= LVIF_IMAGE;
613
614 if ( info.m_mask & wxLIST_MASK_STATE )
615 {
616 lvItem.mask |= LVIF_STATE;
617 // the other bits are hardly interesting anyhow
618 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
619 }
620
621 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
622 if ( !success )
623 {
624 wxLogError(_("Couldn't retrieve information about list control item %d."),
625 lvItem.iItem);
626 }
627 else
628 {
629 wxConvertFromMSWListItem(this, info, lvItem);
630 }
631
632 if (lvItem.pszText)
633 delete[] lvItem.pszText;
634
635 return success;
636 }
637
638 // Sets information about the item
639 bool wxListCtrl::SetItem(wxListItem& info)
640 {
641 LV_ITEM item;
642 wxConvertToMSWListItem(this, info, item);
643
644 // check whether it has any custom attributes
645 if ( info.HasAttributes() )
646 {
647
648 wxListItemAttr *attr;
649 attr = (wxListItemAttr*) m_attrs.Get(item.iItem);
650
651 if (attr == NULL)
652
653 m_attrs.Put(item.iItem, (wxObject *)new wxListItemAttr(*info.GetAttributes()));
654
655 else *attr = *info.GetAttributes();
656
657 m_hasAnyAttr = TRUE;
658 }
659
660 item.cchTextMax = 0;
661 bool ok = ListView_SetItem(GetHwnd(), &item) != 0;
662 if ( ok && (info.m_mask & wxLIST_MASK_IMAGE) )
663 {
664 // make the change visible
665 ListView_Update(GetHwnd(), item.iItem);
666 }
667
668 return ok;
669 }
670
671 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
672 {
673 wxListItem info;
674 info.m_text = label;
675 info.m_mask = wxLIST_MASK_TEXT;
676 info.m_itemId = index;
677 info.m_col = col;
678 if ( imageId > -1 )
679 {
680 info.m_image = imageId;
681 info.m_mask |= wxLIST_MASK_IMAGE;
682 }
683 return SetItem(info);
684 }
685
686
687 // Gets the item state
688 int wxListCtrl::GetItemState(long item, long stateMask) const
689 {
690 wxListItem info;
691
692 info.m_mask = wxLIST_MASK_STATE;
693 info.m_stateMask = stateMask;
694 info.m_itemId = item;
695
696 if (!GetItem(info))
697 return 0;
698
699 return info.m_state;
700 }
701
702 // Sets the item state
703 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
704 {
705 wxListItem info;
706
707 info.m_mask = wxLIST_MASK_STATE;
708 info.m_state = state;
709 info.m_stateMask = stateMask;
710 info.m_itemId = item;
711
712 return SetItem(info);
713 }
714
715 // Sets the item image
716 bool wxListCtrl::SetItemImage(long item, int image, int selImage)
717 {
718 wxListItem info;
719
720 info.m_mask = wxLIST_MASK_IMAGE;
721 info.m_image = image;
722 info.m_itemId = item;
723
724 return SetItem(info);
725 }
726
727 // Gets the item text
728 wxString wxListCtrl::GetItemText(long item) const
729 {
730 wxListItem info;
731
732 info.m_mask = wxLIST_MASK_TEXT;
733 info.m_itemId = item;
734
735 if (!GetItem(info))
736 return wxString("");
737 return info.m_text;
738 }
739
740 // Sets the item text
741 void wxListCtrl::SetItemText(long item, const wxString& str)
742 {
743 wxListItem info;
744
745 info.m_mask = wxLIST_MASK_TEXT;
746 info.m_itemId = item;
747 info.m_text = str;
748
749 SetItem(info);
750 }
751
752 // Gets the item data
753 long wxListCtrl::GetItemData(long item) const
754 {
755 wxListItem info;
756
757 info.m_mask = wxLIST_MASK_DATA;
758 info.m_itemId = item;
759
760 if (!GetItem(info))
761 return 0;
762 return info.m_data;
763 }
764
765 // Sets the item data
766 bool wxListCtrl::SetItemData(long item, long data)
767 {
768 wxListItem info;
769
770 info.m_mask = wxLIST_MASK_DATA;
771 info.m_itemId = item;
772 info.m_data = data;
773
774 return SetItem(info);
775 }
776
777 // Gets the item rectangle
778 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
779 {
780 RECT rect2;
781
782 int code2 = LVIR_BOUNDS;
783 if ( code == wxLIST_RECT_BOUNDS )
784 code2 = LVIR_BOUNDS;
785 else if ( code == wxLIST_RECT_ICON )
786 code2 = LVIR_ICON;
787 else if ( code == wxLIST_RECT_LABEL )
788 code2 = LVIR_LABEL;
789
790 #ifdef __WXWINE__
791 bool success = (ListView_GetItemRect(GetHwnd(), (int) item, &rect2 ) != 0);
792 #else
793 bool success = (ListView_GetItemRect(GetHwnd(), (int) item, &rect2, code2) != 0);
794 #endif
795
796 rect.x = rect2.left;
797 rect.y = rect2.top;
798 rect.width = rect2.right - rect2.left;
799 rect.height = rect2.bottom - rect2.top;
800 return success;
801 }
802
803 // Gets the item position
804 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
805 {
806 POINT pt;
807
808 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
809
810 pos.x = pt.x; pos.y = pt.y;
811 return success;
812 }
813
814 // Sets the item position.
815 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
816 {
817 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
818 }
819
820 // Gets the number of items in the list control
821 int wxListCtrl::GetItemCount() const
822 {
823 return ListView_GetItemCount(GetHwnd());
824 }
825
826 // Retrieves the spacing between icons in pixels.
827 // If small is TRUE, gets the spacing for the small icon
828 // view, otherwise the large icon view.
829 int wxListCtrl::GetItemSpacing(bool isSmall) const
830 {
831 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
832 }
833
834 // Gets the number of selected items in the list control
835 int wxListCtrl::GetSelectedItemCount() const
836 {
837 return ListView_GetSelectedCount(GetHwnd());
838 }
839
840 // Gets the text colour of the listview
841 wxColour wxListCtrl::GetTextColour() const
842 {
843 COLORREF ref = ListView_GetTextColor(GetHwnd());
844 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
845 return col;
846 }
847
848 // Sets the text colour of the listview
849 void wxListCtrl::SetTextColour(const wxColour& col)
850 {
851 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
852 }
853
854 // Gets the index of the topmost visible item when in
855 // list or report view
856 long wxListCtrl::GetTopItem() const
857 {
858 return (long) ListView_GetTopIndex(GetHwnd());
859 }
860
861 // Searches for an item, starting from 'item'.
862 // 'geometry' is one of
863 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
864 // 'state' is a state bit flag, one or more of
865 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
866 // item can be -1 to find the first item that matches the
867 // specified flags.
868 // Returns the item or -1 if unsuccessful.
869 long wxListCtrl::GetNextItem(long item, int geom, int state) const
870 {
871 long flags = 0;
872
873 if ( geom == wxLIST_NEXT_ABOVE )
874 flags |= LVNI_ABOVE;
875 if ( geom == wxLIST_NEXT_ALL )
876 flags |= LVNI_ALL;
877 if ( geom == wxLIST_NEXT_BELOW )
878 flags |= LVNI_BELOW;
879 if ( geom == wxLIST_NEXT_LEFT )
880 flags |= LVNI_TOLEFT;
881 if ( geom == wxLIST_NEXT_RIGHT )
882 flags |= LVNI_TORIGHT;
883
884 if ( state & wxLIST_STATE_CUT )
885 flags |= LVNI_CUT;
886 if ( state & wxLIST_STATE_DROPHILITED )
887 flags |= LVNI_DROPHILITED;
888 if ( state & wxLIST_STATE_FOCUSED )
889 flags |= LVNI_FOCUSED;
890 if ( state & wxLIST_STATE_SELECTED )
891 flags |= LVNI_SELECTED;
892
893 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
894 }
895
896
897 wxImageList *wxListCtrl::GetImageList(int which) const
898 {
899 if ( which == wxIMAGE_LIST_NORMAL )
900 {
901 return m_imageListNormal;
902 }
903 else if ( which == wxIMAGE_LIST_SMALL )
904 {
905 return m_imageListSmall;
906 }
907 else if ( which == wxIMAGE_LIST_STATE )
908 {
909 return m_imageListState;
910 }
911 return NULL;
912 }
913
914 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
915 {
916 int flags = 0;
917 if ( which == wxIMAGE_LIST_NORMAL )
918 {
919 flags = LVSIL_NORMAL;
920 if (m_ownsImageListNormal) delete m_imageListNormal;
921 m_imageListNormal = imageList;
922 m_ownsImageListNormal = FALSE;
923 }
924 else if ( which == wxIMAGE_LIST_SMALL )
925 {
926 flags = LVSIL_SMALL;
927 if (m_ownsImageListSmall) delete m_imageListSmall;
928 m_imageListSmall = imageList;
929 m_ownsImageListSmall = FALSE;
930 }
931 else if ( which == wxIMAGE_LIST_STATE )
932 {
933 flags = LVSIL_STATE;
934 if (m_ownsImageListState) delete m_imageListState;
935 m_imageListState = imageList;
936 m_ownsImageListState = FALSE;
937 }
938 ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
939 }
940
941 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
942 {
943 SetImageList(imageList, which);
944 if ( which == wxIMAGE_LIST_NORMAL )
945 m_ownsImageListNormal = TRUE;
946 else if ( which == wxIMAGE_LIST_SMALL )
947 m_ownsImageListSmall = TRUE;
948 else if ( which == wxIMAGE_LIST_STATE )
949 m_ownsImageListState = TRUE;
950 }
951
952 // ----------------------------------------------------------------------------
953 // Operations
954 // ----------------------------------------------------------------------------
955
956 // Arranges the items
957 bool wxListCtrl::Arrange(int flag)
958 {
959 UINT code = 0;
960 if ( flag == wxLIST_ALIGN_LEFT )
961 code = LVA_ALIGNLEFT;
962 else if ( flag == wxLIST_ALIGN_TOP )
963 code = LVA_ALIGNTOP;
964 else if ( flag == wxLIST_ALIGN_DEFAULT )
965 code = LVA_DEFAULT;
966 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
967 code = LVA_SNAPTOGRID;
968
969 return (ListView_Arrange(GetHwnd(), code) != 0);
970 }
971
972 // Deletes an item
973 bool wxListCtrl::DeleteItem(long item)
974 {
975 return (ListView_DeleteItem(GetHwnd(), (int) item) != 0);
976 }
977
978 // Deletes all items
979 bool wxListCtrl::DeleteAllItems()
980 {
981 return (ListView_DeleteAllItems(GetHwnd()) != 0);
982 }
983
984 // Deletes all items
985 bool wxListCtrl::DeleteAllColumns()
986 {
987 while ( m_colCount > 0 )
988 {
989 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
990 {
991 wxLogLastError(wxT("ListView_DeleteColumn"));
992
993 return FALSE;
994 }
995
996 m_colCount--;
997 }
998
999 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1000
1001 return TRUE;
1002 }
1003
1004 // Deletes a column
1005 bool wxListCtrl::DeleteColumn(int col)
1006 {
1007 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1008
1009 if ( success && (m_colCount > 0) )
1010 m_colCount --;
1011 return success;
1012 }
1013
1014 // Clears items, and columns if there are any.
1015 void wxListCtrl::ClearAll()
1016 {
1017 DeleteAllItems();
1018 if ( m_colCount > 0 )
1019 DeleteAllColumns();
1020 }
1021
1022 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1023 {
1024 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1025
1026 HWND hWnd = (HWND) ListView_EditLabel(GetHwnd(), item);
1027
1028 if (m_textCtrl)
1029 {
1030 m_textCtrl->UnsubclassWin();
1031 m_textCtrl->SetHWND(0);
1032 delete m_textCtrl;
1033 m_textCtrl = NULL;
1034 }
1035
1036 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
1037 m_textCtrl->SetHWND((WXHWND) hWnd);
1038 m_textCtrl->SubclassWin((WXHWND) hWnd);
1039
1040 return m_textCtrl;
1041 }
1042
1043 // End label editing, optionally cancelling the edit
1044 bool wxListCtrl::EndEditLabel(bool cancel)
1045 {
1046 wxFAIL;
1047
1048 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
1049 * ???
1050 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
1051
1052 if (m_textCtrl)
1053 {
1054 m_textCtrl->UnsubclassWin();
1055 m_textCtrl->SetHWND(0);
1056 delete m_textCtrl;
1057 m_textCtrl = NULL;
1058 }
1059 return success;
1060 */
1061 return FALSE;
1062 }
1063
1064
1065 // Ensures this item is visible
1066 bool wxListCtrl::EnsureVisible(long item)
1067 {
1068 return (ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0);
1069 }
1070
1071 // Find an item whose label matches this string, starting from the item after 'start'
1072 // or the beginning if 'start' is -1.
1073 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1074 {
1075 LV_FINDINFO findInfo;
1076
1077 findInfo.flags = LVFI_STRING;
1078 if ( partial )
1079 findInfo.flags |= LVFI_PARTIAL;
1080 findInfo.psz = str;
1081
1082 // ListView_FindItem() excludes the first item from search and to look
1083 // through all the items you need to start from -1 which is unnatural and
1084 // inconsitent with the generic version - so we adjust the index
1085 return ListView_FindItem(GetHwnd(), (int) start - 1, &findInfo);
1086 }
1087
1088 // Find an item whose data matches this data, starting from the item after 'start'
1089 // or the beginning if 'start' is -1.
1090 long wxListCtrl::FindItem(long start, long data)
1091 {
1092 LV_FINDINFO findInfo;
1093
1094 findInfo.flags = LVFI_PARAM;
1095 findInfo.lParam = data;
1096
1097 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1098 }
1099
1100 // Find an item nearest this position in the specified direction, starting from
1101 // the item after 'start' or the beginning if 'start' is -1.
1102 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1103 {
1104 LV_FINDINFO findInfo;
1105
1106 findInfo.flags = LVFI_NEARESTXY;
1107 findInfo.pt.x = pt.x;
1108 findInfo.pt.y = pt.y;
1109 findInfo.vkDirection = VK_RIGHT;
1110
1111 if ( direction == wxLIST_FIND_UP )
1112 findInfo.vkDirection = VK_UP;
1113 else if ( direction == wxLIST_FIND_DOWN )
1114 findInfo.vkDirection = VK_DOWN;
1115 else if ( direction == wxLIST_FIND_LEFT )
1116 findInfo.vkDirection = VK_LEFT;
1117 else if ( direction == wxLIST_FIND_RIGHT )
1118 findInfo.vkDirection = VK_RIGHT;
1119
1120 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1121 }
1122
1123 // Determines which item (if any) is at the specified point,
1124 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1125 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1126 {
1127 LV_HITTESTINFO hitTestInfo;
1128 hitTestInfo.pt.x = (int) point.x;
1129 hitTestInfo.pt.y = (int) point.y;
1130
1131 ListView_HitTest(GetHwnd(), & hitTestInfo);
1132
1133 flags = 0;
1134 if ( hitTestInfo.flags & LVHT_ABOVE )
1135 flags |= wxLIST_HITTEST_ABOVE;
1136 if ( hitTestInfo.flags & LVHT_BELOW )
1137 flags |= wxLIST_HITTEST_BELOW;
1138 if ( hitTestInfo.flags & LVHT_NOWHERE )
1139 flags |= wxLIST_HITTEST_NOWHERE;
1140 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1141 flags |= wxLIST_HITTEST_ONITEMICON;
1142 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1143 flags |= wxLIST_HITTEST_ONITEMLABEL;
1144 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1145 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1146 if ( hitTestInfo.flags & LVHT_TOLEFT )
1147 flags |= wxLIST_HITTEST_TOLEFT;
1148 if ( hitTestInfo.flags & LVHT_TORIGHT )
1149 flags |= wxLIST_HITTEST_TORIGHT;
1150
1151 return (long) hitTestInfo.iItem;
1152 }
1153
1154 // Inserts an item, returning the index of the new item if successful,
1155 // -1 otherwise.
1156 long wxListCtrl::InsertItem(wxListItem& info)
1157 {
1158 LV_ITEM item;
1159 wxConvertToMSWListItem(this, info, item);
1160
1161 // check whether it has any custom attributes
1162 if ( info.HasAttributes() )
1163 {
1164
1165 wxListItemAttr *attr;
1166 attr = (wxListItemAttr*) m_attrs.Get(item.iItem);
1167
1168 if (attr == NULL)
1169
1170 m_attrs.Put(item.iItem, (wxObject *)new wxListItemAttr(*info.GetAttributes()));
1171
1172 else *attr = *info.GetAttributes();
1173
1174 m_hasAnyAttr = TRUE;
1175 }
1176
1177 return (long) ListView_InsertItem(GetHwnd(), & item);
1178 }
1179
1180 long wxListCtrl::InsertItem(long index, const wxString& label)
1181 {
1182 wxListItem info;
1183 info.m_text = label;
1184 info.m_mask = wxLIST_MASK_TEXT;
1185 info.m_itemId = index;
1186 return InsertItem(info);
1187 }
1188
1189 // Inserts an image item
1190 long wxListCtrl::InsertItem(long index, int imageIndex)
1191 {
1192 wxListItem info;
1193 info.m_image = imageIndex;
1194 info.m_mask = wxLIST_MASK_IMAGE;
1195 info.m_itemId = index;
1196 return InsertItem(info);
1197 }
1198
1199 // Inserts an image/string item
1200 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1201 {
1202 wxListItem info;
1203 info.m_image = imageIndex;
1204 info.m_text = label;
1205 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1206 info.m_itemId = index;
1207 return InsertItem(info);
1208 }
1209
1210 // For list view mode (only), inserts a column.
1211 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1212 {
1213 LV_COLUMN lvCol;
1214 lvCol.mask = 0;
1215 lvCol.fmt = 0;
1216 lvCol.pszText = NULL;
1217
1218 if ( item.m_mask & wxLIST_MASK_TEXT )
1219 {
1220 lvCol.mask |= LVCF_TEXT;
1221 lvCol.pszText = WXSTRINGCAST item.m_text;
1222 lvCol.cchTextMax = 0; // Ignored
1223 }
1224 if ( item.m_mask & wxLIST_MASK_FORMAT )
1225 {
1226 lvCol.mask |= LVCF_FMT;
1227
1228 if ( item.m_format == wxLIST_FORMAT_LEFT )
1229 lvCol.fmt = LVCFMT_LEFT;
1230 if ( item.m_format == wxLIST_FORMAT_RIGHT )
1231 lvCol.fmt = LVCFMT_RIGHT;
1232 if ( item.m_format == wxLIST_FORMAT_CENTRE )
1233 lvCol.fmt = LVCFMT_CENTER;
1234 }
1235
1236 lvCol.mask |= LVCF_WIDTH;
1237 if ( item.m_mask & wxLIST_MASK_WIDTH )
1238 {
1239 if ( item.m_width == wxLIST_AUTOSIZE)
1240 lvCol.cx = LVSCW_AUTOSIZE;
1241 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
1242 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
1243 else
1244 lvCol.cx = item.m_width;
1245 }
1246 else
1247 {
1248 // always give some width to the new column: this one is compatible
1249 // with wxGTK
1250 lvCol.cx = 80;
1251 }
1252
1253 lvCol.mask |= LVCF_SUBITEM;
1254 lvCol.iSubItem = col;
1255
1256 bool success = ListView_InsertColumn(GetHwnd(), col, & lvCol) != -1;
1257 if ( success )
1258 {
1259 m_colCount++;
1260 }
1261 else
1262 {
1263 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1264 lvCol.pszText);
1265 }
1266
1267 return success;
1268 }
1269
1270 long wxListCtrl::InsertColumn(long col,
1271 const wxString& heading,
1272 int format,
1273 int width)
1274 {
1275 wxListItem item;
1276 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1277 item.m_text = heading;
1278 if ( width > -1 )
1279 {
1280 item.m_mask |= wxLIST_MASK_WIDTH;
1281 item.m_width = width;
1282 }
1283 item.m_format = format;
1284
1285 return InsertColumn(col, item);
1286 }
1287
1288 // Scrolls the list control. If in icon, small icon or report view mode,
1289 // x specifies the number of pixels to scroll. If in list view mode, x
1290 // specifies the number of columns to scroll.
1291 // If in icon, small icon or list view mode, y specifies the number of pixels
1292 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1293 bool wxListCtrl::ScrollList(int dx, int dy)
1294 {
1295 return (ListView_Scroll(GetHwnd(), dx, dy) != 0);
1296 }
1297
1298 // Sort items.
1299
1300 // fn is a function which takes 3 long arguments: item1, item2, data.
1301 // item1 is the long data associated with a first item (NOT the index).
1302 // item2 is the long data associated with a second item (NOT the index).
1303 // data is the same value as passed to SortItems.
1304 // The return value is a negative number if the first item should precede the second
1305 // item, a positive number of the second item should precede the first,
1306 // or zero if the two items are equivalent.
1307
1308 // data is arbitrary data to be passed to the sort function.
1309 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1310 {
1311 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE) fn, data) != 0);
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // message processing
1316 // ----------------------------------------------------------------------------
1317
1318 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1319 {
1320 if (cmd == EN_UPDATE)
1321 {
1322 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1323 event.SetEventObject( this );
1324 ProcessCommand(event);
1325 return TRUE;
1326 }
1327 else if (cmd == EN_KILLFOCUS)
1328 {
1329 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1330 event.SetEventObject( this );
1331 ProcessCommand(event);
1332 return TRUE;
1333 }
1334 else
1335 return FALSE;
1336 }
1337
1338 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1339 {
1340 // prepare the event
1341 // -----------------
1342
1343 wxListEvent event(wxEVT_NULL, m_windowId);
1344 wxEventType eventType = wxEVT_NULL;
1345
1346 NMHDR *nmhdr = (NMHDR *)lParam;
1347
1348 // almost all messages use NM_LISTVIEW
1349 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1350
1351 // this is true for almost all events
1352 event.m_item.m_data = nmLV->lParam;
1353
1354 switch ( nmhdr->code )
1355 {
1356 case LVN_BEGINRDRAG:
1357 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1358 // fall through
1359
1360 case LVN_BEGINDRAG:
1361 if ( eventType == wxEVT_NULL )
1362 {
1363 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1364 }
1365
1366 event.m_itemIndex = nmLV->iItem;
1367 event.m_pointDrag.x = nmLV->ptAction.x;
1368 event.m_pointDrag.y = nmLV->ptAction.y;
1369 break;
1370
1371 case LVN_BEGINLABELEDIT:
1372 {
1373 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1374 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1375 wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
1376 }
1377 break;
1378
1379 case LVN_COLUMNCLICK:
1380 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1381 event.m_itemIndex = -1;
1382 event.m_col = nmLV->iSubItem;
1383 break;
1384
1385 case LVN_DELETEALLITEMS:
1386 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1387 event.m_itemIndex = -1;
1388
1389 FreeAllAttrs();
1390
1391 break;
1392
1393 case LVN_DELETEITEM:
1394 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1395 event.m_itemIndex = nmLV->iItem;
1396
1397 if ( m_hasAnyAttr )
1398 {
1399 delete (wxListItemAttr *)m_attrs.Delete(nmLV->iItem);
1400 }
1401 break;
1402
1403 case LVN_ENDLABELEDIT:
1404 {
1405 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1406 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1407 wxConvertFromMSWListItem(this, event.m_item, info->item);
1408 if ( info->item.pszText == NULL || info->item.iItem == -1 )
1409 return FALSE;
1410 }
1411 break;
1412
1413 case LVN_SETDISPINFO:
1414 {
1415 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1416 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1417 wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
1418 }
1419 break;
1420
1421 case LVN_GETDISPINFO:
1422 // this provokes stack overflow: indeed, wxConvertFromMSWListItem()
1423 // sends us WM_NOTIFY! As it doesn't do anything for now, just leave
1424 // it out.
1425 #if 0
1426 {
1427 // TODO: some text buffering here, I think
1428 // TODO: API for getting Windows to retrieve values
1429 // on demand.
1430 eventType = wxEVT_COMMAND_LIST_GET_INFO;
1431 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1432 wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
1433 break;
1434 }
1435 #endif // 0
1436 return FALSE;
1437
1438
1439 case LVN_INSERTITEM:
1440 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1441 event.m_itemIndex = nmLV->iItem;
1442 break;
1443
1444 case LVN_ITEMCHANGED:
1445 // This needs to be sent to wxListCtrl as a rather more concrete
1446 // event. For now, just detect a selection or deselection.
1447 if ( (nmLV->uNewState & LVIS_SELECTED) && !(nmLV->uOldState & LVIS_SELECTED) )
1448 {
1449 eventType = wxEVT_COMMAND_LIST_ITEM_SELECTED;
1450 event.m_itemIndex = nmLV->iItem;
1451 }
1452 else if ( !(nmLV->uNewState & LVIS_SELECTED) && (nmLV->uOldState & LVIS_SELECTED) )
1453 {
1454 eventType = wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1455 event.m_itemIndex = nmLV->iItem;
1456 }
1457 else
1458 {
1459 return FALSE;
1460 }
1461 break;
1462
1463 case LVN_KEYDOWN:
1464 {
1465 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1466 WORD wVKey = info->wVKey;
1467
1468 // get the current selection
1469 long lItem = GetNextItem(-1,
1470 wxLIST_NEXT_ALL,
1471 wxLIST_STATE_SELECTED);
1472
1473 // <Enter> or <Space> activate the selected item if any
1474 if ( lItem != -1 && (wVKey == VK_RETURN || wVKey == VK_SPACE) )
1475 {
1476 // TODO this behaviour probably should be optional
1477 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1478 event.m_itemIndex = lItem;
1479 }
1480 else
1481 {
1482 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
1483 event.m_code = wxCharCodeMSWToWX(wVKey);
1484 }
1485
1486 if ( lItem != -1 )
1487 {
1488 // fill the other fields too
1489 event.m_item.m_text = GetItemText(lItem);
1490 event.m_item.m_data = GetItemData(lItem);
1491 }
1492 }
1493 break;
1494
1495 case NM_DBLCLK:
1496 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1497 // anything else
1498 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1499 {
1500 return TRUE;
1501 }
1502
1503 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1504 // if it happened on an item (and not on empty place)
1505 if ( nmLV->iItem == -1 )
1506 {
1507 // not on item
1508 return FALSE;
1509 }
1510
1511 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1512 event.m_itemIndex = nmLV->iItem;
1513 event.m_item.m_text = GetItemText(nmLV->iItem);
1514 event.m_item.m_data = GetItemData(nmLV->iItem);
1515 break;
1516
1517 case NM_RCLICK:
1518 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1519 subclass and check for the actual WM_RBUTTONDOWN message,
1520 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1521 before firing off. We want to have notify events for both down
1522 -and- up. */
1523 {
1524 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1525 // don't do anything else
1526 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1527 {
1528 return TRUE;
1529 }
1530
1531 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1532 LV_HITTESTINFO lvhti;
1533 wxZeroMemory(lvhti);
1534
1535 ::GetCursorPos(&(lvhti.pt));
1536 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
1537 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
1538 {
1539 if ( lvhti.flags & LVHT_ONITEM )
1540 {
1541 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
1542 event.m_itemIndex = lvhti.iItem;
1543 }
1544 }
1545 }
1546 break;
1547
1548 #if 0
1549 case NM_MCLICK: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1550 {
1551 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1552 // anything else
1553 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1554 {
1555 return TRUE;
1556 }
1557
1558 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1559 eventType = wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK;
1560 NMITEMACTIVATE* hdr = (NMITEMACTIVATE*)lParam;
1561 event.m_itemIndex = hdr->iItem;
1562 }
1563 break;
1564 #endif // 0
1565
1566 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1567 case NM_CUSTOMDRAW:
1568 {
1569 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
1570 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
1571 switch( nmcd.dwDrawStage )
1572 {
1573 case CDDS_PREPAINT:
1574 // if we've got any items with non standard attributes,
1575 // notify us before painting each item
1576 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
1577 : CDRF_DODEFAULT;
1578 return TRUE;
1579
1580 case CDDS_ITEMPREPAINT:
1581 {
1582 wxListItemAttr *attr =
1583 (wxListItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
1584
1585 if ( !attr )
1586 {
1587 // nothing to do for this item
1588 return CDRF_DODEFAULT;
1589 }
1590
1591 HFONT hFont;
1592 wxColour colText, colBack;
1593 if ( attr->HasFont() )
1594 {
1595 wxFont font = attr->GetFont();
1596 hFont = (HFONT)font.GetResourceHandle();
1597 }
1598 else
1599 {
1600 hFont = 0;
1601 }
1602
1603 if ( attr->HasTextColour() )
1604 {
1605 colText = attr->GetTextColour();
1606 }
1607 else
1608 {
1609 colText = GetTextColour();
1610 }
1611
1612 if ( attr->HasBackgroundColour() )
1613 {
1614 colBack = attr->GetBackgroundColour();
1615 }
1616 else
1617 {
1618 colBack = GetBackgroundColour();
1619 }
1620
1621 // note that if we wanted to set colours for
1622 // individual columns (subitems), we would have
1623 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1624 if ( hFont )
1625 {
1626 ::SelectObject(nmcd.hdc, hFont);
1627
1628 *result = CDRF_NEWFONT;
1629 }
1630 else
1631 {
1632 *result = CDRF_DODEFAULT;
1633 }
1634
1635 lplvcd->clrText = wxColourToRGB(colText);
1636 lplvcd->clrTextBk = wxColourToRGB(colBack);
1637
1638 return TRUE;
1639 }
1640
1641 default:
1642 *result = CDRF_DODEFAULT;
1643 return TRUE;
1644 }
1645 }
1646 break;
1647 #endif // _WIN32_IE >= 0x300
1648
1649 default:
1650 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1651 }
1652
1653 // process the event
1654 // -----------------
1655
1656 event.SetEventObject( this );
1657 event.SetEventType(eventType);
1658
1659 if ( !GetEventHandler()->ProcessEvent(event) )
1660 return FALSE;
1661
1662 // post processing
1663 // ---------------
1664
1665 switch ( nmhdr->code )
1666 {
1667 case LVN_DELETEALLITEMS:
1668 // always return TRUE to suppress all additional LVN_DELETEITEM
1669 // notifications - this makes deleting all items from a list ctrl
1670 // much faster
1671 *result = TRUE;
1672
1673 return TRUE;
1674
1675 case LVN_GETDISPINFO:
1676 {
1677 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1678 if ( info->item.mask & LVIF_TEXT )
1679 {
1680 if ( !event.m_item.m_text.IsNull() )
1681 {
1682 info->item.pszText = AddPool(event.m_item.m_text);
1683 info->item.cchTextMax = wxStrlen(info->item.pszText) + 1;
1684 }
1685 }
1686 // wxConvertToMSWListItem(this, event.m_item, info->item);
1687 break;
1688 }
1689 case LVN_ENDLABELEDIT:
1690 {
1691 *result = event.IsAllowed();
1692 return TRUE;
1693 }
1694 }
1695
1696 *result = !event.IsAllowed();
1697
1698 return TRUE;
1699 }
1700
1701 wxChar *wxListCtrl::AddPool(const wxString& str)
1702 {
1703 // Remove the first element if 3 strings exist
1704 if ( m_stringPool.Number() == 3 )
1705 {
1706 wxNode *node = m_stringPool.First();
1707 delete[] (char *)node->Data();
1708 delete node;
1709 }
1710 wxNode *node = m_stringPool.Add(WXSTRINGCAST str);
1711 return (wxChar *)node->Data();
1712 }
1713
1714 // Necessary for drawing hrules and vrules, if specified
1715 void wxListCtrl::OnPaint(wxPaintEvent& event)
1716 {
1717 wxPaintDC dc(this);
1718
1719 wxControl::OnPaint(event);
1720
1721 // Reset the device origin since it may have been set
1722 dc.SetDeviceOrigin(0, 0);
1723
1724 bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0);
1725 bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0);
1726
1727 if (!drawHRules && !drawVRules)
1728 return;
1729 if ((GetWindowStyle() & wxLC_REPORT) == 0)
1730 return;
1731
1732 wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
1733 dc.SetPen(pen);
1734 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1735
1736 wxSize clientSize = GetClientSize();
1737 wxRect itemRect;
1738 int cy=0;
1739
1740 int itemCount = GetItemCount();
1741 int i;
1742 for (i = 0; i < itemCount; i++)
1743 {
1744 if (GetItemRect(i, itemRect))
1745 {
1746 cy = itemRect.GetTop();
1747 if (i != 0) // Don't draw the first one
1748 {
1749 dc.DrawLine(0, cy, clientSize.x, cy);
1750
1751 // Draw last line
1752 if (i == (GetItemCount() - 1))
1753 {
1754 cy = itemRect.GetBottom();
1755 dc.DrawLine(0, cy, clientSize.x, cy);
1756 }
1757 }
1758 }
1759 }
1760 i = (GetItemCount() - 1);
1761 if (drawVRules && (i > -1))
1762 {
1763 wxRect firstItemRect;
1764 GetItemRect(0, firstItemRect);
1765
1766 if (GetItemRect(i, itemRect))
1767 {
1768 int col;
1769 int x = itemRect.GetX();
1770 for (col = 0; col < GetColumnCount(); col++)
1771 {
1772 int colWidth = GetColumnWidth(col);
1773 x += colWidth ;
1774 dc.DrawLine(x, firstItemRect.GetY() - 2, x, itemRect.GetBottom());
1775 }
1776 }
1777 }
1778 }
1779
1780 // ----------------------------------------------------------------------------
1781 // wxListItem
1782 // ----------------------------------------------------------------------------
1783
1784 // List item structure
1785 wxListItem::wxListItem()
1786 {
1787 m_mask = 0;
1788 m_itemId = 0;
1789 m_col = 0;
1790 m_state = 0;
1791 m_stateMask = 0;
1792 m_image = 0;
1793 m_data = 0;
1794
1795 m_format = wxLIST_FORMAT_CENTRE;
1796 m_width = 0;
1797
1798 m_attr = NULL;
1799 }
1800
1801 void wxListItem::Clear()
1802 {
1803 m_mask = 0;
1804 m_itemId = 0;
1805 m_col = 0;
1806 m_state = 0;
1807 m_stateMask = 0;
1808 m_image = 0;
1809 m_data = 0;
1810 m_format = wxLIST_FORMAT_CENTRE;
1811 m_width = 0;
1812 m_text = wxEmptyString;
1813
1814 if (m_attr) delete m_attr;
1815 m_attr = NULL;
1816 }
1817
1818 void wxListItem::ClearAttributes()
1819 {
1820 if (m_attr) delete m_attr;
1821 m_attr = NULL;
1822 }
1823
1824 static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem, HWND getFullInfo)
1825 {
1826 info.m_data = lvItem.lParam;
1827 info.m_mask = 0;
1828 info.m_state = 0;
1829 info.m_stateMask = 0;
1830 info.m_itemId = lvItem.iItem;
1831
1832 long oldMask = lvItem.mask;
1833
1834 bool needText = FALSE;
1835 if (getFullInfo != 0)
1836 {
1837 if ( lvItem.mask & LVIF_TEXT )
1838 needText = FALSE;
1839 else
1840 needText = TRUE;
1841
1842 if ( needText )
1843 {
1844 lvItem.pszText = new wxChar[513];
1845 lvItem.cchTextMax = 512;
1846 }
1847 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1848 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
1849 ::SendMessage(getFullInfo, LVM_GETITEM, 0, (LPARAM)& lvItem);
1850 }
1851
1852 if ( lvItem.mask & LVIF_STATE )
1853 {
1854 info.m_mask |= wxLIST_MASK_STATE;
1855
1856 if ( lvItem.stateMask & LVIS_CUT)
1857 {
1858 info.m_stateMask |= wxLIST_STATE_CUT;
1859 if ( lvItem.state & LVIS_CUT )
1860 info.m_state |= wxLIST_STATE_CUT;
1861 }
1862 if ( lvItem.stateMask & LVIS_DROPHILITED)
1863 {
1864 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
1865 if ( lvItem.state & LVIS_DROPHILITED )
1866 info.m_state |= wxLIST_STATE_DROPHILITED;
1867 }
1868 if ( lvItem.stateMask & LVIS_FOCUSED)
1869 {
1870 info.m_stateMask |= wxLIST_STATE_FOCUSED;
1871 if ( lvItem.state & LVIS_FOCUSED )
1872 info.m_state |= wxLIST_STATE_FOCUSED;
1873 }
1874 if ( lvItem.stateMask & LVIS_SELECTED)
1875 {
1876 info.m_stateMask |= wxLIST_STATE_SELECTED;
1877 if ( lvItem.state & LVIS_SELECTED )
1878 info.m_state |= wxLIST_STATE_SELECTED;
1879 }
1880 }
1881
1882 if ( lvItem.mask & LVIF_TEXT )
1883 {
1884 info.m_mask |= wxLIST_MASK_TEXT;
1885 info.m_text = lvItem.pszText;
1886 }
1887 if ( lvItem.mask & LVIF_IMAGE )
1888 {
1889 info.m_mask |= wxLIST_MASK_IMAGE;
1890 info.m_image = lvItem.iImage;
1891 }
1892 if ( lvItem.mask & LVIF_PARAM )
1893 info.m_mask |= wxLIST_MASK_DATA;
1894 if ( lvItem.mask & LVIF_DI_SETITEM )
1895 info.m_mask |= wxLIST_SET_ITEM;
1896 info.m_col = lvItem.iSubItem;
1897
1898 if (needText)
1899 {
1900 if (lvItem.pszText)
1901 delete[] lvItem.pszText;
1902 }
1903 lvItem.mask = oldMask;
1904 }
1905
1906 static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem)
1907 {
1908 lvItem.iItem = (int) info.m_itemId;
1909
1910 lvItem.iImage = info.m_image;
1911 lvItem.lParam = info.m_data;
1912 lvItem.stateMask = 0;
1913 lvItem.state = 0;
1914 lvItem.mask = 0;
1915 lvItem.iSubItem = info.m_col;
1916
1917 if (info.m_mask & wxLIST_MASK_STATE)
1918 {
1919 lvItem.mask |= LVIF_STATE;
1920 if (info.m_stateMask & wxLIST_STATE_CUT)
1921 {
1922 lvItem.stateMask |= LVIS_CUT;
1923 if (info.m_state & wxLIST_STATE_CUT)
1924 lvItem.state |= LVIS_CUT;
1925 }
1926 if (info.m_stateMask & wxLIST_STATE_DROPHILITED)
1927 {
1928 lvItem.stateMask |= LVIS_DROPHILITED;
1929 if (info.m_state & wxLIST_STATE_DROPHILITED)
1930 lvItem.state |= LVIS_DROPHILITED;
1931 }
1932 if (info.m_stateMask & wxLIST_STATE_FOCUSED)
1933 {
1934 lvItem.stateMask |= LVIS_FOCUSED;
1935 if (info.m_state & wxLIST_STATE_FOCUSED)
1936 lvItem.state |= LVIS_FOCUSED;
1937 }
1938 if (info.m_stateMask & wxLIST_STATE_SELECTED)
1939 {
1940 lvItem.stateMask |= LVIS_SELECTED;
1941 if (info.m_state & wxLIST_STATE_SELECTED)
1942 lvItem.state |= LVIS_SELECTED;
1943 }
1944 }
1945
1946 if (info.m_mask & wxLIST_MASK_TEXT)
1947 {
1948 lvItem.mask |= LVIF_TEXT;
1949 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
1950 {
1951 lvItem.pszText = LPSTR_TEXTCALLBACK;
1952 }
1953 else
1954 {
1955 lvItem.pszText = WXSTRINGCAST info.m_text;
1956 if ( lvItem.pszText )
1957 lvItem.cchTextMax = info.m_text.Length();
1958 else
1959 lvItem.cchTextMax = 0;
1960 }
1961 }
1962 if (info.m_mask & wxLIST_MASK_IMAGE)
1963 lvItem.mask |= LVIF_IMAGE;
1964 if (info.m_mask & wxLIST_MASK_DATA)
1965 lvItem.mask |= LVIF_PARAM;
1966 }
1967
1968 // ----------------------------------------------------------------------------
1969 // List event
1970 // ----------------------------------------------------------------------------
1971
1972 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
1973
1974 wxListEvent::wxListEvent(wxEventType commandType, int id)
1975 : wxNotifyEvent(commandType, id)
1976 {
1977 m_code = 0;
1978 m_itemIndex = 0;
1979 m_oldItemIndex = 0;
1980 m_col = 0;
1981 m_cancelled = FALSE;
1982 }
1983
1984 #endif // __WIN95__
1985