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